logger: add --skip-empty-lines 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>
diff --git a/bash-completion/logger b/bash-completion/logger
index 593a678..dcbfa9b 100644
--- a/bash-completion/logger
+++ b/bash-completion/logger
@@ -37,7 +37,7 @@
 	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 164c5f4..d0293a8 100644
--- a/misc-utils/logger.1
+++ b/misc-utils/logger.1
@@ -51,6 +51,14 @@
 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 575d111..0a9e4ab 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -111,7 +111,8 @@
 			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 @@
 		}
 		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 @@
 	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 @@
 		.rfc5424_time = 1,
 		.rfc5424_tq = 1,
 		.rfc5424_host = 1,
+		.skip_empty_lines = 0
 	};
 	int ch;
 	int stdout_reopened = 0;
@@ -676,6 +680,7 @@
 		{ "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 @@
 	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 @@
 				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;