diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2015-03-07 12:14:21 +0100 |
---|---|---|
committer | Karel Zak <kzak@redhat.com> | 2015-03-12 10:21:15 +0100 |
commit | 82f658c68ca6cb67bdf7bf195b96f9664e0a3a78 (patch) | |
tree | 22fc2737313af3e1e30b9da5dd19f1f7fabb13e9 | |
parent | e0610aa83114c1521e52dd099d3180d14dcff34e (diff) | |
download | util-linux-82f658c68ca6cb67bdf7bf195b96f9664e0a3a78.tar.gz |
logger: add --skip-empty to prevent logging empty lines
Empty log messages are generally considered useless. This option
enables to turn them off when processing files (including stdin).
[kzak@redhat.com: - rename --skip-empty-lines to --skip-empty,
- add the option to getopt_long(),
- add the option to bash-completion]
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r-- | bash-completion/logger | 2 | ||||
-rw-r--r-- | misc-utils/logger.1 | 8 | ||||
-rw-r--r-- | misc-utils/logger.c | 14 |
3 files changed, 20 insertions, 4 deletions
diff --git a/bash-completion/logger b/bash-completion/logger index 593a67824..dcbfa9b62 100644 --- a/bash-completion/logger +++ b/bash-completion/logger @@ -37,7 +37,7 @@ _logger_module() esac case $cur in -*) - OPTS="--journald --udp --id --file --help --server --port --priority --rfc3164 --rfc5424 --stderr --tag --size --socket --version" + OPTS="--journald --udp --id --file --help --server --skip-empty --port --priority --rfc3164 --rfc5424 --stderr --tag --size --socket --version" COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) return 0 ;; diff --git a/misc-utils/logger.1 b/misc-utils/logger.1 index 164c5f49c..d0293a801 100644 --- a/misc-utils/logger.1 +++ b/misc-utils/logger.1 @@ -51,6 +51,14 @@ given either, then standard input is logged. Use datagrams (UDP) only. By default the connection is tried to the syslog port defined in /etc/services, which is often 514 . .TP +.BR \-e , " \-\-skip-empty" +When processing files, empty lines will be ignored. An empty line +is defined to be a line without any characters. Thus a line consisting +only of whitespace is NOT considered empty. +Note that when the \fR\-\-prio\-prefix\fR option is specified, the priority +is not part of the line. Thus an empty line in this mode is a line that does +not have any characters after the priority (e.g. "<13>"). +.TP .BR \-f , " \-\-file " \fIfile Log the contents of the specified \fIfile\fR. This option cannot be combined with a command-line message. diff --git a/misc-utils/logger.c b/misc-utils/logger.c index 575d111e6..0a9e4ab57 100644 --- a/misc-utils/logger.c +++ b/misc-utils/logger.c @@ -111,7 +111,8 @@ struct logger_ctl { stderr_printout:1, /* output message to stderr */ rfc5424_time:1, /* include time stamp */ rfc5424_tq:1, /* include time quality markup */ - rfc5424_host:1; /* include hostname */ + rfc5424_host:1, /* include hostname */ + skip_empty_lines:1; /* do not send empty lines when processing files */ }; static int decode(const char *name, CODE *codetab) @@ -574,7 +575,8 @@ static void logger_stdin(struct logger_ctl *ctl) } buf[i] = '\0'; - write_output(ctl, buf); + if(i > 0 || !ctl->skip_empty_lines) + write_output(ctl, buf); if (c == '\n') /* discard line terminator */ c = getchar(); @@ -600,6 +602,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out) fputs(_(" -i log the logger command's PID\n"), out); fputs(_(" --id[=<id>] log the given <id>, or otherwise the PID\n"), out); fputs(_(" -f, --file <file> log the contents of this file\n"), out); + fputs(_(" -e, --skip-empty do not log empty lines when processing files\n"), out); fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out); fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out); fputs(_(" -s, --stderr output message to standard error as well\n"), out); @@ -651,6 +654,7 @@ int main(int argc, char **argv) .rfc5424_time = 1, .rfc5424_tq = 1, .rfc5424_host = 1, + .skip_empty_lines = 0 }; int ch; int stdout_reopened = 0; @@ -676,6 +680,7 @@ int main(int argc, char **argv) { "rfc3164", no_argument, 0, OPT_RFC3164 }, { "rfc5424", optional_argument, 0, OPT_RFC5424 }, { "size", required_argument, 0, 'S' }, + { "skip-empty", no_argument, 0, 'e' }, #ifdef HAVE_LIBSYSTEMD { "journald", optional_argument, 0, OPT_JOURNALD }, #endif @@ -687,7 +692,7 @@ int main(int argc, char **argv) textdomain(PACKAGE); atexit(close_stdout); - while ((ch = getopt_long(argc, argv, "f:ip:S:st:u:dTn:P:Vh", + while ((ch = getopt_long(argc, argv, "ef:ip:S:st:u:dTn:P:Vh", longopts, NULL)) != -1) { switch (ch) { case 'f': /* file to log */ @@ -695,6 +700,9 @@ int main(int argc, char **argv) err(EXIT_FAILURE, _("file %s"), optarg); stdout_reopened = 1; break; + case 'e': + ctl.skip_empty_lines = 1; + break; case 'i': /* log process id also */ ctl.pid = getpid(); break; |