diff options
author | Omar Polo <op@omarpolo.com> | 2021-04-14 14:52:47 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2021-04-14 14:52:47 +0000 |
commit | b33425827e3b96fa5f0ee2b7892fb5782c9b7879 (patch) | |
tree | d751ca1805b0e48dc625ec3385df73d3420b9c85 /log.c | |
parent | 89541eeec019626df4651f44b90df6a31a844dab (diff) |
print the datetime when logging to stderr
Diffstat (limited to 'log.c')
-rw-r--r-- | log.c | 20 |
1 files changed, 18 insertions, 2 deletions
@@ -27,6 +27,7 @@ #include <stdio.h> #include <string.h> #include <syslog.h> +#include <time.h> 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); } |