From 29e4fc5607ace15c0be03fab6930acfadda2610a Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 16 Aug 2018 22:32:34 +0200 Subject: usyslogd: more control over log rotate behaviour, command line processing - Add more fine grained control over how log rotation is supposed to behave - Add command line option processing to usyslogd - Expose log rotation control via command line switches - Add default values to usyslogd service for pygos use case Signed-off-by: David Oberhollenzer --- syslogd/logfile.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'syslogd/logfile.c') diff --git a/syslogd/logfile.c b/syslogd/logfile.c index 2adc136..606db8e 100644 --- a/syslogd/logfile.c +++ b/syslogd/logfile.c @@ -83,6 +83,8 @@ typedef struct logfile_t { typedef struct { log_backend_t base; logfile_t *list; + size_t maxsize; + int flags; } log_backend_file_t; @@ -156,16 +158,20 @@ static int logfile_write(logfile_t *file, const syslog_msg_t *msg) return 0; } -static int logfile_rotate(logfile_t *f) +static int logfile_rotate(logfile_t *f, int flags) { char timebuf[32]; char *filename; struct tm tm; time_t now; - now = time(NULL); - gmtime_r(&now, &tm); - strftime(timebuf, sizeof(timebuf), "%FT%T", &tm); + if (flags & LOG_ROTATE_OVERWRITE) { + strcpy(timebuf, "1"); + } else { + now = time(NULL); + gmtime_r(&now, &tm); + strftime(timebuf, sizeof(timebuf), "%FT%T", &tm); + } filename = alloca(strlen(f->filename) + strlen(timebuf) + 2); sprintf(filename, "%s.%s", f->filename, timebuf); @@ -182,9 +188,10 @@ static int logfile_rotate(logfile_t *f) /*****************************************************************************/ -static int file_backend_init(log_backend_t *log) +static int file_backend_init(log_backend_t *backend, int flags, + size_t sizelimit) { - (void)log; + log_backend_file_t *log = (log_backend_file_t *)backend; if (mkdir(SYSLOG_PATH, 0755)) { if (errno != EEXIST) { @@ -198,6 +205,8 @@ static int file_backend_init(log_backend_t *log) return -1; } + log->flags = flags; + log->maxsize = sizelimit; return 0; } @@ -254,7 +263,13 @@ static int file_backend_write(log_backend_t *backend, const syslog_msg_t *msg) log->list = f; } - return logfile_write(f, msg); + if (logfile_write(f, msg)) + return -1; + + if ((log->flags & LOG_ROTATE_SIZE_LIMIT) && f->size >= log->maxsize) + logfile_rotate(f, log->flags); + + return 0; } static void file_backend_rotate(log_backend_t *backend) @@ -263,7 +278,7 @@ static void file_backend_rotate(log_backend_t *backend) logfile_t *f; for (f = log->list; f != NULL; f = f->next) - logfile_rotate(f); + logfile_rotate(f, log->flags); } log_backend_file_t filebackend = { -- cgit v1.2.3