Skip to content

Commit

Permalink
Merge pull request #496 from ZeroCM/andybarry-logger_signal
Browse files Browse the repository at this point in the history
Andybarry logger signal
  • Loading branch information
jbendes authored Oct 28, 2024
2 parents 8aa3ef4 + 4bb97ed commit 6ce909b
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion tools/cpp/logger/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ using namespace std;
#include "platform.hpp"

static atomic_int done {0};
static atomic_int got_sighup {0};

struct Args
{
Expand Down Expand Up @@ -207,7 +208,9 @@ 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." << endl
<< "Note that if you don't want --increment, you must" << endl
<< "specify a log filename." << endl;
return false;
}

Expand Down Expand Up @@ -580,6 +583,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);
Expand Down Expand Up @@ -650,6 +666,13 @@ void sighandler(int signal)
if (done == 3) exit(1);
}

void sighup_handler(int signal)
{
if (!logger.args.auto_increment && logger.args.rotate <= 0) return;
got_sighup = 1;
logger.wakeup();
}

int main(int argc, char *argv[])
{
Platform::setstreambuf();
Expand Down Expand Up @@ -683,6 +706,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();
Expand Down

0 comments on commit 6ce909b

Please sign in to comment.