From eb7f9acda147543079f261753a11d2afa340fc5e Mon Sep 17 00:00:00 2001 From: Peter Korsgaard Date: Mon, 27 Jan 2020 15:36:41 +0100 Subject: [PATCH] syslogd: add config option to include milliseconds in timestamps For some use cases, having logs with more than 1 second accuracy can be helpful. Add an option to include milliseconds when adding a timestamp in HH:MM:SS.mmm format, similar to syslog-ng with fraq_digits(3) or journalctl -o short-precise. For simplicity, abuse the remaining space in the buffer used by ctime to add the millieconds (overwriting year). function old new delta timestamp_and_log 401 448 +47 Signed-off-by: Peter Korsgaard Signed-off-by: Denys Vlasenko --- sysklogd/syslogd.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index 0e226124a..ab50f4a28 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c @@ -64,6 +64,14 @@ //config: help //config: Supports restricted syslogd config. See docs/syslog.conf.txt //config: +//config:config FEATURE_SYSLOGD_PRECISE_TIMESTAMPS +//config: bool "Include milliseconds in timestamps" +//config: default n +//config: depends on SYSLOGD +//config: help +//config: Includes milliseconds (HH:MM:SS.mmm) in timestamp when +//config: timestamps are added. +//config: //config:config FEATURE_SYSLOGD_READ_BUFFER_SIZE //config: int "Read buffer size in bytes" //config: default 256 @@ -276,7 +284,7 @@ struct globals { /* ...then copy to parsebuf, escaping control chars */ /* (can grow x2 max) */ char parsebuf[MAX_READ*2]; - /* ...then sprintf into printbuf, adding timestamp (15 chars), + /* ...then sprintf into printbuf, adding timestamp (15 or 19 chars), * host (64), fac.prio (20) to the message */ /* (growth by: 15 + 64 + 20 + delims = ~110) */ char printbuf[MAX_READ*2 + 128]; @@ -832,12 +840,24 @@ static void timestamp_and_log(int pri, char *msg, int len) msg += 16; } +#if ENABLE_FEATURE_SYSLOGD_PRECISE_TIMESTAMPS + if (!timestamp) { + struct timeval tv; + gettimeofday(&tv, NULL); + now = tv.tv_sec; + timestamp = ctime(&now) + 4; /* skip day of week */ + /* overwrite year by milliseconds, zero terminate */ + sprintf(timestamp + 15, ".%03u", (unsigned)tv.tv_usec / 1000u); + } else { + timestamp[15] = '\0'; + } +#else if (!timestamp) { time(&now); timestamp = ctime(&now) + 4; /* skip day of week */ } - timestamp[15] = '\0'; +#endif if (option_mask32 & OPT_kmsg) { log_to_kmsg(pri, msg);