[PATCH] syslogd: adds milliseconds to printed timestamps

Christophe Leroy christophe.leroy at c-s.fr
Thu Jan 11 17:04:42 UTC 2018


This patchs adds a FEATURE to allow printing milliseconds in
addition in the timestamps generated by syslogd

Signed-off-by: Christophe Leroy <christophe.leroy at c-s.fr>
---
 sysklogd/syslogd.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 4265f4f90..6b58d668b 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -88,6 +88,13 @@
 //config:	entire filesystem, which may cause your system to
 //config:	break badly.
 //config:
+//config:config FEATURE_SYSLOGD_MS
+//config:	bool "Include milliseconds in time"
+//config:	default n
+//config:	depends on SYSLOGD
+//config:	help
+//config:	  This adds milliseconds to printed timestamps
+//config:
 //config:config FEATURE_IPC_SYSLOG_BUFFER_SIZE
 //config:	int "Circular buffer size in Kbytes (minimum 4KB)"
 //config:	default 16
@@ -814,17 +821,30 @@ static void parse_fac_prio_20(int pri, char *res20)
 static void timestamp_and_log(int pri, char *msg, int len)
 {
 	char *timestamp;
+#if ENABLE_FEATURE_SYSLOGD_MS
+	struct timeval now;
+#else
 	time_t now;
+#endif
 
 	/* Jan 18 00:11:22 msg... */
 	/* 01234567890123456 */
 	if (len < 16 || msg[3] != ' ' || msg[6] != ' '
 	 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
 	) {
+#if ENABLE_FEATURE_SYSLOGD_MS
+		gettimeofday(&now, NULL);
+		timestamp = ctime(&now.tv_sec) + 4; /* skip day of week */
+#else
 		time(&now);
 		timestamp = ctime(&now) + 4; /* skip day of week */
+#endif
 	} else {
+#if ENABLE_FEATURE_SYSLOGD_MS
+		now.tv_sec = 0;
+#else
 		now = 0;
+#endif
 		timestamp = msg;
 		msg += 16;
 	}
@@ -836,11 +856,19 @@ static void timestamp_and_log(int pri, char *msg, int len)
 	}
 
 	if (option_mask32 & OPT_small)
+#if ENABLE_FEATURE_SYSLOGD_MS
+		sprintf(G.printbuf, "%s.%3.3ld %s\n", timestamp, now.tv_usec / 1000, msg);
+#else
 		sprintf(G.printbuf, "%s %s\n", timestamp, msg);
+#endif
 	else {
 		char res[20];
 		parse_fac_prio_20(pri, res);
+#if ENABLE_FEATURE_SYSLOGD_MS
+		sprintf(G.printbuf, "%s.%3.3ld %.64s %s %s\n", timestamp, now.tv_usec / 1000, G.hostname, res, msg);
+#else
 		sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
+#endif
 	}
 
 	/* Log message locally (to file or shared mem) */
@@ -868,7 +896,11 @@ static void timestamp_and_log(int pri, char *msg, int len)
 			return;
 		}
 #endif
+#if ENABLE_FEATURE_SYSLOGD_MS
+		log_locally(now.tv_sec, G.printbuf, &G.logFile);
+#else
 		log_locally(now, G.printbuf, &G.logFile);
+#endif
 	}
 }
 
-- 
2.13.3



More information about the busybox mailing list