]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: errors: implement parsing context type
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 27 May 2021 13:45:28 +0000 (15:45 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Mon, 7 Jun 2021 14:58:16 +0000 (16:58 +0200)
Create a parsing_ctx structure. This type is used to store information
about the current file/line parsed. A global context is created and
can be manipulated when haproxy is in STARTING mode. When starting is
over, the context is resetted and should not be accessed anymore.

include/haproxy/errors.h
src/errors.c
src/haproxy.c

index aaf5cd86630184f4e0c26960760653e769ffe88b..6dd60f1e1e2341cc1c4c464a7ce102163e523e43 100644 (file)
@@ -60,12 +60,22 @@ enum {
 };
 
 
-void usermsgs_clr(void);
+void usermsgs_clr(const char *prefix);
 int usermsgs_empty(void);
 const char *usermsgs_str(void);
 
 /************ Error reporting functions ***********/
 
+struct usermsgs_ctx {
+       const char *prefix;  /* prefix of every output */
+       const char *file;    /* related filename for config parsing */
+       int line;            /* related line number for config parsing */
+       enum obj_type *obj;  /* related proxy, server, ... */
+};
+void set_usermsgs_ctx(const char *file, int line, enum obj_type *obj);
+void register_parsing_obj(enum obj_type *obj);
+void reset_usermsgs_ctx(void);
+
 /*
  * Displays the message on stderr with the date and pid. Overrides the quiet
  * mode during startup.
index 9da04f91846b0c753cce2f7867372c33bbf1dbf1..68fcf1af19998ea8a842306daa2a36ee738181ed 100644 (file)
@@ -22,6 +22,10 @@ static struct ring *startup_logs = NULL;
 #define USER_MESSAGES_BUFSIZE 1024
 static THREAD_LOCAL struct buffer usermsgs_buf = BUF_NULL;
 
+/* A thread local context used for stderr output via ha_alert/warning/notice/diag.
+ */
+static THREAD_LOCAL struct usermsgs_ctx usermsgs_ctx = { };
+
 /* Put msg in usermsgs_buf.
  *
  * The message should not be terminated by a newline because this function
@@ -49,13 +53,19 @@ static void usermsgs_put(const struct ist *msg)
        }
 }
 
-/* Clear the messages log buffer. */
-void usermsgs_clr(void)
+/* Clear the user messages log buffer.
+ *
+ * <prefix> will set the local-thread context appended to every output
+ * following this call. It can be NULL if not necessary.
+ */
+void usermsgs_clr(const char *prefix)
 {
        if (likely(!b_is_null(&usermsgs_buf))) {
                b_reset(&usermsgs_buf);
                usermsgs_buf.area[0] = '\0';
        }
+
+       usermsgs_ctx.prefix = prefix;
 }
 
 /* Check if the user messages buffer is empty. */
@@ -73,6 +83,40 @@ const char *usermsgs_str(void)
        return b_head(&usermsgs_buf);
 }
 
+/* Set thread-local context infos to prefix forthcoming stderr output during
+ * configuration parsing.
+ *
+ * <file> and <line> specify the location of the parsed configuration.
+ *
+ * <obj> can be of various types. If not NULL, the string prefix generated will
+ * depend on its type.
+ */
+void set_usermsgs_ctx(const char *file, int line, enum obj_type *obj)
+{
+       usermsgs_ctx.file = file;
+       usermsgs_ctx.line = line;
+       usermsgs_ctx.obj = obj;
+}
+
+/* Set thread-local context infos to prefix forthcoming stderr output. It will
+ * be set as a complement to possibly already defined file/line.
+ *
+ * <obj> can be of various types. If not NULL, the string prefix generated will
+ * depend on its type.
+ */
+void register_parsing_obj(enum obj_type *obj)
+{
+       usermsgs_ctx.obj = obj;
+}
+
+/* Reset thread-local context infos for stderr output. */
+void reset_usermsgs_ctx(void)
+{
+       usermsgs_ctx.file = NULL;
+       usermsgs_ctx.line = 0;
+       usermsgs_ctx.obj = NULL;
+}
+
 /* Generic function to display messages prefixed by a label */
 static void print_message(const char *label, const char *fmt, va_list argp)
 {
index 0e707f5ef4f0a536c492ea62a5ea8421df076535..f4c24ee7e76da86f050cd9b60208eb261010bda1 100644 (file)
@@ -3380,6 +3380,8 @@ int main(int argc, char **argv)
        }
 
        global.mode &= ~MODE_STARTING;
+       reset_usermsgs_ctx();
+
        /*
         * That's it : the central polling loop. Run until we stop.
         */