diff --git a/lib/zlog.c b/lib/zlog.c index 89ab9265d1..c96e2fc08e 100644 --- a/lib/zlog.c +++ b/lib/zlog.c @@ -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 diff --git a/lib/zlog.h b/lib/zlog.h index c421c16f38..b463606d97 100644 --- a/lib/zlog.h +++ b/lib/zlog.h @@ -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 */