Skip to content

Commit

Permalink
d[sr]log: Add --skip-empty/-e command line option
Browse files Browse the repository at this point in the history
Add a new --skip-empty/-e command line option which can be used to
discard empty input lines. By default empty input lines are copied
over to the output log.
  • Loading branch information
aperezdc committed Feb 23, 2021
1 parent f2df4ba commit 9e5aad6
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 34 deletions.
22 changes: 13 additions & 9 deletions dlog.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" Man page generated from reStructeredText.
.\" Man page generated from reStructuredText.
.
.TH DLOG 8 "" "" ""
.SH NAME
Expand Down Expand Up @@ -44,31 +44,36 @@ output. The latter may be useful to add timestamps in shell pipelines.
Command line options:
.INDENT 0.0
.TP
.BI \-p \ TEXT, \ \-\-prefix \ TEXT
.BI \-p \ TEXT\fR,\fB \ \-\-prefix \ TEXT
Insert the given text as prefix for each logged message. If
adding timestamps is enabled, the text is inserted \fIafter\fP
the timestamp, but still before the logged text.
.TP
.BI \-i \ NUMBER, \ \-\-input\-fd \ NUMBER
.BI \-i \ NUMBER\fR,\fB \ \-\-input\-fd \ NUMBER
Use file descriptor \fBNUMBER\fP to read input. By default the
standard input descriptor (number \fB0\fP) is used.
.TP
.B \-b, \-\-buffered
.B \-b\fP,\fB \-\-buffered
Buffered operation. If enabled, calls to \fIfsync(2)\fP will be
avoided. This improves performance, but may cause messages to
be lost.
.TP
.B \-t, \-\-timestamp
.B \-t\fP,\fB \-\-timestamp
Prepend a timestamp to each saved line. By default
timestamps are disabled. Timestamp format is
\fBYYYY\-mm\-dd/HH:MM:SS\fP.
\fBYYYY\-mm\-dd/HH:MM:SS\fP\&.
.TP
.B \-h, \-\-help
.B \-e\fP,\fB \-\-skip\-empty
Ignore empty input lines. An empty line is one that does not
contain any characters; a line which contains whitespace is
\fBnot\fP considered empty.
.TP
.B \-h\fP,\fB \-\-help
Show a summary of available options.
.UNINDENT
.sp
Albeit it can be used stan\-alone, most of the time you will be running
\fBdlog\fP under a process control tool like \fIdmon(8)\fP or \fIsupervise(8)\fP.
\fBdlog\fP under a process control tool like \fIdmon(8)\fP or \fIsupervise(8)\fP\&.
.SH ENVIRONMENT
.sp
Additional options will be picked from the \fBDLOG_OPTIONS\fP environment
Expand All @@ -83,5 +88,4 @@ given in the command line, so they may still be overriden.
.SH AUTHOR
Adrian Perez <[email protected]>
.\" Generated by docutils manpage writer.
.\"
.
15 changes: 9 additions & 6 deletions dlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
#endif /* !TSTAMP_LEN */


static bool timestamp = false;
static bool buffered = false;
static char *prefix = NULL;
static int log_fd = -1;
static int in_fd = STDIN_FILENO;
static bool timestamp = false;
static bool skip_empty = false;
static bool buffered = false;
static char *prefix = NULL;
static int log_fd = -1;
static int in_fd = STDIN_FILENO;


static const struct cflag dlog_options[] = {
Expand All @@ -48,6 +49,8 @@ static const struct cflag dlog_options[] = {
"Buffered operation, do not use flush to disk after each line."),
CFLAG(bool, "timestamp", 't', &timestamp,
"Prepend a timestamp in YYYY-MM-DD/HH:MM:SS format to each line."),
CFLAG(bool, "skip-empty", 'e', &skip_empty,
"Ignore empty lines with no characters."),
CFLAG_HELP,
CFLAG_END
};
Expand Down Expand Up @@ -116,7 +119,7 @@ dlog_main (int argc, char **argv)
if (bytes < 0)
die ("%s: error reading input: %s\n", argv0, ERRSTR);

if (dbuf_size(&linebuf)) {
if (!skip_empty || dbuf_size(&linebuf) > 1) {
struct iovec iov[6];
int n_iov = 0;

Expand Down
5 changes: 5 additions & 0 deletions dlog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ Command line options:
timestamps are disabled. Timestamp format is
``YYYY-mm-dd/HH:MM:SS``.

-e, --skip-empty
Ignore empty input lines. An empty line is one that does not
contain any characters; a line which contains whitespace is
**not** considered empty.

-h, --help Show a summary of available options.

Albeit it can be used stan-alone, most of the time you will be running
Expand Down
22 changes: 13 additions & 9 deletions drlog.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" Man page generated from reStructeredText.
.\" Man page generated from reStructuredText.
.
.TH DRLOG 8 "" "" ""
.SH NAME
Expand Down Expand Up @@ -43,7 +43,7 @@ a new \fBcurrent\fP file will be opened and, if there are stored more than
a number of timestamped files (\fB\-m\fP) old ones will be deleted.
.sp
The names of the files are designed to make them appear time\-ordered in
output from commands like \fIls(1)\fP. Also, the \fBcurrent\fP file will appear at
output from commands like \fIls(1)\fP\&. Also, the \fBcurrent\fP file will appear at
the top of file listings.
.sp
If \fBdrlog\fP receives a \fITERM\fP signal, it will read and process data until
Expand All @@ -58,44 +58,48 @@ using it that way is unsupported.
Command line options:
.INDENT 0.0
.TP
.BI \-m \ NUMBER, \ \-\-max\-files \ NUMBER
.BI \-m \ NUMBER\fR,\fB \ \-\-max\-files \ NUMBER
Maximum amount of maintained log files. When \fBdrlog\fP sees
more than \fINUMBER\fP log files in the log \fIdirectory\fP it will
remove the oldest log file.
.TP
.BI \-T \ TIME, \ \-\-max\-time \ TIME
.BI \-T \ TIME\fR,\fB \ \-\-max\-time \ TIME
Maximum number of time to use a log file. Once \fBdrlog\fP spends
more than \fITIME\fP using a log file it will start writing to a new
one. Suffixes \fIm\fP (minutes), \fIh\fP (hours), \fId\fP (days), \fIw\fP (weeks),
\fIM\fP (months) and \fIy\fP (years) may be used after the number. If no
suffix is given, it is assummed that \fITIME\fP is in seconds.
.TP
.BI \-s \ SIZE, \ \-\-max\-size \ SIZE
.BI \-s \ SIZE\fR,\fB \ \-\-max\-size \ SIZE
Maximum size of each log file. When a log file grows over
\fISIZE\fP then \fBdrlog\fP will rotate logs and open a new one.
Suffixes \fIk\fP (kilobytes), \fIm\fP (megabytes) and \fIg\fP (gigabytes)
may be used after the number. If no suffix is given, it is
assumed that \fBSIZE\fP is in bytes.
.TP
.BI \-i \ NUMBER, \ \-\-input\-fd \ NUMBER
.BI \-i \ NUMBER\fR,\fB \ \-\-input\-fd \ NUMBER
Use file descriptor \fBNUMBER\fP to read input. By default the
standard input descriptor (number \fB0\fP) is used.
.TP
.B \-b, \-\-buffered
.B \-b\fP,\fB \-\-buffered
Buffered operation. If enabled, calls to \fIfsync(2)\fP will be
avoided. This improves performance, but may cause messages to
be lost.
.TP
.B \-t, \-\-timestamp
.B \-t\fP,\fB \-\-timestamp
Prepend a timestamp to each line. The timestamp format
is \fBYYYY\-mm\-dd/HH:MM:SS\fP, following that of rotated log files.
It is easy to parse and sort. And human\-readable, too.
.TP
.B \-e\fP,\fB \-\-skip\-empty
Ignore empty input lines. An empty line is one that does not
contain any characters; a line which contains whitespace is
\fBnot\fP considered empty.
.UNINDENT
.SH SEE ALSO
.sp
\fImultilog(8)\fP, \fIsupervise(8)\fP, \fIsvc(8)\fP, \fIdslog(8)\fP, \fIdlog(8)\fP, \fIdmon(8)\fP
.SH AUTHOR
Adrian Perez <[email protected]>
.\" Generated by docutils manpage writer.
.\"
.
5 changes: 4 additions & 1 deletion drlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static unsigned long long curtime = 0;
static unsigned long long cursize = 0;
static bool timestamp = false;
static bool buffered = false;
static bool skip_empty = false;
static int returncode = 0;
static struct dbuf line = DBUF_INIT;
static struct dbuf overflow = DBUF_INIT;
Expand Down Expand Up @@ -262,7 +263,7 @@ flush_line (void)
}
}

if (dbuf_empty (&line))
if (dbuf_empty(&line) || (skip_empty && dbuf_size(&line) == 1))
return;

char timebuf[TSTAMP_LEN+1];
Expand Down Expand Up @@ -352,6 +353,8 @@ static const struct cflag drlog_options[] = {
"Buffered operation, do not flush to disk after each line."),
CFLAG(bool, "timestamp", 't', &timestamp,
"Prepend a timestamp in YYYY-MM-DD/HH:MM:SS format to each line."),
CFLAG(bool, "skip-empty", 'e', &skip_empty,
"Ignore empty lines with no characters."),
CFLAG_HELP,
CFLAG_END
};
Expand Down
5 changes: 5 additions & 0 deletions drlog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ Command line options:
is ``YYYY-mm-dd/HH:MM:SS``, following that of rotated log files.
It is easy to parse and sort. And human-readable, too.

-e, --skip-empty
Ignore empty input lines. An empty line is one that does not
contain any characters; a line which contains whitespace is
**not** considered empty.


SEE ALSO
========
Expand Down
20 changes: 12 additions & 8 deletions dslog.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" Man page generated from reStructeredText.
.\" Man page generated from reStructuredText.
.
.TH DLOG 8 "" "" ""
.SH NAME
Expand Down Expand Up @@ -43,30 +43,35 @@ program name.
Command line options:
.INDENT 0.0
.TP
.BI \-p \ PRIORITY, \ \-\-priority \ PRIORITY
.BI \-p \ PRIORITY\fR,\fB \ \-\-priority \ PRIORITY
Priority of messages. Refer to \fIsyslog(3)\fP to see possible
values. Just pass any valid priority without the \fBLOG_\fP
prefix. Case does not matter.
.TP
.BI \-f \ FACILITY, \ \-\-facility \ FACILITY
.BI \-f \ FACILITY\fR,\fB \ \-\-facility \ FACILITY
Logging facility. Refer to \fIsyslog(3)\fP to see possible values.
Just pass any valid facility without the \fBLOG_\fP prefix. Case
does not matter.
.TP
.BI \-i \ NUMBER, \ \-\-input\-fd \ NUMBER
.BI \-i \ NUMBER\fR,\fB \ \-\-input\-fd \ NUMBER
Use file descriptor \fBNUMBER\fP to read input. By default the
standard input descriptor (number \fB0\fP) is used.
.TP
.B \-c, \-\-console
.B \-c\fP,\fB \-\-console
If a message cannot be sent to the system logger, print a copy
of it to the system console.
.TP
.B \-h, \-\-help
.B \-e\fP,\fB \-\-skip\-empty
Ignore empty input lines. An empty line is one that does not
contain any characters; a line which contains whitespace is
\fBnot\fP considered empty.
.TP
.B \-h\fP,\fB \-\-help
Show a summary of available options.
.UNINDENT
.sp
Albeit it can be used stan\-alone, most of the time you will be running
\fBdslog\fP under a process control tool like \fIdmon(8)\fP or \fIsupervise(8)\fP.
\fBdslog\fP under a process control tool like \fIdmon(8)\fP or \fIsupervise(8)\fP\&.
.SH ENVIRONMENT
.sp
Additional options will be picked from the \fBDSLOG_OPTIONS\fP environment
Expand All @@ -81,5 +86,4 @@ given in the command line, so they may still be overriden.
.SH AUTHOR
Adrian Perez <[email protected]>
.\" Generated by docutils manpage writer.
.\"
.
5 changes: 4 additions & 1 deletion dslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ dslog_main (int argc, char **argv)
int facility = name_to_facility (DEFAULT_FACILITY);
int priority = name_to_priority (DEFAULT_PRIORITY);
bool console = false;
bool skip_empty = false;
char *env_opts = NULL;
struct dbuf linebuf = DBUF_INIT;
struct dbuf overflow = DBUF_INIT;
Expand All @@ -172,6 +173,8 @@ dslog_main (int argc, char **argv)
"File descriptor to read input from (default: stdin)."),
CFLAG(bool, "console", 'c', &console,
"Log to console if sending messages to logger fails."),
CFLAG(bool, "skip-empty", 'e', &skip_empty,
"Ignore empty lines with no characters."),
CFLAG_HELP,
CFLAG_END
};
Expand Down Expand Up @@ -202,7 +205,7 @@ dslog_main (int argc, char **argv)
exit (111);
}

if (dbuf_size(&linebuf))
if (!skip_empty || dbuf_size(&linebuf) > 1)
syslog(priority, "%s", dbuf_str(&linebuf));

dbuf_clear(&linebuf);
Expand Down
5 changes: 5 additions & 0 deletions dslog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ Command line options:
If a message cannot be sent to the system logger, print a copy
of it to the system console.

-e, --skip-empty
Ignore empty input lines. An empty line is one that does not
contain any characters; a line which contains whitespace is
**not** considered empty.

-h, --help Show a summary of available options.

Albeit it can be used stan-alone, most of the time you will be running
Expand Down

0 comments on commit 9e5aad6

Please sign in to comment.