]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: errors: split errors reporting function from log.c
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 4 Jun 2021 09:20:32 +0000 (11:20 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Mon, 7 Jun 2021 14:58:15 +0000 (16:58 +0200)
Move functions related to errors output on stderr from log.c to a newly
created errors.c file. It targets print_message and
ha_alert/warning/notice/diag functions and related startup_logs feature.

Makefile
include/haproxy/errors.h
src/errors.c [new file with mode: 0644]
src/log.c

index 6571dc6fc457ead167b10fab666dd826ad10318e..5344df58190a7841d30cd35ef79c42961ebb81cf 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -896,7 +896,7 @@ OBJS += src/mux_h2.o src/mux_fcgi.o src/http_ana.o src/mux_h1.o src/stream.o   \
         src/hpack-tbl.o src/ebimtree.o src/auth.o src/ebsttree.o               \
         src/ebistree.o src/base64.o src/wdt.o src/pipe.o src/http_acl.o        \
         src/hpack-enc.o src/dict.o src/dgram.o src/init.o src/hpack-huff.o     \
-        src/freq_ctr.o src/ebtree.o src/hash.o src/version.o
+        src/freq_ctr.o src/ebtree.o src/hash.o src/version.o src/errors.o
 
 ifneq ($(TRACE),)
 OBJS += src/calltrace.o
index a3614b7f0c131e9e1408249739ab9c37ccb26d57..629b7209a9eabaa811d018ac360221b091e1322e 100644 (file)
@@ -60,7 +60,7 @@ enum {
 };
 
 
-/************ Error reporting functions from log.c ***********/
+/************ Error reporting functions ***********/
 
 /*
  * Displays the message on stderr with the date and pid. Overrides the quiet
diff --git a/src/errors.c b/src/errors.c
new file mode 100644 (file)
index 0000000..d3ca6f1
--- /dev/null
@@ -0,0 +1,190 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <syslog.h>
+
+#include <haproxy/api.h>
+#include <haproxy/applet-t.h>
+#include <haproxy/cli.h>
+#include <haproxy/errors.h>
+#include <haproxy/global.h>
+#include <haproxy/ring.h>
+#include <haproxy/tools.h>
+#include <haproxy/version.h>
+
+/* A global buffer used to store all startup alerts/warnings. It will then be
+ * retrieve on the CLI. */
+static struct ring *startup_logs = NULL;
+
+/* Generic function to display messages prefixed by a label */
+static void print_message(const char *label, const char *fmt, va_list argp)
+{
+       char *head, *msg;
+       char prefix[11]; // '[' + 8 chars + ']' + 0.
+
+       *prefix = '[';
+       strncpy(prefix + 1, label, sizeof(prefix) - 2);
+       msg = prefix + strlen(prefix);
+       *msg++ = ']';
+       while (msg < prefix + sizeof(prefix) - 1)
+               *msg++ = ' ';
+       *msg = 0;
+
+       head = msg = NULL;
+       memprintf(&head, "%s (%u) : ", prefix, (uint)getpid());
+       memvprintf(&msg, fmt, argp);
+
+       if (global.mode & MODE_STARTING) {
+               if (unlikely(!startup_logs))
+                       startup_logs = ring_new(STARTUP_LOG_SIZE);
+
+               if (likely(startup_logs)) {
+                       struct ist m[2];
+
+                       m[0] = ist(head);
+                       m[1] = ist(msg);
+                       /* trim the trailing '\n' */
+                       if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
+                               m[1].len--;
+                       ring_write(startup_logs, ~0, 0, 0, m, 2);
+               }
+       }
+
+       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.
+ */
+void ha_alert(const char *fmt, ...)
+{
+       va_list argp;
+
+       if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
+               if (!(warned & WARN_EXEC_PATH)) {
+                       const char *path = get_exec_path();
+
+                       warned |= WARN_EXEC_PATH;
+                       ha_notice("haproxy version is %s\n", haproxy_version);
+                       if (path)
+                               ha_notice("path to executable is %s\n", path);
+               }
+               va_start(argp, fmt);
+               print_message("ALERT", fmt, argp);
+               va_end(argp);
+       }
+}
+
+/*
+ * Displays the message on stderr with the date and pid.
+ */
+void ha_warning(const char *fmt, ...)
+{
+       va_list argp;
+
+       warned |= WARN_ANY;
+
+       if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
+               va_start(argp, fmt);
+               print_message("WARNING", fmt, argp);
+               va_end(argp);
+       }
+}
+
+/*
+ * Variant of _ha_diag_warning with va_list.
+ * Use it only if MODE_DIAG has been previously checked.
+ */
+void _ha_vdiag_warning(const char *fmt, va_list argp)
+{
+       print_message("DIAG", fmt, argp);
+}
+
+/*
+ * Output a diagnostic warning.
+ * Use it only if MODE_DIAG has been previously checked.
+ */
+void _ha_diag_warning(const char *fmt, ...)
+{
+       va_list argp;
+
+       va_start(argp, fmt);
+       _ha_vdiag_warning(fmt, argp);
+       va_end(argp);
+}
+
+/*
+ * Output a diagnostic warning. Do nothing of MODE_DIAG is not on.
+ */
+void ha_diag_warning(const char *fmt, ...)
+{
+       va_list argp;
+
+       if (global.mode & MODE_DIAG) {
+               va_start(argp, fmt);
+               _ha_vdiag_warning(fmt, argp);
+               va_end(argp);
+       }
+}
+
+/*
+ * Displays the message on stderr with the date and pid.
+ */
+void ha_notice(const char *fmt, ...)
+{
+       va_list argp;
+
+       if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
+               va_start(argp, fmt);
+               print_message("NOTICE", fmt, argp);
+               va_end(argp);
+       }
+}
+
+/*
+ * Displays the message on <out> only if quiet mode is not set.
+ */
+void qfprintf(FILE *out, const char *fmt, ...)
+{
+       va_list argp;
+
+       if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
+               va_start(argp, fmt);
+               vfprintf(out, fmt, argp);
+               fflush(out);
+               va_end(argp);
+       }
+}
+
+
+/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
+static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
+{
+       if (!cli_has_level(appctx, ACCESS_LVL_OPER))
+               return 1;
+
+       if (!startup_logs)
+               return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
+
+       return ring_attach_cli(startup_logs, appctx);
+}
+
+/* register cli keywords */
+static struct cli_kw_list cli_kws = {{ },{
+       { { "show", "startup-logs",  NULL }, "show startup-logs                       : report logs emitted during HAProxy startup", cli_parse_show_startup_logs, NULL, NULL },
+       {{},}
+}};
+
+INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
+
+
+static void deinit_errors_buffers()
+{
+       ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
+}
+
+REGISTER_PER_THREAD_FREE(deinit_errors_buffers);
index 7dabe16f8fa54631f6eab815eb73f77d058d0368..3ce41cf5fe5a738418649d1b74e6d5e0d9195eb7 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -27,7 +27,6 @@
 #include <haproxy/api.h>
 #include <haproxy/applet-t.h>
 #include <haproxy/cfgparse.h>
-#include <haproxy/cli.h>
 #include <haproxy/fd.h>
 #include <haproxy/frontend.h>
 #include <haproxy/global.h>
@@ -35,7 +34,6 @@
 #include <haproxy/listener.h>
 #include <haproxy/log.h>
 #include <haproxy/proxy.h>
-#include <haproxy/ring.h>
 #include <haproxy/sample.h>
 #include <haproxy/sink.h>
 #include <haproxy/ssl_sock.h>
@@ -43,7 +41,6 @@
 #include <haproxy/stream_interface.h>
 #include <haproxy/time.h>
 #include <haproxy/tools.h>
-#include <haproxy/version.h>
 
 /* global recv logs counter */
 int cum_log_messages;
@@ -232,10 +229,6 @@ THREAD_LOCAL char *logline = NULL;
  */
 THREAD_LOCAL char *logline_rfc5424 = NULL;
 
-/* A global buffer used to store all startup alerts/warnings. It will then be
- * retrieve on the CLI. */
-static struct ring *startup_logs = NULL;
-
 struct logformat_var_args {
        char *name;
        int mask;
@@ -1078,152 +1071,6 @@ int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file
 }
 
 
-/* Generic function to display messages prefixed by a label */
-static void print_message(const char *label, const char *fmt, va_list argp)
-{
-       char *head, *msg;
-       char prefix[11]; // '[' + 8 chars + ']' + 0.
-
-       *prefix = '[';
-       strncpy(prefix + 1, label, sizeof(prefix) - 2);
-       msg = prefix + strlen(prefix);
-       *msg++ = ']';
-       while (msg < prefix + sizeof(prefix) - 1)
-               *msg++ = ' ';
-       *msg = 0;
-
-       head = msg = NULL;
-       memprintf(&head, "%s (%u) : ", prefix, (uint)getpid());
-       memvprintf(&msg, fmt, argp);
-
-       if (global.mode & MODE_STARTING) {
-               if (unlikely(!startup_logs))
-                       startup_logs = ring_new(STARTUP_LOG_SIZE);
-
-               if (likely(startup_logs)) {
-                       struct ist m[2];
-
-                       m[0] = ist(head);
-                       m[1] = ist(msg);
-                       /* trim the trailing '\n' */
-                       if (m[1].len > 0 && m[1].ptr[m[1].len - 1] == '\n')
-                               m[1].len--;
-                       ring_write(startup_logs, ~0, 0, 0, m, 2);
-               }
-       }
-
-       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.
- */
-void ha_alert(const char *fmt, ...)
-{
-       va_list argp;
-
-       if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
-               if (!(warned & WARN_EXEC_PATH)) {
-                       const char *path = get_exec_path();
-
-                       warned |= WARN_EXEC_PATH;
-                       ha_notice("haproxy version is %s\n", haproxy_version);
-                       if (path)
-                               ha_notice("path to executable is %s\n", path);
-               }
-               va_start(argp, fmt);
-               print_message("ALERT", fmt, argp);
-               va_end(argp);
-       }
-}
-
-
-/*
- * Displays the message on stderr with the date and pid.
- */
-void ha_warning(const char *fmt, ...)
-{
-       va_list argp;
-
-       warned |= WARN_ANY;
-
-       if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
-               va_start(argp, fmt);
-               print_message("WARNING", fmt, argp);
-               va_end(argp);
-       }
-}
-
-/*
- * Variant of _ha_diag_warning with va_list.
- * Use it only if MODE_DIAG has been previously checked.
- */
-void _ha_vdiag_warning(const char *fmt, va_list argp)
-{
-       print_message("DIAG", fmt, argp);
-}
-
-/*
- * Output a diagnostic warning.
- * Use it only if MODE_DIAG has been previously checked.
- */
-void _ha_diag_warning(const char *fmt, ...)
-{
-       va_list argp;
-
-       va_start(argp, fmt);
-       _ha_vdiag_warning(fmt, argp);
-       va_end(argp);
-}
-
-/*
- * Output a diagnostic warning. Do nothing of MODE_DIAG is not on.
- */
-void ha_diag_warning(const char *fmt, ...)
-{
-       va_list argp;
-
-       if (global.mode & MODE_DIAG) {
-               va_start(argp, fmt);
-               _ha_vdiag_warning(fmt, argp);
-               va_end(argp);
-       }
-}
-
-/*
- * Displays the message on stderr with the date and pid.
- */
-void ha_notice(const char *fmt, ...)
-{
-       va_list argp;
-
-       if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
-               va_start(argp, fmt);
-               print_message("NOTICE", fmt, argp);
-               va_end(argp);
-       }
-}
-
-/*
- * Displays the message on <out> only if quiet mode is not set.
- */
-void qfprintf(FILE *out, const char *fmt, ...)
-{
-       va_list argp;
-
-       if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
-               va_start(argp, fmt);
-               vfprintf(out, fmt, argp);
-               fflush(out);
-               va_end(argp);
-       }
-}
-
 /*
  * returns log format, LOG_FORMAT_UNSPEC is return if not found.
  */
@@ -2136,7 +1983,6 @@ void deinit_log_buffers()
 {
        free(logline);
        free(logline_rfc5424);
-       ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
        logline           = NULL;
        logline_rfc5424   = NULL;
 }
@@ -4161,26 +4007,6 @@ out:
 }
 
 
-/* parse the "show startup-logs" command, returns 1 if a message is returned, otherwise zero */
-static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx *appctx, void *private)
-{
-       if (!cli_has_level(appctx, ACCESS_LVL_OPER))
-               return 1;
-
-       if (!startup_logs)
-               return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
-
-       return ring_attach_cli(startup_logs, appctx);
-}
-
-/* register cli keywords */
-static struct cli_kw_list cli_kws = {{ },{
-       { { "show", "startup-logs",  NULL }, "show startup-logs                       : report logs emitted during HAProxy startup", cli_parse_show_startup_logs, NULL, NULL },
-       {{},}
-}};
-
-INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
-
 /* config parsers for this section */
 REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);