From 7521af58c4e959c16e3cdf1fd7fe334040d3fffe Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Sun, 5 May 2024 22:32:48 -0600 Subject: [PATCH] syslog_daemon: add syslog_max_history option This driver setting in ~/config/settings/kernel/drivers/kernel allows setting an arbitrary number of syslog files to keep around. The files are now named with a numeric suffix instead of being called "syslog.old". The old files are rotated so that syslog.1 is the newest, then syslog.2, and so on. Change-Id: I7e3d01caa34680553b161ceb27ab8c3b2eaf508c Reviewed-on: https://review.haiku-os.org/c/haiku/+/9055 Reviewed-by: waddlesplash Tested-by: Commit checker robot --- data/settings/kernel/drivers/kernel | 6 ++++ src/servers/syslog_daemon/syslog_output.cpp | 31 +++++++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/data/settings/kernel/drivers/kernel b/data/settings/kernel/drivers/kernel index 7078cc91d4..99aa79e176 100644 --- a/data/settings/kernel/drivers/kernel +++ b/data/settings/kernel/drivers/kernel @@ -52,6 +52,12 @@ load_symbols true #syslog_time_stamps true # Include time stamps in syslog debug output, defaults to false. +#syslog_max_history 7 + # Maximum number of old syslogs to keep. The files are named + # syslog.1, syslog.2, syslog.3,... with syslog.1 being the newest. + # Defaults to 1, using 0 will disable this and syslog will be recreated + # when it reaches syslog_max_size. + #syslog_max_size 20MB # Sets the maximum syslog file size, default is 512kB. diff --git a/src/servers/syslog_daemon/syslog_output.cpp b/src/servers/syslog_daemon/syslog_output.cpp index 0678ee2993..9abf1a8c39 100644 --- a/src/servers/syslog_daemon/syslog_output.cpp +++ b/src/servers/syslog_daemon/syslog_output.cpp @@ -34,6 +34,7 @@ static char sLastMessage[1024]; static thread_id sLastThread; static int32 sRepeatCount; static size_t sLogMaxSize = 524288; // 512kB +static int32 sMaxHistory = 1; static bool sLogTimeStamps = false; @@ -69,14 +70,25 @@ prepare_output() BPath syslog(base); syslog.Append("syslog"); - // move old file if it already exists + // move old files if they already exist if (tooLarge) { - BPath oldlog(base); - oldlog.Append("syslog.old"); - - remove(oldlog.Path()); - rename(syslog.Path(), oldlog.Path()); + // remove latest syslog.X and rename others with a suffix incremented by one + // syslog.6 -> syslog.7, syslog.5 -> syslog.6, ... + for (int32 x = sMaxHistory; x >= 0; x--) { + BString oldlog(syslog.Path()); + // no suffix on 0, just 'syslog' + if (x > 0) + oldlog << "." << x; + if (x == sMaxHistory) + remove(oldlog.String()); + else { + // increment our suffix + BString rotateTo(syslog.Path()); + rotateTo << "." << (x + 1); + rename(oldlog.String(), rotateTo.String()); + } + } // ToDo: just remove old file if space on device is tight? } @@ -215,8 +227,11 @@ init_syslog_output(SyslogDaemon *daemon) if (handle != NULL) { sLogTimeStamps = get_driver_boolean_parameter(handle, "syslog_time_stamps", false, false); - const char *param = get_driver_parameter(handle, - "syslog_max_size", "0", "0"); + const char *param = get_driver_parameter(handle, "syslog_max_history", "1", "1"); + sMaxHistory = strtol(param, NULL, 0); + if (sMaxHistory < 0) + sMaxHistory = 0; + param = get_driver_parameter(handle, "syslog_max_size", "0", "0"); int maxSize = strtol(param, NULL, 0); if (strchr(param, 'k') || strchr(param, 'K')) maxSize *= 1024;