From 3bcdae106e76f3f7259a1e9c251993ec7e33dc96 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Sat, 5 Mar 2022 19:43:44 +0100 Subject: [PATCH] lib: add `monitor:` command line log target This provides direct raw log output with full metadata directly at startup regardless of configuration details. Signed-off-by: David Lamparter --- lib/log_vty.c | 16 ++++++++++++++++ lib/zlog_live.c | 16 ++++++++++++---- lib/zlog_live.h | 1 + 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/log_vty.c b/lib/log_vty.c index 682c9ea372..ef33a39d4a 100644 --- a/lib/log_vty.c +++ b/lib/log_vty.c @@ -427,6 +427,22 @@ void command_setup_early_logging(const char *dest, const char *level) set_log_file(&zt_file_cmdline, NULL, sep, nlevel); return; } + if (strcmp(type, "monitor") == 0 && sep) { + struct zlog_live_cfg cfg = {}; + unsigned long fd; + char *endp; + + sep++; + fd = strtoul(sep, &endp, 10); + if (!*sep || *endp) { + fprintf(stderr, "invalid monitor fd \"%s\"\n", sep); + exit(1); + } + + zlog_live_open_fd(&cfg, nlevel, fd); + zlog_live_disown(&cfg); + return; + } fprintf(stderr, "invalid log target \"%s\" (\"%s\")\n", type, dest); exit(1); diff --git a/lib/zlog_live.c b/lib/zlog_live.c index fd2291a86f..d871e20db1 100644 --- a/lib/zlog_live.c +++ b/lib/zlog_live.c @@ -182,8 +182,6 @@ static void zlog_live_sigsafe(struct zlog_target *zt, const char *text, void zlog_live_open(struct zlog_live_cfg *cfg, int prio_min, int *other_fd) { int sockets[2]; - struct zlt_live *zte; - struct zlog_target *zt; if (cfg->target) zlog_live_close(cfg); @@ -208,13 +206,23 @@ void zlog_live_open(struct zlog_live_cfg *cfg, int prio_min, int *other_fd) shutdown(sockets[0], SHUT_RD); *other_fd = sockets[1]; + zlog_live_open_fd(cfg, prio_min, sockets[0]); +} + +void zlog_live_open_fd(struct zlog_live_cfg *cfg, int prio_min, int fd) +{ + struct zlt_live *zte; + struct zlog_target *zt; + + if (cfg->target) + zlog_live_close(cfg); zt = zlog_target_clone(MTYPE_LOG_LIVE, NULL, sizeof(*zte)); zte = container_of(zt, struct zlt_live, zt); cfg->target = zte; - set_nonblocking(sockets[0]); - zte->fd = sockets[0]; + set_nonblocking(fd); + zte->fd = fd; zte->zt.prio_min = prio_min; zte->zt.logfn = zlog_live; zte->zt.logfn_sigsafe = zlog_live_sigsafe; diff --git a/lib/zlog_live.h b/lib/zlog_live.h index 5e80f016fd..ab8aae452f 100644 --- a/lib/zlog_live.h +++ b/lib/zlog_live.h @@ -68,6 +68,7 @@ struct zlog_live_cfg { extern void zlog_live_open(struct zlog_live_cfg *cfg, int prio_min, int *other_fd); +extern void zlog_live_open_fd(struct zlog_live_cfg *cfg, int prio_min, int fd); static inline bool zlog_live_is_null(struct zlog_live_cfg *cfg) { -- 2.39.5