[PATCH] syslogd: let log-message timestamps include the year

Joshua Judson Rosen jrosen at harvestai.com
Tue May 20 05:06:04 UTC 2014


Configurable at build time.

Signed-off-by: Joshua Judson Rosen <jrosen at harvestai.com>
---
 sysklogd/Config.src |   11 +++++++++++
 sysklogd/syslogd.c  |   24 +++++++++++++++++-------
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/sysklogd/Config.src b/sysklogd/Config.src
index fcf9930..4ca0c02 100644
--- a/sysklogd/Config.src
+++ b/sysklogd/Config.src
@@ -22,6 +22,17 @@ config SYSLOGD
 	  wrong. And something almost always will go wrong if
 	  you wait long enough....
 
+config FEATURE_SYSLOG_TIMESTAMP_YEAR
+	bool "Include year in syslog timestamps"
+	default n
+	help
+	  Timestamps in system logs traditionally do not include
+	  the year, but this can be useful for some long-running
+	  systems where logs are checked infrequently.
+	  Note that this causes syslogd to always generate
+	  its own timestamps, overriding the timestamps
+	  generated by clients when they called syslog().
+
 config FEATURE_ROTATE_LOGFILE
 	bool "Rotate message files"
 	default y
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index f758510..d79f2fe 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -174,9 +174,9 @@ 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 20 chars),
 	 * host (64), fac.prio (20) to the message */
-	/* (growth by: 15 + 64 + 20 + delims = ~110) */
+	/* (growth by: 20 + 64 + 20 + delims = ~115) */
 	char printbuf[MAX_READ*2 + 128];
 };
 
@@ -701,17 +701,27 @@ static void timestamp_and_log(int pri, char *msg, int len)
 
 	/* Jan 18 00:11:22 msg... */
 	/* 01234567890123456 */
-	if (len < 16 || msg[3] != ' ' || msg[6] != ' '
-	 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
+	if (len >= 16 && msg[3] == ' ' && msg[6] == ' '
+	 && msg[9] == ':' && msg[12] == ':' && msg[15] == ' '
 	) {
-		time(&now);
-		timestamp = ctime(&now) + 4; /* skip day of week */
-	} else {
 		now = 0;
 		timestamp = msg;
 		msg += 16;
 	}
+#if !CONFIG_FEATURE_SYSLOG_TIMESTAMP_YEAR
+	else {
+#endif
+		time(&now);
+		timestamp = ctime(&now) + 4; /* skip day of week */
+#if !CONFIG_FEATURE_SYSLOG_TIMESTAMP_YEAR
+	}
+#endif
+
+#if CONFIG_FEATURE_SYSLOG_TIMESTAMP_YEAR
+	timestamp[20] = '\0';
+#else
 	timestamp[15] = '\0';
+#endif
 
 	if (option_mask32 & OPT_kmsg) {
 		log_to_kmsg(pri, msg);
-- 
1.7.10.4



More information about the busybox mailing list