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 <[email protected]>
Tested-by: Commit checker robot <[email protected]>
This commit is contained in:
Chris Roberts
2025-03-24 21:24:21 +00:00
committed by waddlesplash
parent ce6f846b6f
commit 7521af58c4
2 changed files with 29 additions and 8 deletions
+6
View File
@@ -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.
+23 -8
View File
@@ -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;