From 1c707d59ee9f1458e4b9c1e4442fb0a81b1558b0 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Mon, 8 Nov 2021 11:21:34 +0100 Subject: [PATCH 270/325] syslogd: Add ElectroBSD output format Obtained from: ElectroBSD --- usr.sbin/syslogd/syslogd.c | 68 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c index 4b30c27e9e2d..fc6257a80edb 100644 --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -402,6 +402,7 @@ static int needdofsync = 0; /* Are any file(s) waiting to be fsynced? */ static struct pidfh *pfh; static int sigpipe[2]; /* Pipe to catch a signal during select(). */ static bool RFC3164OutputFormat = true; /* Use legacy format by default. */ +static bool ElectroBSDOutputFormat = false; static volatile sig_atomic_t MarkSet, WantDie, WantInitialize, WantReapchild; @@ -665,7 +666,10 @@ main(int argc, char *argv[]) resolve = 0; break; case 'O': - if (strcmp(optarg, "bsd") == 0 || + if (strcmp(optarg, "electrobsd") == 0) { + ElectroBSDOutputFormat = true; + RFC3164OutputFormat = false; + } else if (strcmp(optarg, "bsd") == 0 || strcmp(optarg, "rfc3164") == 0) RFC3164OutputFormat = true; else if (strcmp(optarg, "syslog") == 0 || @@ -1947,6 +1951,63 @@ fprintlog_rfc5424(struct filed *f, const char *hostname, const char *app_name, fprintlog_write(f, &il, flags); } + +/* + * Similiar to fprintlog_rfc5424() but the priority is moved + * after the app name so log messages always start with the + * timestamp and are more pleasant to read. + */ +static void +fprintlog_electrobsd(struct filed *f, const char *hostname, const char *app_name, + const char *procid, const char *msgid, const char *structured_data, + const char *msg, int flags) +{ + struct iovlist il; + suseconds_t usec; + int i; + char timebuf[33], priority_number[5]; + + iovlist_init(&il); + if (f->f_type == F_WALL) + iovlist_append(&il, "\r\n\aMessage from syslogd ...\r\n"); + if (strftime(timebuf, sizeof(timebuf), "%FT%T.______%z", + &f->f_lasttime.tm) == sizeof(timebuf) - 2) { + /* Add colon to the time zone offset, which %z doesn't do. */ + timebuf[32] = '\0'; + timebuf[31] = timebuf[30]; + timebuf[30] = timebuf[29]; + timebuf[29] = ':'; + + /* Overwrite space for microseconds with actual value. */ + usec = f->f_lasttime.usec; + for (i = 25; i >= 20; --i) { + timebuf[i] = usec % 10 + '0'; + usec /= 10; + } + iovlist_append(&il, timebuf); + } else + iovlist_append(&il, "-"); + iovlist_append(&il, " "); + iovlist_append(&il, hostname); + iovlist_append(&il, " "); + iovlist_append(&il, app_name == NULL ? "-" : app_name); + iovlist_append(&il, " "); + iovlist_append(&il, "<"); + snprintf(priority_number, sizeof(priority_number), "%d", f->f_prevpri); + iovlist_append(&il, priority_number); + iovlist_append(&il, ">1 "); + iovlist_append(&il, procid == NULL ? "-" : procid); + iovlist_append(&il, " "); + iovlist_append(&il, msgid == NULL ? "-" : msgid); + iovlist_append(&il, " "); + iovlist_append(&il, structured_data == NULL ? "-" : structured_data); + iovlist_append(&il, " "); + iovlist_append(&il, msg); + + fprintlog_write(f, &il, flags); +} + + static void fprintlog_rfc3164(struct filed *f, const char *hostname, const char *app_name, const char *procid, const char *msg, int flags) @@ -2071,7 +2132,10 @@ fprintlog_first(struct filed *f, const char *hostname, const char *app_name, return; } - if (RFC3164OutputFormat) + if (ElectroBSDOutputFormat) + fprintlog_electrobsd(f, hostname, app_name, procid, msgid, + structured_data, msg, flags); + else if (RFC3164OutputFormat) fprintlog_rfc3164(f, hostname, app_name, procid, msg, flags); else fprintlog_rfc5424(f, hostname, app_name, procid, msgid, -- 2.32.0