From 75a6ea7f53999e2fccc96aa1365e7dfabb2667b4 Mon Sep 17 00:00:00 2001 From: Andy Barry Date: Tue, 8 Oct 2024 09:38:08 -0400 Subject: [PATCH 1/4] Handle SIGHUP to cause the log to rotate/get a new one manually. --- tools/cpp/logger/main.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tools/cpp/logger/main.cpp b/tools/cpp/logger/main.cpp index 2113efb6..22210514 100644 --- a/tools/cpp/logger/main.cpp +++ b/tools/cpp/logger/main.cpp @@ -32,6 +32,7 @@ using namespace std; #include "platform.hpp" static atomic_int done {0}; +static atomic_int got_sighup {0}; struct Args { @@ -580,6 +581,19 @@ struct Logger } } + // Did we get a SIGHUP and the user wants a new logfile? + if (got_sighup) { + log->close(); + if (args.rotate > 0) + rotate_logfiles(); + if (!openLogfile()) exit(1); + num_splits++; + logsize = 0; + last_report_logsize = 0; + + got_sighup = 0; + } + if (log->writeEvent(le) != 0) { static u64 last_spew_utime = 0; string reason = strerror(errno); @@ -650,6 +664,12 @@ void sighandler(int signal) if (done == 3) exit(1); } +void sighup_handler(int signal) +{ + got_sighup = 1; + logger.wakeup(); +} + int main(int argc, char *argv[]) { Platform::setstreambuf(); @@ -683,6 +703,7 @@ int main(int argc, char *argv[]) signal(SIGINT, sighandler); signal(SIGQUIT, sighandler); signal(SIGTERM, sighandler); + signal(SIGHUP, sighup_handler); ZCM_DEBUG("Starting zcms"); for (auto& z : zcms) z->start(); From a44d43a12def80e5353ad737f95cf6d22999990a Mon Sep 17 00:00:00 2001 From: Andy Barry Date: Mon, 14 Oct 2024 11:52:01 -0400 Subject: [PATCH 2/4] Ignore SIGHUP if the user has not used --rotate or --increment. Add some more context to the --rotate / --increment error message. --- tools/cpp/logger/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/cpp/logger/main.cpp b/tools/cpp/logger/main.cpp index 22210514..ee8b34ec 100644 --- a/tools/cpp/logger/main.cpp +++ b/tools/cpp/logger/main.cpp @@ -208,7 +208,7 @@ struct Args } if (rotate > 0 && auto_increment) { - cerr << "ERROR. --increment and --rotate can't both be used" << endl; + cerr << "ERROR. --increment and --rotate can't both be used. Note that if you don't want --increment, you must specify a log filename." << endl; return false; } @@ -582,7 +582,7 @@ struct Logger } // Did we get a SIGHUP and the user wants a new logfile? - if (got_sighup) { + if (got_sighup && (args.auto_increment || (args.rotate > 0))) { log->close(); if (args.rotate > 0) rotate_logfiles(); From fbe35b5fddffef58672bee3b6fd87a2b4ebcb144 Mon Sep 17 00:00:00 2001 From: Andy Barry Date: Fri, 18 Oct 2024 16:56:40 -0400 Subject: [PATCH 3/4] Only register SIGHUP handler when needed, fix line length. --- tools/cpp/logger/main.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tools/cpp/logger/main.cpp b/tools/cpp/logger/main.cpp index ee8b34ec..92576825 100644 --- a/tools/cpp/logger/main.cpp +++ b/tools/cpp/logger/main.cpp @@ -208,7 +208,8 @@ struct Args } if (rotate > 0 && auto_increment) { - cerr << "ERROR. --increment and --rotate can't both be used. Note that if you don't want --increment, you must specify a log filename." << endl; + cerr << "ERROR. --increment and --rotate can't both be used." << endl + << "Note that if you don't want --increment, you must specify a log filename." << endl; return false; } @@ -582,7 +583,7 @@ struct Logger } // Did we get a SIGHUP and the user wants a new logfile? - if (got_sighup && (args.auto_increment || (args.rotate > 0))) { + if (got_sighup) { log->close(); if (args.rotate > 0) rotate_logfiles(); @@ -703,7 +704,10 @@ int main(int argc, char *argv[]) signal(SIGINT, sighandler); signal(SIGQUIT, sighandler); signal(SIGTERM, sighandler); - signal(SIGHUP, sighup_handler); + + if (logger.args.auto_increment || logger.args.rotate > 0) { + signal(SIGHUP, sighup_handler); + } ZCM_DEBUG("Starting zcms"); for (auto& z : zcms) z->start(); From bc68ac68167f95611485b935ca408a53a74b0374 Mon Sep 17 00:00:00 2001 From: Jonathan Bendes Date: Mon, 28 Oct 2024 10:46:41 -0400 Subject: [PATCH 4/4] reviewed --- tools/cpp/logger/main.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tools/cpp/logger/main.cpp b/tools/cpp/logger/main.cpp index 92576825..f86d820e 100644 --- a/tools/cpp/logger/main.cpp +++ b/tools/cpp/logger/main.cpp @@ -209,7 +209,8 @@ struct Args if (rotate > 0 && auto_increment) { cerr << "ERROR. --increment and --rotate can't both be used." << endl - << "Note that if you don't want --increment, you must specify a log filename." << endl; + << "Note that if you don't want --increment, you must" << endl + << "specify a log filename." << endl; return false; } @@ -667,6 +668,7 @@ void sighandler(int signal) void sighup_handler(int signal) { + if (!logger.args.auto_increment && logger.args.rotate <= 0) return; got_sighup = 1; logger.wakeup(); } @@ -704,10 +706,7 @@ int main(int argc, char *argv[]) signal(SIGINT, sighandler); signal(SIGQUIT, sighandler); signal(SIGTERM, sighandler); - - if (logger.args.auto_increment || logger.args.rotate > 0) { - signal(SIGHUP, sighup_handler); - } + signal(SIGHUP, sighup_handler); ZCM_DEBUG("Starting zcms"); for (auto& z : zcms) z->start();