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 <peter@korsgaard.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Peter Korsgaard 2020-01-27 15:36:41 +01:00 committed by Denys Vlasenko
parent 020abc8856
commit eb7f9acda1

View File

@ -64,6 +64,14 @@
//config: help //config: help
//config: Supports restricted syslogd config. See docs/syslog.conf.txt //config: Supports restricted syslogd config. See docs/syslog.conf.txt
//config: //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:config FEATURE_SYSLOGD_READ_BUFFER_SIZE
//config: int "Read buffer size in bytes" //config: int "Read buffer size in bytes"
//config: default 256 //config: default 256
@ -276,7 +284,7 @@ struct globals {
/* ...then copy to parsebuf, escaping control chars */ /* ...then copy to parsebuf, escaping control chars */
/* (can grow x2 max) */ /* (can grow x2 max) */
char parsebuf[MAX_READ*2]; 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 */ * host (64), fac.prio (20) to the message */
/* (growth by: 15 + 64 + 20 + delims = ~110) */ /* (growth by: 15 + 64 + 20 + delims = ~110) */
char printbuf[MAX_READ*2 + 128]; char printbuf[MAX_READ*2 + 128];
@ -832,12 +840,24 @@ static void timestamp_and_log(int pri, char *msg, int len)
msg += 16; 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) { if (!timestamp) {
time(&now); time(&now);
timestamp = ctime(&now) + 4; /* skip day of week */ timestamp = ctime(&now) + 4; /* skip day of week */
} }
timestamp[15] = '\0'; timestamp[15] = '\0';
#endif
if (option_mask32 & OPT_kmsg) { if (option_mask32 & OPT_kmsg) {
log_to_kmsg(pri, msg); log_to_kmsg(pri, msg);