diff options
author | Sami Kerola | 2014-08-09 01:49:46 +0200 |
---|---|---|
committer | Sami Kerola | 2014-08-15 22:52:12 +0200 |
commit | 1d57503378bdcd838365d625f6d2d0a09da9c29d (patch) | |
tree | 1c8980efb3528f767b9e03905c87a286c30b7048 /misc-utils | |
parent | fdisk: add independent debug stuff (diff) | |
download | kernel-qcow2-util-linux-1d57503378bdcd838365d625f6d2d0a09da9c29d.tar.gz kernel-qcow2-util-linux-1d57503378bdcd838365d625f6d2d0a09da9c29d.tar.xz kernel-qcow2-util-linux-1d57503378bdcd838365d625f6d2d0a09da9c29d.zip |
logger: allow use of --id=ppid when logging locally
There is no obvious way to make syslog(3) to print both pid or ppid, so
duplicate the libc syslog() to logger. Making the ppid printing work
using unix socket has side effect of local becoming capable to use both
rfc format output, which is hopefully seen as good thing. The
syslog_local() is format wise one-to-one copy with glibc syslog(3)
format.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'misc-utils')
-rw-r--r-- | misc-utils/logger.c | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/misc-utils/logger.c b/misc-utils/logger.c index 4836b8f6e..d1b93d0bd 100644 --- a/misc-utils/logger.c +++ b/misc-utils/logger.c @@ -418,7 +418,30 @@ static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg) static void syslog_local(struct logger_ctl *ctl, char *msg) { - syslog(ctl->pri, "%s", msg); + char *buf, *tag; + char time[32], pid[32]; + struct timeval tv; + struct tm *tm; + pid_t process; + int len; + + gettimeofday(&tv, NULL); + tm = localtime(&tv.tv_sec); + strftime(time, sizeof(time), "%h %e %T", tm); + + tag = ctl->tag ? ctl->tag : program_invocation_short_name; + + if ((process = get_process_id(ctl))) + snprintf(pid, sizeof(pid), "[%d]", process); + else + pid[0] = '\0'; + + len = xasprintf(&buf, "<%d>%s %s%s: %s", ctl->pri, time, tag, pid, msg); + if (write_all(ctl->fd, buf, len) < 0) + warn(_("write failed")); + if (ctl->logflags & LOG_PERROR) + fprintf(stderr, "%s\n", buf); + free(buf); } static void logger_open(struct logger_ctl *ctl) @@ -435,12 +458,7 @@ static void logger_open(struct logger_ctl *ctl) ctl->syslogfp = syslog_rfc5424; return; } - - if (ctl->syslogfp == syslog_rfc5424 || ctl->syslogfp == syslog_rfc3164) - errx(EXIT_FAILURE, _("--server or --socket are required to " - "log by --rfc5424 or --rfc3164")); - - openlog(ctl->tag ? ctl->tag : xgetlogin(), ctl->logflags, 0); + ctl->fd = unix_socket("/dev/log", ctl->socket_type); ctl->syslogfp = syslog_local; } @@ -493,10 +511,8 @@ static void logger_stdin(struct logger_ctl *ctl) static void logger_close(struct logger_ctl *ctl) { - if (!ctl->unix_socket && !ctl->server) - closelog(); - else - close(ctl->fd); + if (close(ctl->fd) != 0) + err(EXIT_FAILURE, _("close failed")); } static void __attribute__ ((__noreturn__)) usage(FILE *out) |