aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Korsgaard <peter@korsgaard.com>2020-01-27 15:36:41 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2020-01-29 13:56:36 +0100
commiteb7f9acda147543079f261753a11d2afa340fc5e (patch)
treecc9aefc903421664a3261a8c3d7fd6c405e8c414
parent020abc8856f94d6e355f4daa972ac75fb05ae113 (diff)
downloadbusybox-w32-eb7f9acda147543079f261753a11d2afa340fc5e.tar.gz
busybox-w32-eb7f9acda147543079f261753a11d2afa340fc5e.tar.bz2
busybox-w32-eb7f9acda147543079f261753a11d2afa340fc5e.zip
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>
-rw-r--r--sysklogd/syslogd.c24
1 files 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 @@
64//config: help 64//config: help
65//config: Supports restricted syslogd config. See docs/syslog.conf.txt 65//config: Supports restricted syslogd config. See docs/syslog.conf.txt
66//config: 66//config:
67//config:config FEATURE_SYSLOGD_PRECISE_TIMESTAMPS
68//config: bool "Include milliseconds in timestamps"
69//config: default n
70//config: depends on SYSLOGD
71//config: help
72//config: Includes milliseconds (HH:MM:SS.mmm) in timestamp when
73//config: timestamps are added.
74//config:
67//config:config FEATURE_SYSLOGD_READ_BUFFER_SIZE 75//config:config FEATURE_SYSLOGD_READ_BUFFER_SIZE
68//config: int "Read buffer size in bytes" 76//config: int "Read buffer size in bytes"
69//config: default 256 77//config: default 256
@@ -276,7 +284,7 @@ struct globals {
276 /* ...then copy to parsebuf, escaping control chars */ 284 /* ...then copy to parsebuf, escaping control chars */
277 /* (can grow x2 max) */ 285 /* (can grow x2 max) */
278 char parsebuf[MAX_READ*2]; 286 char parsebuf[MAX_READ*2];
279 /* ...then sprintf into printbuf, adding timestamp (15 chars), 287 /* ...then sprintf into printbuf, adding timestamp (15 or 19 chars),
280 * host (64), fac.prio (20) to the message */ 288 * host (64), fac.prio (20) to the message */
281 /* (growth by: 15 + 64 + 20 + delims = ~110) */ 289 /* (growth by: 15 + 64 + 20 + delims = ~110) */
282 char printbuf[MAX_READ*2 + 128]; 290 char printbuf[MAX_READ*2 + 128];
@@ -832,12 +840,24 @@ static void timestamp_and_log(int pri, char *msg, int len)
832 msg += 16; 840 msg += 16;
833 } 841 }
834 842
843#if ENABLE_FEATURE_SYSLOGD_PRECISE_TIMESTAMPS
844 if (!timestamp) {
845 struct timeval tv;
846 gettimeofday(&tv, NULL);
847 now = tv.tv_sec;
848 timestamp = ctime(&now) + 4; /* skip day of week */
849 /* overwrite year by milliseconds, zero terminate */
850 sprintf(timestamp + 15, ".%03u", (unsigned)tv.tv_usec / 1000u);
851 } else {
852 timestamp[15] = '\0';
853 }
854#else
835 if (!timestamp) { 855 if (!timestamp) {
836 time(&now); 856 time(&now);
837 timestamp = ctime(&now) + 4; /* skip day of week */ 857 timestamp = ctime(&now) + 4; /* skip day of week */
838 } 858 }
839
840 timestamp[15] = '\0'; 859 timestamp[15] = '\0';
860#endif
841 861
842 if (option_mask32 & OPT_kmsg) { 862 if (option_mask32 & OPT_kmsg) {
843 log_to_kmsg(pri, msg); 863 log_to_kmsg(pri, msg);