logger: add support for RFC3164 style (for remote) logging

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg 2022-07-31 10:16:01 +02:00
parent 5d98f06718
commit c8fe229cfc
3 changed files with 62 additions and 11 deletions

View File

@ -272,7 +272,7 @@ int main(int argc, char *argv[])
struct sockaddr sa;
char buf[512] = "";
while ((c = getopt(argc, argv, "46?cd:f:h:ikm:np:P:r:st:u:v")) != EOF) {
while ((c = getopt(argc, argv, "46?bcd:f:h:ikm:np:P:r:st:u:v")) != EOF) {
switch (c) {
case '4':
family = AF_INET;
@ -282,6 +282,10 @@ int main(int argc, char *argv[])
family = AF_INET6;
break;
case 'b':
log_opts |= LOG_RFC3154;
break;
case 'c':
log_opts |= LOG_CONS;
break;

View File

@ -241,6 +241,17 @@ vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
if ((pri & LOG_FACMASK) == 0)
pri |= data->log_fac;
/* Get system time, wallclock, fall back to UNIX time */
if (gettimeofday(&tv, NULL) == -1) {
tv.tv_sec = time(NULL);
tv.tv_usec = 0;
}
/* strftime() implies tzset(), localtime_r() doesn't. */
tzset();
now = (time_t) tv.tv_sec;
localtime_r(&now, &tmnow);
/* Build the message. */
p = tbuf;
tbuf_left = TBUF_LEN;
@ -253,23 +264,57 @@ vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
tbuf_left -= prlen; \
} while (/*CONSTCOND*/0)
/* Default log format is RFC5424, continues below BSD format */
if (data->log_stat & LOG_RFC3154) {
if (!(data->log_stat & LOG_NLOG)) {
prlen = snprintf(p, tbuf_left, "<%d>", pri);
DEC();
} else
prlen = 0;
prlen = strftime(dbuf, sizeof(dbuf), "%b %d %T ", &tmnow);
if (data->log_stat & (LOG_PERROR|LOG_CONS|LOG_NLOG)) {
iov[iovcnt].iov_base = dbuf;
iov[iovcnt].iov_len = strlen(dbuf);
iovcnt++;
}
if (data->log_host) {
memcpy(p, dbuf, prlen);
DEC();
}
if (data->log_hostname[0] == '\0' && gethostname(data->log_hostname,
sizeof(data->log_hostname)) == -1) {
/* can this really happen? */
data->log_hostname[0] = '-';
data->log_hostname[1] = '\0';
}
prlen = snprintf(p, tbuf_left, "%s ", data->log_hostname);
DEC();
if (data->log_tag == NULL)
data->log_tag = getprogname();
prlen = snprintf(p, tbuf_left, "%s", data->log_tag);
DEC();
if (data->log_stat & LOG_PID) {
prlen = snprintf(p, tbuf_left, "[%d]", getpid());
DEC();
}
strlcat(p, ":", tbuf_left);
prlen = 1;
DEC();
goto output;
}
if (!(data->log_stat & LOG_NLOG)) {
prlen = snprintf(p, tbuf_left, "<%d>1 ", pri);
DEC();
} else
prlen = 0;
if (gettimeofday(&tv, NULL) == -1) {
tv.tv_sec = time(NULL);
tv.tv_usec = 0;
}
{
/* strftime() implies tzset(), localtime_r() doesn't. */
tzset();
now = (time_t) tv.tv_sec;
localtime_r(&now, &tmnow);
prlen = strftime(p, tbuf_left, "%FT%T", &tmnow);
DEC();
prlen = snprintf(p, tbuf_left, ".%06ld", (long)tv.tv_usec);
@ -359,6 +404,7 @@ vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
} else
strlcat(fmt_cat, "-", FMT_LEN);
output:
if (data->log_stat & (LOG_PERROR|LOG_CONS|LOG_NLOG))
msgsdlen = strlen(fmt_cat) + 1;
else

View File

@ -190,6 +190,7 @@ CODE facilitynames[] = {
#define LOG_PTRIM 0x040 /* trim tag and pid from messages to stderr */
#define LOG_NLOG 0x080 /* don't write to the system log */
#define LOG_STDOUT 0x100 /* like nlog, for debugging syslogp() API */
#define LOG_RFC3154 0x200 /* Log to remote/ipc socket in old BSD format */
#ifndef __KERNEL__