Skip to content

Commit

Permalink
logger: always update header when read from stdin
Browse files Browse the repository at this point in the history
The current code updates the header only when the priority has been
changed. It's incorrect because wanted is a valid header or each entry
(don't forget that logger for stdin use-case is used in pipe to log
long-time running processes).

This patch also fixes the initial timestamp; it was originally generated
on logger startup, it now generates the header on the first message.

$ (sleep 2; date; sleep 2; date; sleep 2; date) | logger --stderr --no-act

old:
<13>Nov  1 10:42:14 kzak: Tue Nov  1 10:42:16 AM CET 2022
<13>Nov  1 10:42:14 kzak: Tue Nov  1 10:42:18 AM CET 2022
<13>Nov  1 10:42:14 kzak: Tue Nov  1 10:42:20 AM CET 2022

new:
<13>Nov  1 10:19:02 kzak: Tue Nov  1 10:19:02 AM CET 2022
<13>Nov  1 10:19:04 kzak: Tue Nov  1 10:19:04 AM CET 2022
<13>Nov  1 10:19:06 kzak: Tue Nov  1 10:19:06 AM CET 2022

Fixes: #1866
Signed-off-by: Karel Zak <kzak@redhat.com>
  • Loading branch information
karelzak committed Nov 1, 2022
1 parent 85dd05d commit 96ccdc0
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions misc-utils/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,8 +945,6 @@ static void logger_open(struct logger_ctl *ctl)
ctl->tag = ctl->login = xgetlogin();
if (!ctl->tag)
ctl->tag = "<someone>";

generate_syslog_header(ctl);
}

/* re-open; usually after failed connection */
Expand Down Expand Up @@ -996,10 +994,8 @@ static void logger_stdin(struct logger_ctl *ctl)
{
/* note: we re-generate the syslog header for each log message to
* update header timestamps and to reflect possible priority changes.
* The initial header is generated by logger_open().
*/
int default_priority = ctl->pri;
int last_pri = default_priority;
char *buf = xmalloc(ctl->max_message_size + 2 + 2);
int pri;
int c;
Expand All @@ -1026,10 +1022,6 @@ static void logger_stdin(struct logger_ctl *ctl)
} else
ctl->pri = default_priority;

if (ctl->pri != last_pri) {
generate_syslog_header(ctl);
last_pri = ctl->pri;
}
if (c != EOF && c != '\n')
c = getchar();
}
Expand All @@ -1040,8 +1032,10 @@ static void logger_stdin(struct logger_ctl *ctl)
}
buf[i] = '\0';

if (i > 0 || !ctl->skip_empty_lines)
if (i > 0 || !ctl->skip_empty_lines) {
generate_syslog_header(ctl);
write_output(ctl, buf);
}

if (c == '\n') /* discard line terminator */
c = getchar();
Expand Down Expand Up @@ -1317,12 +1311,14 @@ int main(int argc, char **argv)
abort();
}
logger_open(&ctl);
if (0 < argc)
if (0 < argc) {
generate_syslog_header(&ctl);
logger_command_line(&ctl, argv);
else
} else
/* Note. --file <arg> reopens stdin making the below
* function to be used for file inputs. */
logger_stdin(&ctl);

logger_close(&ctl);
return EXIT_SUCCESS;
}

0 comments on commit 96ccdc0

Please sign in to comment.