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). /scripts/bloat-o-meter busybox_old busybox function old new delta .rodata 8353 8360 +7 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/0 up/down: 7/0) Total: 7 bytes Signed-off-by: Peter Korsgaard <pe...@korsgaard.com> --- sysklogd/syslogd.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index 0e226124a..95e213a0a 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]; @@ -833,11 +841,24 @@ static void timestamp_and_log(int pri, char *msg, int len) } if (!timestamp) { +#if ENABLE_FEATURE_SYSLOGD_PRECISE_TIMESTAMPS + struct timeval tv; + gettimeofday(&tv, NULL); + now = tv.tv_sec; +#else time(&now); +#endif timestamp = ctime(&now) + 4; /* skip day of week */ - } - timestamp[15] = '\0'; +#if ENABLE_FEATURE_SYSLOGD_PRECISE_TIMESTAMPS + /* overwrite year by milliseconds, zero terminate */ + sprintf(timestamp + 15, ".%03ld", tv.tv_usec / 1000); +#else + timestamp[15] = '\0'; +#endif + } + else + timestamp[15] = '\0'; if (option_mask32 & OPT_kmsg) { log_to_kmsg(pri, msg); -- 2.20.1 _______________________________________________ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox