From b33425827e3b96fa5f0ee2b7892fb5782c9b7879 Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Wed, 14 Apr 2021 14:52:47 +0000 Subject: [PATCH] print the datetime when logging to stderr --- ChangeLog | 4 ++++ log.c | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 519a326..9978df4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2021-04-14 Omar Polo + + * log.c (handle_imsg_log): print the datetime when logging to stderr + 2021-04-13 Omar Polo * ex.c (launch_cgi): define TLS_VERSION, TLS_CIPHER and TLS_CIPHER_STRENGTH for CGI scripts diff --git a/log.c b/log.c index 2ff2158..d41c9e3 100644 --- a/log.c +++ b/log.c @@ -27,6 +27,7 @@ #include #include #include +#include static struct event imsgev; @@ -39,6 +40,19 @@ static imsg_handlerfn *handlers[] = { [IMSG_LOG] = handle_imsg_log, }; +static inline void +print_date(void) +{ + struct tm tminfo; + time_t t; + char buf[20]; + + time(&t); + strftime(buf, sizeof(buf), "%F %T", + localtime_r(&t, &tminfo)); + fprintf(stderr, "[%s] ", buf); +} + void fatal(const char *fmt, ...) { @@ -47,6 +61,7 @@ fatal(const char *fmt, ...) va_start(ap, fmt); if (conf.foreground) { + print_date(); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); } else @@ -249,9 +264,10 @@ handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen) msg = imsg->data; msg[datalen-1] = '\0'; - if (conf.foreground) + if (conf.foreground) { + print_date(); fprintf(stderr, "%s\n", msg); - else + } else syslog(LOG_DAEMON, "%s", msg); }