[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] syslogd: allow use of timestamps on rotated logfile-names rather than series-numbers
From:       Joshua Judson Rosen <jrosen () harvestai ! com>
Date:       2014-05-20 5:07:10
Message-ID: 1400562430-16490-1-git-send-email-jrosen () harvestai ! com
[Download RAW message or body]

Configurable at build time.

Timestamp-based names are stable, which can be invaluable
if someone needs to process logs on a running system;
e.g.: creating a tarball of logs when a rotation might happen at any moment.

Signed-off-by: Joshua Judson Rosen <jrosen@harvestai.com>
---
 sysklogd/Config.src |    8 ++++++++
 sysklogd/syslogd.c  |   42 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/sysklogd/Config.src b/sysklogd/Config.src
index 4ca0c02..fd8d408 100644
--- a/sysklogd/Config.src
+++ b/sysklogd/Config.src
@@ -41,6 +41,14 @@ config FEATURE_ROTATE_LOGFILE
 	  This enables syslogd to rotate the message files
 	  on his own. No need to use an external rotatescript.
 
+config FEATURE_LOGROTATE_TIMESTAMPS
+	bool "Suffix rotated logfile names with timestamps"
+	default n
+	depends on FEATURE_ROTATE_LOGFILE
+	help
+	  This makes syslogd append timestamps rather than
+          series-numbers to the names of rotated logfiles.
+
 config FEATURE_REMOTE_LOG
 	bool "Remote Log support"
 	default y
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index d79f2fe..d9cf289 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -75,6 +75,12 @@
 #include <sys/shm.h>
 #endif
 
+#if ENABLE_FEATURE_LOGROTATE_TIMESTAMPS
+#include <glob.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#endif
 
 #define DEBUG 0
 
@@ -634,9 +640,42 @@ static void log_locally(time_t now, char *msg, logFile_t \
*log_file)  #if ENABLE_FEATURE_ROTATE_LOGFILE
 	if (G.logFileSize && log_file->isRegular && log_file->size > G.logFileSize) {
 		if (G.logFileRotate) { /* always 0..99 */
-			int i = strlen(log_file->path) + 3 + 1;
+			int oldname_len = strlen(log_file->path);
+#if ENABLE_FEATURE_LOGROTATE_TIMESTAMPS
+			int i = oldname_len + 1 + 4 + 2 + 2 + 2 + 2 + 2    + 1;
+			                   /* .   Y   m   d   H   M   S */
+#else
+			int i = oldname_len + 3 + 1;
 			char oldFile[i];
+#endif
 			char newFile[i];
+#if ENABLE_FEATURE_LOGROTATE_TIMESTAMPS
+			char globpat[oldname_len + 1 + 5*14 + 1]; /* .YYYYmmddHHMMSS */
+			glob_t globres;
+
+			struct stat statbuf;
+			struct tm *ltmp;
+
+			/* FIXME: this glob doesn't DTRT for old series-stamped
+			   "^*.[[:digit:]]$" logfiles; may want to use scandir
+			   to control sorting:
+			*/
+			sprintf(globpat, \
"%s.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]", \
log_file->path); +			glob(globpat, 0, NULL, &globres);
+
+			i = 0;
+			while ((G.logFileRotate + i) <= globres.gl_pathc) {
+				unlink(globres.gl_pathv[i++]);
+			}
+
+			globfree(&globres);
+
+			stat(log_file->path, &statbuf);
+			ltmp = localtime(&statbuf.st_mtime);
+			sprintf(newFile, "%s.", log_file->path);
+			strftime(newFile + (oldname_len+1), sizeof(newFile) - (oldname_len+1),
+			        "%Y%m%d%H%M%S", ltmp);
+#else
 			i = G.logFileRotate - 1;
 			/* rename: f.8 -> f.9; f.7 -> f.8; ... */
 			while (1) {
@@ -647,6 +686,7 @@ static void log_locally(time_t now, char *msg, logFile_t \
*log_file)  rename(oldFile, newFile);
 			}
 			/* newFile == "f.0" now */
+#endif
 			rename(log_file->path, newFile);
 		}
 
-- 
1.7.10.4

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic