diff options
author | David Oberhollenzer <david.oberhollenzer@tele2.at> | 2018-08-16 18:20:27 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@tele2.at> | 2018-08-16 18:20:27 +0200 |
commit | 21b88d935267503a52d12c208145ccf61562c441 (patch) | |
tree | 935324dee7409da58ac09ef3852db75558a908dd | |
parent | d62ee95689e1e8c6fa4b395adfc8c0fcbca96b6f (diff) |
usyslogd: keep track of log file size
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
-rw-r--r-- | syslogd/logfile.c | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/syslogd/logfile.c b/syslogd/logfile.c index 5f0f98a..2adc136 100644 --- a/syslogd/logfile.c +++ b/syslogd/logfile.c @@ -74,6 +74,7 @@ static const enum_map_t facilities[] = { typedef struct logfile_t { struct logfile_t *next; + size_t size; int fd; char filename[]; } logfile_t; @@ -87,18 +88,26 @@ typedef struct { static int logfile_open(logfile_t *file) { + struct stat sb; + file->fd = open(file->filename, O_WRONLY | O_CREAT, 0640); if (file->fd < 0) { perror(file->filename); return -1; } - if (lseek(file->fd, 0, SEEK_END)) { - close(file->fd); - return -1; - } + if (lseek(file->fd, 0, SEEK_END)) + goto fail; + if (fstat(file->fd, &sb)) + goto fail; + + file->size = sb.st_size; return 0; +fail: + close(file->fd); + file->fd = -1; + return -1; } static logfile_t *logfile_create(const char *filename) @@ -125,6 +134,7 @@ static int logfile_write(logfile_t *file, const syslog_msg_t *msg) const char *lvl_str; char timebuf[32]; struct tm tm; + int ret; if (file->fd < 0 && logfile_open(file) != 0) return -1; @@ -136,10 +146,13 @@ static int logfile_write(logfile_t *file, const syslog_msg_t *msg) gmtime_r(&msg->timestamp, &tm); strftime(timebuf, sizeof(timebuf), "%FT%T", &tm); - dprintf(file->fd, "[%s][%s][%u] %s", timebuf, lvl_str, msg->pid, - msg->message); + ret = dprintf(file->fd, "[%s][%s][%u] %s", timebuf, lvl_str, msg->pid, + msg->message); fsync(file->fd); + + if (ret > 0) + file->size += ret; return 0; } |