diff options
author | David Oberhollenzer <david.oberhollenzer@tele2.at> | 2018-08-16 22:32:34 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@tele2.at> | 2018-08-16 22:48:55 +0200 |
commit | 29e4fc5607ace15c0be03fab6930acfadda2610a (patch) | |
tree | 3b8c8068bc5afd0ee1e76c0277058f42f058f758 /syslogd/logfile.c | |
parent | 21b88d935267503a52d12c208145ccf61562c441 (diff) |
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 <david.oberhollenzer@tele2.at>
Diffstat (limited to 'syslogd/logfile.c')
-rw-r--r-- | syslogd/logfile.c | 31 |
1 files changed, 23 insertions, 8 deletions
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 = { |