From: David Lamparter Date: Mon, 31 Jul 2017 21:49:11 +0000 (+0200) Subject: lib: plug logging hole during startup X-Git-Tag: frr-4.0-dev~435^2~7 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=d34cb7f0b7a9ff4c484ae4158f22e0bebf17586b;p=mirror%2Ffrr.git lib: plug logging hole during startup zlog_* doesn't work in startup before we've loaded the real logging configuration. Add some code to log to stderr for that window of time. Signed-off-by: David Lamparter --- diff --git a/lib/libfrr.c b/lib/libfrr.c index 6ebf837a1d..55d7784752 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -693,6 +693,9 @@ void frr_run(struct thread_master *master) daemon_ctl_sock = -1; } + /* end fixed stderr startup logging */ + zlog_startup_stderr = false; + struct thread thread; while (thread_fetch(master, &thread)) thread_call(&thread); diff --git a/lib/log.c b/lib/log.c index 28e0865354..c64bdff466 100644 --- a/lib/log.c +++ b/lib/log.c @@ -41,6 +41,7 @@ DEFINE_MTYPE_STATIC(LIB, ZLOG, "Logging") static int logfile_fd = -1; /* Used in signal handler. */ struct zlog *zlog_default = NULL; +bool zlog_startup_stderr = true; const char *zlog_priority[] = { "emergencies", "alerts", "critical", "errors", "warnings", @@ -172,6 +173,25 @@ static void time_print(FILE *fp, struct timestamp_control *ctl) } +static void vzlog_file(struct zlog *zl, struct timestamp_control *tsctl, + const char *proto_str, int record_priority, + int priority, FILE *fp, const char *format, + va_list args) +{ + va_list ac; + + time_print(fp, tsctl); + if (record_priority) + fprintf(fp, "%s: ", zlog_priority[priority]); + + fprintf(fp, "%s", proto_str); + va_copy(ac, args); + vfprintf(fp, format, ac); + va_end(ac); + fprintf(fp, "\n"); + fflush(fp); +} + /* va_list version of zlog. */ void vzlog(int priority, const char *format, va_list args) { @@ -210,32 +230,21 @@ void vzlog(int priority, const char *format, va_list args) sprintf(proto_str, "%s: ", zl->protoname); /* File output. */ - if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp) { - va_list ac; - time_print(zl->fp, &tsctl); - if (zl->record_priority) - fprintf(zl->fp, "%s: ", zlog_priority[priority]); - fprintf(zl->fp, "%s", proto_str); - va_copy(ac, args); - vfprintf(zl->fp, format, ac); - va_end(ac); - fprintf(zl->fp, "\n"); - fflush(zl->fp); - } - - /* stdout output. */ - if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT]) { - va_list ac; - time_print(stdout, &tsctl); - if (zl->record_priority) - fprintf(stdout, "%s: ", zlog_priority[priority]); - fprintf(stdout, "%s", proto_str); - va_copy(ac, args); - vfprintf(stdout, format, ac); - va_end(ac); - fprintf(stdout, "\n"); - fflush(stdout); - } + if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp) + vzlog_file(zl, &tsctl, proto_str, zl->record_priority, + priority, zl->fp, format, args); + + /* fixed-config logging to stderr while we're stating up & haven't + * daemonized / reached mainloop yet + * + * note the "else" on stdout output -- we don't want to print the same + * message to both stderr and stdout. */ + if (zlog_startup_stderr && priority <= LOG_WARNING) + vzlog_file(zl, &tsctl, proto_str, 1, + priority, stderr, format, args); + else if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT]) + vzlog_file(zl, &tsctl, proto_str, zl->record_priority, + priority, stdout, format, args); /* Terminal monitor. */ if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR]) diff --git a/lib/log.h b/lib/log.h index d872ce56d6..07eb6d5bd5 100644 --- a/lib/log.h +++ b/lib/log.h @@ -24,6 +24,7 @@ #include #include +#include #include /* Here is some guidance on logging levels to use: @@ -54,6 +55,8 @@ typedef enum { } zlog_dest_t; #define ZLOG_NUM_DESTS (ZLOG_DEST_FILE+1) +extern bool zlog_startup_stderr; + /* Message structure. */ struct message { int key;