[PATCH] syslogd: let log-message timestamps include the year
Denys Vlasenko
vda.linux at googlemail.com
Wed Jul 2 13:34:18 UTC 2014
On Tue, May 20, 2014 at 7:06 AM, Joshua Judson Rosen
<jrosen at harvestai.com> wrote:
> Configurable at build time.
>
> Signed-off-by: Joshua Judson Rosen <jrosen at harvestai.com>
> ---
> sysklogd/Config.src | 11 +++++++++++
> sysklogd/syslogd.c | 24 +++++++++++++++++-------
> 2 files changed, 28 insertions(+), 7 deletions(-)
>
> diff --git a/sysklogd/Config.src b/sysklogd/Config.src
> index fcf9930..4ca0c02 100644
> --- a/sysklogd/Config.src
> +++ b/sysklogd/Config.src
> @@ -22,6 +22,17 @@ config SYSLOGD
> wrong. And something almost always will go wrong if
> you wait long enough....
>
> +config FEATURE_SYSLOG_TIMESTAMP_YEAR
> + bool "Include year in syslog timestamps"
> + default n
> + help
> + Timestamps in system logs traditionally do not include
> + the year, but this can be useful for some long-running
> + systems where logs are checked infrequently.
> + Note that this causes syslogd to always generate
> + its own timestamps, overriding the timestamps
> + generated by clients when they called syslog().
> +
> config FEATURE_ROTATE_LOGFILE
> bool "Rotate message files"
> default y
> diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
> index f758510..d79f2fe 100644
> --- a/sysklogd/syslogd.c
> +++ b/sysklogd/syslogd.c
> @@ -174,9 +174,9 @@ struct globals {
> /* ...then copy to parsebuf, escaping control chars */
> /* (can grow x2 max) */
> char parsebuf[MAX_READ*2];
> - /* ...then sprintf into printbuf, adding timestamp (15 chars),
> + /* ...then sprintf into printbuf, adding timestamp (15 or 20 chars),
> * host (64), fac.prio (20) to the message */
> - /* (growth by: 15 + 64 + 20 + delims = ~110) */
> + /* (growth by: 20 + 64 + 20 + delims = ~115) */
> char printbuf[MAX_READ*2 + 128];
> };
>
> @@ -701,17 +701,27 @@ static void timestamp_and_log(int pri, char *msg, int len)
>
> /* Jan 18 00:11:22 msg... */
> /* 01234567890123456 */
> - if (len < 16 || msg[3] != ' ' || msg[6] != ' '
> - || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
> + if (len >= 16 && msg[3] == ' ' && msg[6] == ' '
> + && msg[9] == ':' && msg[12] == ':' && msg[15] == ' '
> ) {
> - time(&now);
> - timestamp = ctime(&now) + 4; /* skip day of week */
> - } else {
> now = 0;
> timestamp = msg;
> msg += 16;
> }
> +#if !CONFIG_FEATURE_SYSLOG_TIMESTAMP_YEAR
> + else {
> +#endif
> + time(&now);
> + timestamp = ctime(&now) + 4; /* skip day of week */
> +#if !CONFIG_FEATURE_SYSLOG_TIMESTAMP_YEAR
> + }
> +#endif
> +
> +#if CONFIG_FEATURE_SYSLOG_TIMESTAMP_YEAR
> + timestamp[20] = '\0';
> +#else
> timestamp[15] = '\0';
> +#endif
>
> if (option_mask32 & OPT_kmsg) {
> log_to_kmsg(pri, msg);
The "right" solution would be to petition your libc maintainers
to deprecate "Month DD hh:mm:ss" silliness and
start using sensible (and not i18n-butchered) timestamps
in syslog()-generated messages.
(My personal opinion is that yyyy-mm-dd hh:mm:ss
is much more practical, and costs only four more chars per line).
I don't know how practical is to expect libc people to hear you...
For now, maybe just a silly solution of prepending year
to every line (enable-able by an option)?
I'm not happy with the idea of repalcing users' timestamp.
It _does_ carry information.
More information about the busybox
mailing list