This patch allows syslogd to additionally rotate the log files by age and on a 
per-day basis. All settings can be configured either mutually exclusive or in 
combination.


Mit freundlichen Grüßen | With kind regards

Daniel Giritzer, MSc

Certified Professional for Software 
Architecture<https://www.certible.com/badge/c7057dd8-7007-4f04-b510-6ec200b05ac7/>



T:  +43 732 266277

E:  d.girit...@danube-dynamics.at

L:  Let’s connect on <https://www.linkedin.com/in/nwrkbiz/> 
LinkedIn<https://www.linkedin.com/in/nwrkbiz/>



[Ein Bild, das Schrift, Grafiken, Text, Screenshot enthält.  Automatisch 
generierte Beschreibung]<http://www.danube-dynamics.at/>

Danube Dynamics Embedded Solutions GmbH

Lastenstraße 38/12. OG | 4020 Linz | Austria



Registry Court: Linz

Reg. No.: FN 536539 d

VAT Reg. No.: ATU75804667

Web: www.danube-dynamics.at<http://www.danube-dynamics.at/>



[Image]<https://www.facebook.com/danubedynamics/>  [Image] 
<https://youtube.com/@danubedynamics625>   [Image] 
<https://www.instagram.com/danube_dynamics/>   [Image] 
<https://www.linkedin.com/company/danube-dynamics/>



The information contained in this message may be confidential and legally 
protected under applicable law. The message is intended solely for the 
addressee(s). If you are not the intended recipient, you are hereby notified 
that any use, forwarding, dissemination, or reproduction of this message is 
strictly prohibited and may be unlawful. If you are not the intended recipient, 
please contact the sender by return e-mail and destroy all copies of the 
original message.



See our privacy policy: https://www.danube-dynamics.at/datenschutz/
From c23901cde610e84e36b1513ecdddc56f6f59eb92 Mon Sep 17 00:00:00 2001
From: Daniel Giritzer <d.girit...@danube-dynamics.at>
Date: Mon, 15 Apr 2024 16:10:33 +0200
Subject: [PATCH 1/1] feat(syslogd): implement log rotation by log-age and by
 day

Signed-off-by: Daniel Giritzer <d.girit...@danube-dynamics.at>
---
 sysklogd/syslogd.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 6ddfd771a..8b1241d0c 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -144,6 +144,8 @@
 //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	-T N		N seconds to pass since file opening before rotation (default 0, 0=Off)"
+//usage:     "\n	-d		Rotate every day"
 //usage:	)
 //usage:     "\n	-l N		Log only messages more urgent than prio N (1-8)"
 //usage:     "\n	-S		Smaller output"
@@ -223,6 +225,7 @@ typedef struct logFile_t {
 #if ENABLE_FEATURE_ROTATE_LOGFILE
 	unsigned size;
 	uint8_t isRegular;
+	time_t file_first_open_time;
 #endif
 } logFile_t;
 
@@ -246,6 +249,8 @@ IF_FEATURE_ROTATE_LOGFILE( \
 	unsigned logFileSize;                   \
 	/* number of rotated message files */   \
 	unsigned logFileRotate;                 \
+	/* time to pass until rotation     */   \
+	unsigned logFileAge;                    \
 ) \
 IF_FEATURE_IPC_SYSLOG( \
 	int shmid; /* ipc shared memory id */   \
@@ -301,6 +306,7 @@ static const struct init_globals init_data = {
 #if ENABLE_FEATURE_ROTATE_LOGFILE
 	.logFileSize = 200 * 1024,
 	.logFileRotate = 1,
+	.logFileAge = 0,
 #endif
 #if ENABLE_FEATURE_IPC_SYSLOG
 	.shmid = -1,
@@ -327,6 +333,8 @@ enum {
 	OPTBIT_timestamp, // -t
 	IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize   ,)	// -s
 	IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt  ,)	// -b
+	IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatetime ,)	// -T
+	IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotateday  ,)	// -d
 	IF_FEATURE_REMOTE_LOG(    OPTBIT_remotelog  ,)	// -R
 	IF_FEATURE_REMOTE_LOG(    OPTBIT_locallog   ,)	// -L
 	IF_FEATURE_IPC_SYSLOG(    OPTBIT_circularlog,)	// -C
@@ -342,6 +350,8 @@ 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_rotatetime  = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatetime )) + 0,
+	OPT_rotateday   = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotateday  )) + 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 +362,8 @@ enum {
 #define OPTION_STR "m:nO:l:St" \
 	IF_FEATURE_ROTATE_LOGFILE("s:" ) \
 	IF_FEATURE_ROTATE_LOGFILE("b:" ) \
+	IF_FEATURE_ROTATE_LOGFILE("T:" ) \
+	IF_FEATURE_ROTATE_LOGFILE("d" ) \
 	IF_FEATURE_REMOTE_LOG(    "R:*") \
 	IF_FEATURE_REMOTE_LOG(    "L"  ) \
 	IF_FEATURE_IPC_SYSLOG(    "C::") \
@@ -361,11 +373,13 @@ enum {
 #define OPTION_DECL *opt_m, *opt_l \
 	IF_FEATURE_ROTATE_LOGFILE(,*opt_s) \
 	IF_FEATURE_ROTATE_LOGFILE(,*opt_b) \
+	IF_FEATURE_ROTATE_LOGFILE(,*opt_T) \
 	IF_FEATURE_IPC_SYSLOG(    ,*opt_C = NULL) \
 	IF_FEATURE_SYSLOGD_CFG(   ,*opt_f = NULL)
 #define OPTION_PARAM &opt_m, &(G.logFile.path), &opt_l \
 	IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \
 	IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \
+	IF_FEATURE_ROTATE_LOGFILE(,&opt_T) \
 	IF_FEATURE_REMOTE_LOG(    ,&remoteAddrList) \
 	IF_FEATURE_IPC_SYSLOG(    ,&opt_C) \
 	IF_FEATURE_SYSLOGD_CFG(   ,&opt_f)
@@ -694,6 +708,9 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file)
 	struct flock fl;
 #endif
 	int len = strlen(msg);
+#if ENABLE_FEATURE_ROTATE_LOGFILE
+	struct tm then_tm, now_tm;
+#endif
 
 	/* fd can't be 0 (we connect fd 0 to /dev/log socket) */
 	/* fd is 1 if "-O -" is in use */
@@ -740,6 +757,9 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file)
 				log_file->isRegular = (fstat(log_file->fd, &statf) == 0 && S_ISREG(statf.st_mode));
 				/* bug (mostly harmless): can wrap around if file > 4gb */
 				log_file->size = statf.st_size;
+				if(log_file->file_first_open_time == 0) {
+					log_file->file_first_open_time = time(NULL);
+				}
 			}
 #endif
 		}
@@ -754,7 +774,13 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file)
 #endif
 
 #if ENABLE_FEATURE_ROTATE_LOGFILE
-	if (G.logFileSize && log_file->isRegular && log_file->size > G.logFileSize) {
+	then_tm = *localtime(&log_file->file_first_open_time);
+	now_tm = *localtime(&now);
+	if (log_file->isRegular && (
+        (G.logFileSize && log_file->size > G.logFileSize) || // rotate by size
+        (G.logFileAge && now > G.logFileAge + log_file->file_first_open_time) || // rotate by age
+        ((option_mask32 & OPT_rotateday) && then_tm.tm_mday != now_tm.tm_mday) // rotate by day
+        )) {
 		if (G.logFileRotate) { /* always 0..99 */
 			int i = strlen(log_file->path) + 3 + 1;
 			char oldFile[i];
@@ -782,6 +808,7 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file)
 		 * So ensure old file is gone in any case:
 		 */
 		unlink(log_file->path);
+		log_file->file_first_open_time = 0; // will be set on open
 #ifdef SYSLOGD_WRLOCK
 		fl.l_type = F_UNLCK;
 		fcntl(log_file->fd, F_SETLKW, &fl);
@@ -1163,6 +1190,8 @@ int syslogd_main(int argc UNUSED_PARAM, char **argv)
 		G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
 	if (opts & OPT_rotatecnt) // -b
 		G.logFileRotate = xatou_range(opt_b, 0, 99);
+	if (opts & OPT_rotatetime) // -T
+		G.logFileAge = xatou_range(opt_T, 0, INT_MAX);
 #endif
 #if ENABLE_FEATURE_IPC_SYSLOG
 	if (opt_C) // -Cn
-- 
2.39.2

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

Reply via email to