On 20/03/12 15:55, ra...@gmx.de wrote:
> I'm working on a patch to make the busybox syslogd's log-rotation
> facility produce rotated logfiles with stable filename, i.e.: rather
> than appending a series-number to the filename and rotating the
> numbers, I append a *timestamp* to the name--and the name of a
> rotated logfile then never needs to change.

IMHO it is a great idea. I would like it.

Patch attached.

I think my simple-minded glob-sorting logic is not entirely kosher--
it's just doing an alphabetic sort, so it doesn't DTRT when migrating
from a different backlog-naming scheme; I'm not quite sure what the
most appropriate fix is for that is. Suggestions?

--
"Don't be afraid to ask (λf.((λx.xx) (λr.f(rr))))."
diff --git a/sysklogd/Config.src b/sysklogd/Config.src
index b7a494e..99c22e9 100644
--- a/sysklogd/Config.src
+++ b/sysklogd/Config.src
@@ -30,6 +30,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_ROTATE_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 fc380d9..a50905d 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -69,6 +69,12 @@
 #include <sys/shm.h>
 #endif
 
+#if ENABLE_FEATURE_ROTATE_TIMESTAMPS
+#include <glob.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#endif
 
 #define DEBUG 0
 
@@ -580,9 +586,39 @@ 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_ROTATE_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_ROTATE_TIMESTAMPS
+			char globpat[oldname_len + 2 + 1]; /* ".*\000" */
+			glob_t globres;
+
+			struct stat statbuf;
+			struct tm *ltmp;
+
+			sprintf(globpat, "%s.*", 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);
+			rename(log_file->path, newFile); /* ignore errors - file might be missing */
+#else
 			i = G.logFileRotate - 1;
 			/* rename: f.8 -> f.9; f.7 -> f.8; ... */
 			while (1) {
@@ -594,6 +630,7 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file)
 			}
 			/* newFile == "f.0" now */
 			rename(log_file->path, newFile);
+#endif
 			/* Incredibly, if F and F.0 are hardlinks, POSIX
 			 * _demands_ that rename returns 0 but does not
 			 * remove F!!!
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to