diff --git a/config.c b/config.c index 69bb2f07..8376877a 100644 --- a/config.c +++ b/config.c @@ -752,7 +752,7 @@ reload_config(t_configuration_options *orig_options) { log_notice(_("restarting logging with changed parameters\n")); logger_shutdown(); - logger_init(orig_options, progname(), false); + logger_init(orig_options, progname()); } if (config_changed == true) diff --git a/log.c b/log.c index a498e5f3..db40682b 100644 --- a/log.c +++ b/log.c @@ -48,6 +48,11 @@ int log_level = LOG_NOTICE; int last_log_level = LOG_NOTICE; int verbose_logging = false; int terse_logging = false; +/* + * Global variable to be set by the main application to ensure any log output + * emitted before logger_init is called, is output in the correct format + */ +int logger_output_mode = OM_DAEMON; extern void stderr_log_with_level(const char *level_name, int level, const char *fmt, ...) @@ -62,9 +67,7 @@ stderr_log_with_level(const char *level_name, int level, const char *fmt, ...) static void _stderr_log_with_level(const char *level_name, int level, const char *fmt, va_list ap) { - time_t t; - struct tm *tm; - char buff[100]; + char buf[100]; /* * Store the requested level so that if there's a subsequent @@ -74,10 +77,21 @@ _stderr_log_with_level(const char *level_name, int level, const char *fmt, va_li if (log_level >= level) { - time(&t); - tm = localtime(&t); - strftime(buff, 100, "[%Y-%m-%d %H:%M:%S]", tm); - fprintf(stderr, "%s [%s] ", buff, level_name); + + /* Format log line prefix with timestamp if in daemon mode */ + if (logger_output_mode == OM_DAEMON) + { + time_t t; + struct tm *tm; + time(&t); + tm = localtime(&t); + strftime(buf, 100, "[%Y-%m-%d %H:%M:%S]", tm); + fprintf(stderr, "%s [%s] ", buf, level_name); + } + else + { + fprintf(stderr, "%s: ", level_name); + } vfprintf(stderr, fmt, ap); @@ -142,7 +156,7 @@ log_verbose(int level, const char *fmt, ...) bool -logger_init(t_configuration_options *opts, const char *ident, bool stderr_only) +logger_init(t_configuration_options *opts, const char *ident) { char *level = opts->loglevel; char *facility = opts->logfacility; @@ -180,7 +194,7 @@ logger_init(t_configuration_options *opts, const char *ident, bool stderr_only) * STDERR only logging requested - finish here without setting up any further * logging facility. */ - if (stderr_only == true) + if (logger_output_mode == OM_COMMAND_LINE) return true; if (facility && *facility) diff --git a/log.h b/log.h index 23423869..f7cffcc7 100644 --- a/log.h +++ b/log.h @@ -25,6 +25,9 @@ #define REPMGR_SYSLOG 1 #define REPMGR_STDERR 2 +#define OM_COMMAND_LINE 1 +#define OM_DAEMON 2 + extern void stderr_log_with_level(const char *level_name, int level, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4))); @@ -116,7 +119,7 @@ int detect_log_level(const char *level); /* Logger initialisation and shutdown */ -bool logger_init(t_configuration_options * opts, const char *ident, bool stderr_only); +bool logger_init(t_configuration_options * opts, const char *ident); bool logger_shutdown(void); @@ -132,5 +135,6 @@ extern int log_type; extern int log_level; extern int verbose_logging; extern int terse_logging; +extern int logger_output_mode; #endif /* _REPMGR_LOG_H_ */ diff --git a/repmgr.c b/repmgr.c index 667129f7..5a94fde1 100644 --- a/repmgr.c +++ b/repmgr.c @@ -265,6 +265,13 @@ main(int argc, char **argv) exit(1); } + /* + * Tell the logger we're a command-line program - this will + * ensure any output logged before the logger is initialized + * will be formatted correctly + */ + logger_output_mode = OM_COMMAND_LINE; + initialize_conninfo_params(&source_conninfo, true); /* @@ -837,7 +844,7 @@ main(int argc, char **argv) * which makes little sense for a command line program. */ - logger_init(&options, progname(), true); + logger_init(&options, progname()); if (runtime_options.verbose) logger_set_verbose(); diff --git a/repmgrd.c b/repmgrd.c index f8beb50b..00236e72 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -207,6 +207,13 @@ main(int argc, char **argv) } } + + /* + * Tell the logger we're a daemon - this will ensure any output logged + * before the logger is initialized will be formatted correctly + */ + logger_output_mode = OM_DAEMON; + /* * Parse the configuration file, if provided. If no configuration file * was provided, or one was but was incomplete, parse_config() will @@ -246,7 +253,7 @@ main(int argc, char **argv) strerror(errno)); } - logger_init(&local_options, progname(), false); + logger_init(&local_options, progname()); if (verbose) logger_set_verbose();