]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
logger: add --skip-empty to prevent logging empty lines
authorRainer Gerhards <rgerhards@adiscon.com>
Sat, 7 Mar 2015 11:14:21 +0000 (12:14 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 12 Mar 2015 09:21:15 +0000 (10:21 +0100)
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>
bash-completion/logger
misc-utils/logger.1
misc-utils/logger.c

index 593a67824f82438d2a7e6b24777affa1d7297469..dcbfa9b62a2237f2cac5dab878ca24ffe38823f2 100644 (file)
@@ -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
                        ;;
index 164c5f49c1ee82c9ba4e9d6cbc86561e823bc4f4..d0293a801b0209ec7fb93b5a332379efbf0ad04b 100644 (file)
@@ -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.
index 575d111e684a2d15f97f92ce56714a7100116c81..0a9e4ab57d6abd535f364c7c0006cb42f31054eb 100644 (file)
@@ -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;