]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: log: Save alerts and warnings emitted during HAProxy startup
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 24 Oct 2017 09:44:05 +0000 (11:44 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 31 Oct 2017 10:36:13 +0000 (11:36 +0100)
Because we can't always display the standard error messages when HAProxy is
started, all alerts and warnings emitted during the startup will now be saved in
a buffer. It can also be handy to store these messages just in case you
missed something during the startup

To implement this feature, Alert and Warning functions now relies on
display_message. The difference is just on conditions to call this function and
it remains unchanged. In display_message, if MODE_STARTING flag is set, we save
the message.

src/log.c

index 2af9050a9c0c91f78925ce9343f95b0b4ea09324..ff1ab9a8163e9350304ee222c45a040daf9be3a5 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -225,6 +225,10 @@ char *logline = NULL;
  */
 char *logline_rfc5424 = NULL;
 
+/* A global buffer used to store all startup alerts/warnings. It will then be
+ * retrieve on the CLI. */
+static char *startup_logs = NULL;
+
 struct logformat_var_args {
        char *name;
        int mask;
@@ -664,6 +668,29 @@ int parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list
        return 1;
 }
 
+/* Generic function to display messages prefixed by a label */
+static void print_message(const char *label, const char *fmt, va_list argp)
+{
+       struct tm tm;
+       char *head, *msg;
+
+       head = msg = NULL;
+
+       get_localtime(date.tv_sec, &tm);
+       memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ",
+                 label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
+       memvprintf(&msg, fmt, argp);
+
+       if (global.mode & MODE_STARTING)
+               memprintf(&startup_logs, "%s%s%s", (startup_logs ? startup_logs : ""), head, msg);
+
+       fprintf(stderr, "%s%s", head, msg);
+       fflush(stderr);
+
+       free(head);
+       free(msg);
+}
+
 /*
  * Displays the message on stderr with the date and pid. Overrides the quiet
  * mode during startup.
@@ -671,16 +698,10 @@ int parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list
 void Alert(const char *fmt, ...)
 {
        va_list argp;
-       struct tm tm;
 
        if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
                va_start(argp, fmt);
-
-               get_localtime(date.tv_sec, &tm);
-               fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
-                       tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
-               vfprintf(stderr, fmt, argp);
-               fflush(stderr);
+               print_message("ALERT", fmt, argp);
                va_end(argp);
        }
 }
@@ -692,16 +713,10 @@ void Alert(const char *fmt, ...)
 void Warning(const char *fmt, ...)
 {
        va_list argp;
-       struct tm tm;
 
        if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
                va_start(argp, fmt);
-
-               get_localtime(date.tv_sec, &tm);
-               fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
-                       tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
-               vfprintf(stderr, fmt, argp);
-               fflush(stderr);
+               print_message("WARNING", fmt, argp);
                va_end(argp);
        }
 }
@@ -1343,10 +1358,12 @@ void deinit_log_buffers()
        free(logheader_rfc5424);
        free(logline);
        free(logline_rfc5424);
+       free(startup_logs);
        logheader         = NULL;
        logheader_rfc5424 = NULL;
        logline           = NULL;
        logline_rfc5424   = NULL;
+       startup_logs      = NULL;
 }
 
 /* Builds a log line in <dst> based on <list_format>, and stops before reaching