lib: record output argument positions in zlog

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>
This commit is contained in:
David Lamparter 2021-03-22 13:45:20 +01:00 committed by David Lamparter
parent ba9256d2d0
commit e3daa82c18
2 changed files with 36 additions and 1 deletions

View 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

View 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 */