1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-22 19:19:47 +01:00

usyslogd: merge log files for the same service

If a service provides an identifier string, write all messages for that
service itno a file named after that identifier instread of splitting it
up by facility ID in a sub directory.

This is supposed to cleanup and simplify the clutter created in /var/log.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-08-19 10:16:31 +02:00
parent f51dca0878
commit 61bc850984

View file

@ -45,30 +45,30 @@ static const enum_map_t levels[] = {
}; };
static const enum_map_t facilities[] = { static const enum_map_t facilities[] = {
{ "kernel.log", 0 }, { "kernel", 0 },
{ "user.log", 1 }, { "user", 1 },
{ "mail.log", 2 }, { "mail", 2 },
{ "daemon.log", 3 }, { "daemon", 3 },
{ "auth.log", 4 }, { "auth", 4 },
{ "syslog.log", 5 }, { "syslog", 5 },
{ "lpr.log", 6 }, { "lpr", 6 },
{ "news.log", 7 }, { "news", 7 },
{ "uucp.log", 8 }, { "uucp", 8 },
{ "clock.log", 9 }, { "clock", 9 },
{ "authpriv.log", 10 }, { "authpriv", 10 },
{ "ftp.log", 11 }, { "ftp", 11 },
{ "ntp.log", 12 }, { "ntp", 12 },
{ "audit.log", 13 }, { "audit", 13 },
{ "alert.log", 14 }, { "alert", 14 },
{ "cron.log", 15 }, { "cron", 15 },
{ "local0.log", 16 }, { "local0", 16 },
{ "local1.log", 17 }, { "local1", 17 },
{ "local2.log", 18 }, { "local2", 18 },
{ "local3.log", 19 }, { "local3", 19 },
{ "local4.log", 20 }, { "local4", 20 },
{ "local5.log", 21 }, { "local5", 21 },
{ "local6.log", 22 }, { "local6", 22 },
{ "local7.log", 23 }, { "local7", 23 },
{ NULL, 0 }, { NULL, 0 },
}; };
@ -135,7 +135,7 @@ static logfile_t *logfile_create(const char *filename)
static int logfile_write(logfile_t *file, const syslog_msg_t *msg) static int logfile_write(logfile_t *file, const syslog_msg_t *msg)
{ {
const char *lvl_str; const char *lvl_str, *fac_name;
char timebuf[32]; char timebuf[32];
struct tm tm; struct tm tm;
int ret; int ret;
@ -150,8 +150,17 @@ static int logfile_write(logfile_t *file, const syslog_msg_t *msg)
gmtime_r(&msg->timestamp, &tm); gmtime_r(&msg->timestamp, &tm);
strftime(timebuf, sizeof(timebuf), "%FT%T", &tm); strftime(timebuf, sizeof(timebuf), "%FT%T", &tm);
ret = dprintf(file->fd, "[%s][%s][%u] %s", timebuf, lvl_str, msg->pid, if (msg->ident != NULL) {
msg->message); fac_name = enum_to_name(facilities, msg->facility);
if (fac_name == NULL)
return -1;
ret = dprintf(file->fd, "[%s][%s][%s][%u] %s", timebuf,
fac_name, lvl_str, msg->pid, msg->message);
} else {
ret = dprintf(file->fd, "[%s][%s][%u] %s", timebuf, lvl_str,
msg->pid, msg->message);
}
fsync(file->fd); fsync(file->fd);
@ -229,35 +238,30 @@ static void file_backend_cleanup(log_backend_t *backend)
static int file_backend_write(log_backend_t *backend, const syslog_msg_t *msg) static int file_backend_write(log_backend_t *backend, const syslog_msg_t *msg)
{ {
log_backend_file_t *log = (log_backend_file_t *)backend; log_backend_file_t *log = (log_backend_file_t *)backend;
const char *fac_name; const char *ident;
char *filename; char *filename;
logfile_t *f; logfile_t *f;
size_t len; size_t len;
fac_name = enum_to_name(facilities, msg->facility); if (msg->ident != NULL) {
if (fac_name == NULL) ident = msg->ident;
return -1;
if (msg->ident) {
len = strlen(msg->ident) + 1 + strlen(fac_name) + 1;
filename = alloca(len);
sprintf(filename, "%s/%s", msg->ident, fac_name);
} else { } else {
filename = (char *)fac_name; ident = enum_to_name(facilities, msg->facility);
if (ident == NULL)
return -1;
} }
len = strlen(ident) + strlen(".log") + 1;
filename = alloca(len);
strcpy(filename, ident);
strcat(filename, ".log");
for (f = log->list; f != NULL; f = f->next) { for (f = log->list; f != NULL; f = f->next) {
if (strcmp(filename, f->filename) == 0) if (strcmp(filename, f->filename) == 0)
break; break;
} }
if (f == NULL) { if (f == NULL) {
if (msg->ident != NULL && mkdir(msg->ident, 0750) != 0 &&
errno != EEXIST) {
perror(msg->ident);
return -1;
}
f = logfile_create(filename); f = logfile_create(filename);
if (f == NULL) if (f == NULL)
return -1; return -1;