syslogd: add config option to include milliseconds in timestamps
authorPeter Korsgaard <peter@korsgaard.com>
Mon, 27 Jan 2020 14:36:41 +0000 (15:36 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 29 Jan 2020 12:56:36 +0000 (13:56 +0100)
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>
sysklogd/syslogd.c

index 0e226124a00777c4c9be29541d1eecb38a03c64e..ab50f4a28e701daba3e85aed3165781ca4d66b19 100644 (file)
 //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);