summaryrefslogtreecommitdiffstats
path: root/lib/zlog.c
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2021-04-08 14:28:43 +0200
committerDavid Lamparter <equinox@opensourcerouting.org>2021-06-18 21:05:21 +0200
commit243ff228a9b4b680c2c405320634f4f72c65191c (patch)
treecf0b77560a89a8d2a691f748bc30e7f7f2a3c5ea /lib/zlog.c
parentlib: use fbuf for zlog_msg_ts() (diff)
downloadfrr-243ff228a9b4b680c2c405320634f4f72c65191c.tar.xz
frr-243ff228a9b4b680c2c405320634f4f72c65191c.zip
lib: add RFC3164 logging timestamps
This is old-style syslog, used among other things for /dev/log. Signed-off-by: David Lamparter <equinox@diac24.net>
Diffstat (limited to 'lib/zlog.c')
-rw-r--r--lib/zlog.c37
1 files changed, 36 insertions, 1 deletions
diff --git a/lib/zlog.c b/lib/zlog.c
index e843c1e47..d88337cb8 100644
--- a/lib/zlog.c
+++ b/lib/zlog.c
@@ -117,8 +117,14 @@ struct zlog_msg {
* Valid if ZLOG_TS_ISO8601 is set.
* (0 if timestamp has not been formatted yet)
*/
- uint32_t ts_flags;
char ts_str[32], *ts_dot, ts_zonetail[8];
+ uint32_t ts_flags;
+
+ /* "mmm dd hh:mm:ss" for 3164 legacy syslog - too dissimilar from
+ * the above, so just kept separately here.
+ */
+ uint32_t ts_3164_flags;
+ char ts_3164_str[16];
/* at the time of writing, 16 args was the actual maximum of arguments
* to a single zlog call. Particularly printing flag bitmasks seems
@@ -746,6 +752,35 @@ size_t zlog_msg_ts(struct zlog_msg *msg, struct fbuf *out, uint32_t flags)
}
}
+size_t zlog_msg_ts_3164(struct zlog_msg *msg, struct fbuf *out, uint32_t flags)
+{
+ flags &= ZLOG_TS_UTC;
+
+ if (!msg->ts_3164_str[0] || flags != msg->ts_3164_flags) {
+ /* these are "hardcoded" in RFC3164, so they're here too... */
+ static const char *const months[12] = {
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
+ };
+ struct tm tm;
+
+ /* RFC3164 explicitly asks for local time, but common usage
+ * also includes UTC.
+ */
+ if (flags & ZLOG_TS_UTC)
+ gmtime_r(&msg->ts.tv_sec, &tm);
+ else
+ localtime_r(&msg->ts.tv_sec, &tm);
+
+ snprintfrr(msg->ts_3164_str, sizeof(msg->ts_3164_str),
+ "%3s %2d %02d:%02d:%02d", months[tm.tm_mon],
+ tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+
+ msg->ts_3164_flags = flags;
+ }
+ return bputs(out, msg->ts_3164_str);
+}
+
void zlog_set_prefix_ec(bool enable)
{
atomic_store_explicit(&zlog_ec, enable, memory_order_relaxed);