From 60872ffcb57834aa242126ce114b2e73913b50fb Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Thu, 15 Feb 2024 05:42:58 -0500 Subject: [PATCH] Cosmetic and size improvements to logging. --- crontab.cpp | 198 +++++++++++++++++++++++++--------------------------- crontab.rl | 20 ++---- ncron.cpp | 22 +++--- 3 files changed, 112 insertions(+), 128 deletions(-) diff --git a/crontab.cpp b/crontab.cpp index ed38dbc..b47cad1 100644 --- a/crontab.cpp +++ b/crontab.cpp @@ -111,28 +111,20 @@ struct ParseCfgState inline void debug_print_ce() const { - if (!gflags_debug) - return; - log_line("=== NEW JOB ==="); - log_line("id: %d", ce->id_); + if (!gflags_debug) return; + log_line("=== NEW JOB ===\nid: %d", ce->id_); log_line("command: %s", ce->command_ ? ce->command_ : ""); log_line("args: %s", ce->args_ ? ce->args_ : ""); - log_line("numruns: %u", ce->numruns_); - log_line("maxruns: %u", ce->maxruns_); + log_line("numruns: %u\nmaxruns: %u", ce->numruns_, ce->maxruns_); log_line("journal: %s", ce->journal_ ? "true" : "false"); log_line("runat: %s", ce->runat_ ? "true" : "false"); - log_line("interval: %u", ce->interval_); - log_line("exectime: %lu", ce->exectime_); - log_line("lasttime: %lu", ce->lasttime_); + log_line("interval: %u\nexectime: %lu\nlasttime: %lu", ce->interval_, ce->exectime_, ce->lasttime_); } inline void debug_print_ce_history() const { - if (!gflags_debug) - return; - log_line("[%d]->numruns = %u", ce->id_, ce->numruns_); - log_line("[%d]->exectime = %lu", ce->id_, ce->exectime_); - log_line("[%d]->lasttime = %lu", ce->id_, ce->lasttime_); + if (!gflags_debug) return; + log_line("numruns = %u\nexectime = %lu\nlasttime = %lu", ce->numruns_, ce->exectime_, ce->lasttime_); } void finish_ce() @@ -205,11 +197,11 @@ struct hstm { #define MARKED_HST() hst.st, (p > hst.st ? static_cast(p - hst.st) : 0) -#line 241 "crontab.rl" +#line 233 "crontab.rl" -#line 210 "crontab.cpp" +#line 202 "crontab.cpp" static const signed char _history_m_actions[] = { 0, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 0 @@ -274,7 +266,7 @@ static const int history_m_error = 0; static const int history_m_en_main = 1; -#line 243 "crontab.rl" +#line 235 "crontab.rl" static int do_parse_history(hstm &hst, const char *p, size_t plen) @@ -283,15 +275,15 @@ static int do_parse_history(hstm &hst, const char *p, size_t plen) const char *eof = pe; -#line 281 "crontab.cpp" +#line 273 "crontab.cpp" { hst.cs = (int)history_m_start; } -#line 250 "crontab.rl" +#line 242 "crontab.rl" -#line 286 "crontab.cpp" +#line 278 "crontab.cpp" { unsigned int _trans = 0; const char * _keys; @@ -335,16 +327,16 @@ static int do_parse_history(hstm &hst, const char *p, size_t plen) { case 0: { { -#line 210 "crontab.rl" +#line 202 "crontab.rl" hst.st = p; } -#line 332 "crontab.cpp" +#line 324 "crontab.cpp" break; } case 1: { { -#line 211 "crontab.rl" +#line 203 "crontab.rl" if (!nk::from_string(MARKED_HST(), &hst.h.lasttime)) { hst.parse_error = true; @@ -352,13 +344,13 @@ static int do_parse_history(hstm &hst, const char *p, size_t plen) } } -#line 345 "crontab.cpp" +#line 337 "crontab.cpp" break; } case 2: { { -#line 217 "crontab.rl" +#line 209 "crontab.rl" if (!nk::from_string(MARKED_HST(), &hst.h.numruns)) { hst.parse_error = true; @@ -366,13 +358,13 @@ static int do_parse_history(hstm &hst, const char *p, size_t plen) } } -#line 358 "crontab.cpp" +#line 350 "crontab.cpp" break; } case 3: { { -#line 223 "crontab.rl" +#line 215 "crontab.rl" if (!nk::from_string(MARKED_HST(), &hst.h.exectime)) { hst.parse_error = true; @@ -380,13 +372,13 @@ static int do_parse_history(hstm &hst, const char *p, size_t plen) } } -#line 371 "crontab.cpp" +#line 363 "crontab.cpp" break; } case 4: { { -#line 229 "crontab.rl" +#line 221 "crontab.rl" if (!nk::from_string(MARKED_HST(), &hst.id)) { hst.parse_error = true; @@ -394,7 +386,7 @@ static int do_parse_history(hstm &hst, const char *p, size_t plen) } } -#line 384 "crontab.cpp" +#line 376 "crontab.cpp" break; } @@ -418,7 +410,7 @@ static int do_parse_history(hstm &hst, const char *p, size_t plen) _out: {} } -#line 251 "crontab.rl" +#line 243 "crontab.rl" if (hst.parse_error) return -1; @@ -552,11 +544,11 @@ struct Pckm { }; -#line 427 "crontab.rl" +#line 419 "crontab.rl" -#line 542 "crontab.cpp" +#line 534 "crontab.cpp" static const signed char _parse_cmd_key_m_actions[] = { 0, 1, 0, 1, 1, 1, 2, 2, 0, 2, 2, 1, 0, 2, 1, 2, @@ -625,7 +617,7 @@ static const int parse_cmd_key_m_error = 0; static const int parse_cmd_key_m_en_main = 1; -#line 429 "crontab.rl" +#line 421 "crontab.rl" static void parse_command_key(ParseCfgState &ncs) @@ -642,15 +634,15 @@ static void parse_command_key(ParseCfgState &ncs) } -#line 625 "crontab.cpp" +#line 617 "crontab.cpp" { pckm.cs = (int)parse_cmd_key_m_start; } -#line 444 "crontab.rl" +#line 436 "crontab.rl" -#line 630 "crontab.cpp" +#line 622 "crontab.cpp" { unsigned int _trans = 0; const char * _keys; @@ -694,16 +686,16 @@ static void parse_command_key(ParseCfgState &ncs) { case 0: { { -#line 387 "crontab.rl" +#line 379 "crontab.rl" pckm.st = p; } -#line 676 "crontab.cpp" +#line 668 "crontab.cpp" break; } case 1: { { -#line 388 "crontab.rl" +#line 380 "crontab.rl" size_t l = p > pckm.st ? static_cast(p - pckm.st) : 0; if (l) { @@ -729,13 +721,13 @@ static void parse_command_key(ParseCfgState &ncs) } } -#line 707 "crontab.cpp" +#line 699 "crontab.cpp" break; } case 2: { { -#line 412 "crontab.rl" +#line 404 "crontab.rl" size_t l = p > pckm.st ? static_cast(p - pckm.st) : 0; if (l) { @@ -746,7 +738,7 @@ static void parse_command_key(ParseCfgState &ncs) } } -#line 723 "crontab.cpp" +#line 715 "crontab.cpp" break; } @@ -770,7 +762,7 @@ static void parse_command_key(ParseCfgState &ncs) _out: {} } -#line 445 "crontab.rl" +#line 437 "crontab.rl" if (pckm.cs == parse_cmd_key_m_error) { @@ -804,11 +796,11 @@ static void parse_int_value(const char *p, const char *start, size_t linenum, in } -#line 584 "crontab.rl" +#line 576 "crontab.rl" -#line 781 "crontab.cpp" +#line 773 "crontab.cpp" static const signed char _ncrontab_actions[] = { 0, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, @@ -1046,7 +1038,7 @@ static const int ncrontab_error = 0; static const int ncrontab_en_main = 1; -#line 586 "crontab.rl" +#line 578 "crontab.rl" static int do_parse_config(ParseCfgState &ncs, const char *p, size_t plen) @@ -1055,15 +1047,15 @@ static int do_parse_config(ParseCfgState &ncs, const char *p, size_t plen) const char *eof = pe; -#line 1025 "crontab.cpp" +#line 1017 "crontab.cpp" { ncs.cs = (int)ncrontab_start; } -#line 593 "crontab.rl" +#line 585 "crontab.rl" -#line 1030 "crontab.cpp" +#line 1022 "crontab.cpp" { unsigned int _trans = 0; const char * _keys; @@ -1107,135 +1099,135 @@ static int do_parse_config(ParseCfgState &ncs, const char *p, size_t plen) { case 0: { { -#line 484 "crontab.rl" +#line 476 "crontab.rl" ncs.time_st = p; ncs.v_time = 0; } -#line 1076 "crontab.cpp" +#line 1068 "crontab.cpp" break; } case 1: { { -#line 485 "crontab.rl" +#line 477 "crontab.rl" parse_time_unit(ncs, p, 1, &ncs.v_time); } -#line 1084 "crontab.cpp" +#line 1076 "crontab.cpp" break; } case 2: { { -#line 486 "crontab.rl" +#line 478 "crontab.rl" parse_time_unit(ncs, p, 60, &ncs.v_time); } -#line 1092 "crontab.cpp" +#line 1084 "crontab.cpp" break; } case 3: { { -#line 487 "crontab.rl" +#line 479 "crontab.rl" parse_time_unit(ncs, p, 3600, &ncs.v_time); } -#line 1100 "crontab.cpp" +#line 1092 "crontab.cpp" break; } case 4: { { -#line 488 "crontab.rl" +#line 480 "crontab.rl" parse_time_unit(ncs, p, 86400, &ncs.v_time); } -#line 1108 "crontab.cpp" +#line 1100 "crontab.cpp" break; } case 5: { { -#line 489 "crontab.rl" +#line 481 "crontab.rl" parse_time_unit(ncs, p, 604800, &ncs.v_time); } -#line 1116 "crontab.cpp" +#line 1108 "crontab.cpp" break; } case 6: { { -#line 491 "crontab.rl" +#line 483 "crontab.rl" ncs.intv_st = p; ncs.v_int1 = ncs.v_int2 = 0; ncs.intv2_exist = false; } -#line 1128 "crontab.cpp" +#line 1120 "crontab.cpp" break; } case 7: { { -#line 496 "crontab.rl" +#line 488 "crontab.rl" parse_int_value(p, ncs.intv_st, ncs.linenum, &ncs.v_int1); } -#line 1136 "crontab.cpp" +#line 1128 "crontab.cpp" break; } case 8: { { -#line 497 "crontab.rl" +#line 489 "crontab.rl" ncs.intv2_st = p; } -#line 1144 "crontab.cpp" +#line 1136 "crontab.cpp" break; } case 9: { { -#line 498 "crontab.rl" +#line 490 "crontab.rl" parse_int_value(p, ncs.intv2_st, ncs.linenum, &ncs.v_int2); ncs.intv2_exist = true; } -#line 1152 "crontab.cpp" +#line 1144 "crontab.cpp" break; } case 10: { { -#line 499 "crontab.rl" +#line 491 "crontab.rl" using std::swap; swap(ncs.v_int1, ncs.v_int3); swap(ncs.v_int2, ncs.v_int4); } -#line 1164 "crontab.cpp" +#line 1156 "crontab.cpp" break; } case 11: { { -#line 504 "crontab.rl" +#line 496 "crontab.rl" ncs.v_int3 = -1; ncs.v_int4 = -1; } -#line 1175 "crontab.cpp" +#line 1167 "crontab.cpp" break; } case 12: { { -#line 509 "crontab.rl" +#line 501 "crontab.rl" ncs.strv_st = p; ncs.v_strlen = 0; } -#line 1183 "crontab.cpp" +#line 1175 "crontab.cpp" break; } case 13: { { -#line 510 "crontab.rl" +#line 502 "crontab.rl" ncs.v_strlen = p > ncs.strv_st ? static_cast(p - ncs.strv_st) : 0; if (ncs.v_strlen >= sizeof ncs.v_str) { @@ -1246,22 +1238,22 @@ static int do_parse_config(ParseCfgState &ncs, const char *p, size_t plen) ncs.v_str[ncs.v_strlen] = 0; } -#line 1199 "crontab.cpp" +#line 1191 "crontab.cpp" break; } case 14: { { -#line 533 "crontab.rl" +#line 525 "crontab.rl" ncs.ce->journal_ = true; } -#line 1207 "crontab.cpp" +#line 1199 "crontab.cpp" break; } case 15: { { -#line 536 "crontab.rl" +#line 528 "crontab.rl" ncs.ce->runat_ = true; ncs.ce->exectime_ = ncs.v_int1; @@ -1269,100 +1261,100 @@ static int do_parse_config(ParseCfgState &ncs, const char *p, size_t plen) ncs.ce->journal_ = true; } -#line 1220 "crontab.cpp" +#line 1212 "crontab.cpp" break; } case 16: { { -#line 542 "crontab.rl" +#line 534 "crontab.rl" if (!ncs.ce->runat_) ncs.ce->maxruns_ = ncs.v_int1 > 0 ? static_cast(ncs.v_int1) : 0; } -#line 1231 "crontab.cpp" +#line 1223 "crontab.cpp" break; } case 17: { { -#line 550 "crontab.rl" +#line 542 "crontab.rl" ncs.ce->interval_ = ncs.v_time; } -#line 1239 "crontab.cpp" +#line 1231 "crontab.cpp" break; } case 18: { { -#line 559 "crontab.rl" +#line 551 "crontab.rl" add_cst_mon(ncs); } -#line 1247 "crontab.cpp" +#line 1239 "crontab.cpp" break; } case 19: { { -#line 560 "crontab.rl" +#line 552 "crontab.rl" add_cst_mday(ncs); } -#line 1255 "crontab.cpp" +#line 1247 "crontab.cpp" break; } case 20: { { -#line 561 "crontab.rl" +#line 553 "crontab.rl" add_cst_wday(ncs); } -#line 1263 "crontab.cpp" +#line 1255 "crontab.cpp" break; } case 21: { { -#line 562 "crontab.rl" +#line 554 "crontab.rl" add_cst_time(ncs); } -#line 1271 "crontab.cpp" +#line 1263 "crontab.cpp" break; } case 22: { { -#line 569 "crontab.rl" +#line 561 "crontab.rl" parse_command_key(ncs); } -#line 1279 "crontab.cpp" +#line 1271 "crontab.cpp" break; } case 23: { { -#line 576 "crontab.rl" +#line 568 "crontab.rl" ncs.jobid_st = p; } -#line 1287 "crontab.cpp" +#line 1279 "crontab.cpp" break; } case 24: { { -#line 577 "crontab.rl" +#line 569 "crontab.rl" parse_int_value(p, ncs.jobid_st, ncs.linenum, &ncs.ce->id_); } -#line 1295 "crontab.cpp" +#line 1287 "crontab.cpp" break; } case 25: { { -#line 578 "crontab.rl" +#line 570 "crontab.rl" ncs.finish_ce(); ncs.create_ce(); } -#line 1303 "crontab.cpp" +#line 1295 "crontab.cpp" break; } @@ -1386,7 +1378,7 @@ static int do_parse_config(ParseCfgState &ncs, const char *p, size_t plen) _out: {} } -#line 594 "crontab.rl" +#line 586 "crontab.rl" if (ncs.cs == ncrontab_error) diff --git a/crontab.rl b/crontab.rl index 0afe183..601708c 100644 --- a/crontab.rl +++ b/crontab.rl @@ -110,28 +110,20 @@ struct ParseCfgState inline void debug_print_ce() const { - if (!gflags_debug) - return; - log_line("=== NEW JOB ==="); - log_line("id: %d", ce->id_); + if (!gflags_debug) return; + log_line("=== NEW JOB ===\nid: %d", ce->id_); log_line("command: %s", ce->command_ ? ce->command_ : ""); log_line("args: %s", ce->args_ ? ce->args_ : ""); - log_line("numruns: %u", ce->numruns_); - log_line("maxruns: %u", ce->maxruns_); + log_line("numruns: %u\nmaxruns: %u", ce->numruns_, ce->maxruns_); log_line("journal: %s", ce->journal_ ? "true" : "false"); log_line("runat: %s", ce->runat_ ? "true" : "false"); - log_line("interval: %u", ce->interval_); - log_line("exectime: %lu", ce->exectime_); - log_line("lasttime: %lu", ce->lasttime_); + log_line("interval: %u\nexectime: %lu\nlasttime: %lu", ce->interval_, ce->exectime_, ce->lasttime_); } inline void debug_print_ce_history() const { - if (!gflags_debug) - return; - log_line("[%d]->numruns = %u", ce->id_, ce->numruns_); - log_line("[%d]->exectime = %lu", ce->id_, ce->exectime_); - log_line("[%d]->lasttime = %lu", ce->id_, ce->lasttime_); + if (!gflags_debug) return; + log_line("numruns = %u\nexectime = %lu\nlasttime = %lu", ce->numruns_, ce->exectime_, ce->lasttime_); } void finish_ce() diff --git a/ncron.cpp b/ncron.cpp index 8d529c7..3128fba 100644 --- a/ncron.cpp +++ b/ncron.cpp @@ -63,13 +63,13 @@ static Job * deadstackl; { auto f = fopen(g_ncron_execfile_tmp, "w"); if (!f) { - log_line("%s: failed to open history file %s for write", __func__, g_ncron_execfile_tmp); + log_line("Failed to open history file %s for write", g_ncron_execfile_tmp); return false; } auto do_save = [&f](Job *j) -> bool { for (; j; j = j->next_) { if (fprintf(f, "%d=%li:%u|%lu\n", j->id_, j->exectime_, j->numruns_, j->lasttime_) < 0) { - log_line("%s: failed writing to history file %s", __func__, g_ncron_execfile_tmp); + log_line("Failed to write to history file %s", g_ncron_execfile_tmp); return false; } } @@ -83,7 +83,7 @@ static Job * deadstackl; } if (rename(g_ncron_execfile_tmp, g_ncron_execfile)) { - log_line("%s: failed to update to new history file (%s => %s): %s", __func__, + log_line("Failed to update history file (%s => %s): %s", g_ncron_execfile_tmp, g_ncron_execfile, strerror(errno)); return false; } @@ -150,7 +150,7 @@ static void fix_signals(void) static void fail_on_fdne(char const *file, int mode) { if (access(file, mode)) { - log_line("%s: file '%s' does not exist or is not %s", __func__, + log_line("File '%s' does not exist or is not %s", file, (mode & W_OK) ? "writable" : "readable"); exit(EXIT_FAILURE); } @@ -165,7 +165,7 @@ static void sleep_or_die(struct timespec *ts) if (pending_save_and_exit) save_and_exit(); goto retry; } - log_line("%s: clock_nanosleep failed: %s", __func__, strerror(r)); + log_line("clock_nanosleep failed: %s", strerror(r)); exit(EXIT_FAILURE); } } @@ -173,7 +173,7 @@ static void sleep_or_die(struct timespec *ts) void clock_or_die(struct timespec *ts) { if (clock_gettime(CLOCK_REALTIME, ts)) { - log_line("%s: clock_gettime failed: %s", __func__, strerror(errno)); + log_line("clock_gettime failed: %s", strerror(errno)); exit(EXIT_FAILURE); } } @@ -182,9 +182,9 @@ static inline void debug_stack_print(const struct timespec &ts) { if (!gflags_debug) return; if (stackl) - log_line("do_work: ts.tv_sec = %lu stack.front().exectime = %lu", ts.tv_sec, stackl->exectime_); + log_line("ts.tv_sec = %lu stack.front().exectime = %lu", ts.tv_sec, stackl->exectime_); for (auto j = stackl; j; j = j->next_) - log_line("do_work: job %d exectime = %lu", j->id_, j->exectime_); + log_line("job %d exectime = %lu", j->id_, j->exectime_); } static void do_work(unsigned initial_sleep) @@ -209,7 +209,7 @@ static void do_work(unsigned initial_sleep) while (stackl->exectime_ <= ts.tv_sec) { auto j = stackl; if (gflags_debug) - log_line("do_work: DISPATCH %d (%lu <= %lu)", j->id_, j->exectime_, ts.tv_sec); + log_line("DISPATCH %d (%lu <= %lu)", j->id_, j->exectime_, ts.tv_sec); j->exec(ts); if (j->journal_ || g_ncron_execmode == Execmode::journal) @@ -238,7 +238,7 @@ static void do_work(unsigned initial_sleep) ts.tv_sec = j->exectime_; ts.tv_nsec = 0; if (gflags_debug) - log_line("do_work: SLEEP %zu seconds", tdelta); + log_line("SLEEP %zu seconds", tdelta); } } } @@ -343,7 +343,7 @@ int main(int argc, char* argv[]) parse_config(g_ncron_conf, g_ncron_execfile, &stackl, &deadstackl); if (!stackl) { - log_line("%s: no jobs, exiting", __func__); + log_line("No jobs, exiting."); exit(EXIT_FAILURE); }