]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: record output argument positions in zlog
authorDavid Lamparter <equinox@diac24.net>
Mon, 22 Mar 2021 12:45:20 +0000 (13:45 +0100)
committerDavid Lamparter <equinox@opensourcerouting.org>
Fri, 18 Jun 2021 18:56:53 +0000 (20:56 +0200)
printfrr() recently acquired the capability to record start/end of
formatting outputs.  Make use of this in the zlog code so logging
targets have access to this information.

(This also records how long the `[XXXXX-XXXXX][EC 9999999]` prefix was
so log targets can choose to skip over it.)

Signed-off-by: David Lamparter <equinox@diac24.net>
lib/zlog.c
lib/zlog.h

index 89ab9265d1c3bc2df0d370a8b51f97409fe4592c..c96e2fc08ee7e469d443a789e46aa1107f926a79 100644 (file)
@@ -107,6 +107,7 @@ struct zlog_msg {
        size_t stackbufsz;
        char *text;
        size_t textlen;
+       size_t hdrlen;
 
        /* This is always ISO8601 with sub-second precision 9 here, it's
         * converted for callers as needed.  ts_dot points to the "."
@@ -118,6 +119,15 @@ struct zlog_msg {
         */
        uint32_t ts_flags;
        char ts_str[32], *ts_dot, ts_zonetail[8];
+
+       /* at the time of writing, 16 args was the actual maximum of arguments
+        * to a single zlog call.  Particularly printing flag bitmasks seems
+        * to drive this.  That said, the overhead of dynamically sizing this
+        * probably outweighs the value.  If anything, a printfrr extension
+        * for printing flag bitmasks might be a good idea.
+        */
+       struct fmt_outpos argpos[24];
+       size_t n_argpos;
 };
 
 /* thread-local log message buffering
@@ -592,9 +602,13 @@ const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen)
                if (need)
                        need += bputch(&fb, ' ');
 
-               hdrlen = need;
+               msg->hdrlen = hdrlen = need;
                assert(hdrlen < msg->stackbufsz);
 
+               fb.outpos = msg->argpos;
+               fb.outpos_n = array_size(msg->argpos);
+               fb.outpos_i = 0;
+
                va_copy(args, msg->args);
                need += vbprintfrr(&fb, msg->fmt, args);
                va_end(args);
@@ -612,6 +626,7 @@ const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen)
                        fb.buf = msg->text;
                        fb.len = need;
                        fb.pos = msg->text + hdrlen;
+                       fb.outpos_i = 0;
 
                        va_copy(args, msg->args);
                        vbprintfrr(&fb, msg->fmt, args);
@@ -619,12 +634,28 @@ const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen)
 
                        bputch(&fb, '\0');
                }
+
+               msg->n_argpos = fb.outpos_i;
        }
        if (textlen)
                *textlen = msg->textlen;
        return msg->text;
 }
 
+void zlog_msg_args(struct zlog_msg *msg, size_t *hdrlen, size_t *n_argpos,
+                  const struct fmt_outpos **argpos)
+{
+       if (!msg->text)
+               zlog_msg_text(msg, NULL);
+
+       if (hdrlen)
+               *hdrlen = msg->hdrlen;
+       if (n_argpos)
+               *n_argpos = msg->n_argpos;
+       if (argpos)
+               *argpos = msg->argpos;
+}
+
 #define ZLOG_TS_FORMAT         (ZLOG_TS_ISO8601 | ZLOG_TS_LEGACY)
 #define ZLOG_TS_FLAGS          ~ZLOG_TS_PREC
 
index c421c16f38835dfe63a0b12a8bb3d06c146f82b4..b463606d974e4cc81a96aa6198860167c1601cb5 100644 (file)
@@ -31,6 +31,7 @@
 #include "frrcu.h"
 #include "memory.h"
 #include "hook.h"
+#include "printfrr.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -146,6 +147,9 @@ extern const struct xref_logmsg *zlog_msg_xref(struct zlog_msg *msg);
 /* pass NULL as textlen if you don't need it. */
 extern const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen);
 
+extern void zlog_msg_args(struct zlog_msg *msg, size_t *hdrlen,
+                         size_t *n_argpos, const struct fmt_outpos **argpos);
+
 /* timestamp formatting control flags */
 
 /* sub-second digit count */