[PATCH 1/1] syslogd: Add gzip feature to the rotated files
Dmitry Smirnov
dimonija at gmail.com
Thu Apr 10 13:15:16 UTC 2025
---
sysklogd/syslogd.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 2cbb22b6d..403b3971a 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -144,6 +144,7 @@
//usage: IF_FEATURE_ROTATE_LOGFILE(
//usage: "\n -s SIZE Max size (KB) before rotation (default 200KB, 0=off)"
//usage: "\n -b N N rotated logs to keep (default 1, max 99, 0=purge)"
+//usage: "\n -z Gzip rotated files (default 0, 1=gzip)"
//usage: )
//usage: "\n -l N Log only messages more urgent than prio N (1-8)"
//usage: "\n -S Smaller output"
@@ -184,7 +185,6 @@
#include <sys/shm.h>
#endif
-
#define DEBUG 0
/* MARK code is not very useful, is bloat, and broken:
@@ -246,6 +246,8 @@ IF_FEATURE_ROTATE_LOGFILE( \
unsigned logFileSize; \
/* number of rotated message files */ \
unsigned logFileRotate; \
+ /* the need to gzip rotated files */ \
+ unsigned logGzipRotated; \
) \
IF_FEATURE_IPC_SYSLOG( \
int shmid; /* ipc shared memory id */ \
@@ -301,6 +303,7 @@ static const struct init_globals init_data = {
#if ENABLE_FEATURE_ROTATE_LOGFILE
.logFileSize = 200 * 1024,
.logFileRotate = 1,
+ .logGzipRotated = 0,
#endif
#if ENABLE_FEATURE_IPC_SYSLOG
.shmid = -1,
@@ -327,6 +330,7 @@ enum {
OPTBIT_timestamp, // -t
IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
+ IF_FEATURE_ROTATE_LOGFILE(OPTBIT_gziprotated,) // -z
IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R
IF_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
IF_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
@@ -342,6 +346,7 @@ enum {
OPT_timestamp = 1 << OPTBIT_timestamp,
OPT_filesize = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
+ OPT_gziprotated = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_gziprotated)) + 0,
OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0,
OPT_locallog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
OPT_circularlog = IF_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
@@ -352,6 +357,7 @@ enum {
#define OPTION_STR "m:nO:l:St" \
IF_FEATURE_ROTATE_LOGFILE("s:" ) \
IF_FEATURE_ROTATE_LOGFILE("b:" ) \
+ IF_FEATURE_ROTATE_LOGFILE("z" ) \
IF_FEATURE_REMOTE_LOG( "R:*") \
IF_FEATURE_REMOTE_LOG( "L" ) \
IF_FEATURE_IPC_SYSLOG( "C::") \
@@ -759,17 +765,21 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file)
int i = strlen(log_file->path) + 3 + 1;
char oldFile[i];
char newFile[i];
+ const char *filename_template = (option_mask32 & OPT_gziprotated) ? "%s.%d.gz" : "%s.%d";
i = G.logFileRotate - 1;
/* rename: f.8 -> f.9; f.7 -> f.8; ... */
while (1) {
- sprintf(newFile, "%s.%d", log_file->path, i);
+ sprintf(newFile, filename_template, log_file->path, i);
if (i == 0) break;
- sprintf(oldFile, "%s.%d", log_file->path, --i);
+ sprintf(oldFile, filename_template, log_file->path, --i);
/* ignore errors - file might be missing */
rename(oldFile, newFile);
}
/* newFile == "f.0" now */
- rename(log_file->path, newFile);
+ if (option_mask32 & OPT_gziprotated) // -z
+ system(xasprintf("gzip -c %s > %s", log_file->path, newFile));
+ else
+ rename(log_file->path, newFile);
}
/* We may or may not have just renamed the file away;
--
2.49.0
More information about the busybox
mailing list