~aperezdc/dmon

9e5aad64d4c300b333e4bcb0292e36e0851af42f — Adrian Perez de Castro 3 years ago f2df4ba
d[sr]log: Add --skip-empty/-e command line option

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.
9 files changed, 70 insertions(+), 34 deletions(-)

M dlog.8
M dlog.c
M dlog.rst
M drlog.8
M drlog.c
M drlog.rst
M dslog.8
M dslog.c
M dslog.rst
M dlog.8 => dlog.8 +13 -9
@@ 1,4 1,4 @@
.\" Man page generated from reStructeredText.
.\" Man page generated from reStructuredText.
.
.TH DLOG 8 "" "" ""
.SH NAME


@@ 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


@@ 83,5 88,4 @@ given in the command line, so they may still be overriden.
.SH AUTHOR
Adrian Perez <aperez@igalia.com>
.\" Generated by docutils manpage writer.
.\" 
.

M dlog.c => dlog.c +9 -6
@@ 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[] = {


@@ 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
};


@@ 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;


M dlog.rst => dlog.rst +5 -0
@@ 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

M drlog.8 => drlog.8 +13 -9
@@ 1,4 1,4 @@
.\" Man page generated from reStructeredText.
.\" Man page generated from reStructuredText.
.
.TH DRLOG 8 "" "" ""
.SH NAME


@@ 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


@@ 58,38 58,43 @@ 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


@@ 97,5 102,4 @@ It is easy to parse and sort. And human\-readable, too.
.SH AUTHOR
Adrian Perez <aperez@igalia.com>
.\" Generated by docutils manpage writer.
.\" 
.

M drlog.c => drlog.c +4 -1
@@ 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;


@@ 262,7 263,7 @@ recreate_ts:
        }
    }

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

    char timebuf[TSTAMP_LEN+1];


@@ 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
};

M drlog.rst => drlog.rst +5 -0
@@ 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
========

M dslog.8 => dslog.8 +12 -8
@@ 1,4 1,4 @@
.\" Man page generated from reStructeredText.
.\" Man page generated from reStructuredText.
.
.TH DLOG 8 "" "" ""
.SH NAME


@@ 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


@@ 81,5 86,4 @@ given in the command line, so they may still be overriden.
.SH AUTHOR
Adrian Perez <aperez@igalia.com>
.\" Generated by docutils manpage writer.
.\" 
.

M dslog.c => dslog.c +4 -1
@@ 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;


@@ 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
    };


@@ 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);

M dslog.rst => dslog.rst +5 -0
@@ 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