print the datetime when logging to stderr

This commit is contained in:
Omar Polo 2021-04-14 14:52:47 +00:00
parent 89541eeec0
commit b33425827e
2 changed files with 22 additions and 2 deletions

View File

@ -1,3 +1,7 @@
2021-04-14 Omar Polo <op@omarpolo.com>
* log.c (handle_imsg_log): print the datetime when logging to stderr
2021-04-13 Omar Polo <op@omarpolo.com> 2021-04-13 Omar Polo <op@omarpolo.com>
* ex.c (launch_cgi): define TLS_VERSION, TLS_CIPHER and TLS_CIPHER_STRENGTH for CGI scripts * ex.c (launch_cgi): define TLS_VERSION, TLS_CIPHER and TLS_CIPHER_STRENGTH for CGI scripts

20
log.c
View File

@ -27,6 +27,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
#include <time.h>
static struct event imsgev; static struct event imsgev;
@ -39,6 +40,19 @@ static imsg_handlerfn *handlers[] = {
[IMSG_LOG] = handle_imsg_log, [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 void
fatal(const char *fmt, ...) fatal(const char *fmt, ...)
{ {
@ -47,6 +61,7 @@ fatal(const char *fmt, ...)
va_start(ap, fmt); va_start(ap, fmt);
if (conf.foreground) { if (conf.foreground) {
print_date();
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} else } else
@ -249,9 +264,10 @@ handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
msg = imsg->data; msg = imsg->data;
msg[datalen-1] = '\0'; msg[datalen-1] = '\0';
if (conf.foreground) if (conf.foreground) {
print_date();
fprintf(stderr, "%s\n", msg); fprintf(stderr, "%s\n", msg);
else } else
syslog(LOG_DAEMON, "%s", msg); syslog(LOG_DAEMON, "%s", msg);
} }