[PATCH] Add feature to gzip rotated message logs.
Danomi Manchego
danomimanchego123 at gmail.com
Wed Jun 6 04:07:39 UTC 2012
Signed-off-by: Danomi Manchego <danomimanchego123 at gmail.com>
---
sysklogd/Config.src | 7 +++++++
sysklogd/syslogd.c | 23 ++++++++++++++++++++++-
2 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/sysklogd/Config.src b/sysklogd/Config.src
index b7a494e..5ca86f4 100644
--- a/sysklogd/Config.src
+++ b/sysklogd/Config.src
@@ -30,6 +30,13 @@ config FEATURE_ROTATE_LOGFILE
This enables syslogd to rotate the message files
on his own. No need to use an external rotatescript.
+config FEATURE_GZIP_ROTATED_LOGFILE
+ bool "Gzip rotated message files"
+ default n
+ depends on GZIP && SYSLOGD
+ help
+ Compress rotated message files using gzip.
+
config FEATURE_REMOTE_LOG
bool "Remote Log support"
default y
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index fc380d9..f2a06fc 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -580,11 +580,30 @@ 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 i = strlen(log_file->path) + 3 + 3 + 1;
char oldFile[i];
char newFile[i];
i = G.logFileRotate - 1;
/* rename: f.8 -> f.9; f.7 -> f.8; ... */
+#if ENABLE_FEATURE_GZIP_ROTATED_LOGFILE
+ // gzip if gzip enabled, create patch.
+ while (1) {
+ sprintf(newFile, "%s.%d.gz", log_file->path, i);
+ if (i == 0) break;
+ sprintf(oldFile, "%s.%d.gz", log_file->path, --i);
+ /* ignore errors - file might be missing */
+ rename(oldFile, newFile);
+ }
+ /* newFile == "f.0" now */
+ sprintf(newFile, "%s.0", log_file->path);
+ rename(log_file->path, newFile);
+ char *argv[3] = {
+ "gzip",
+ };
+ argv[1] = newFile;
+ argv[2] = NULL;
+ spawn_and_wait(argv);
+#else
while (1) {
sprintf(newFile, "%s.%d", log_file->path, i);
if (i == 0) break;
@@ -602,6 +621,8 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file)
* Ensure old file is gone:
*/
unlink(log_file->path);
+#endif
+
#ifdef SYSLOGD_WRLOCK
fl.l_type = F_UNLCK;
fcntl(log_file->fd, F_SETLKW, &fl);
--
1.7.9.5
More information about the busybox
mailing list