util: On -DDEBUG, log to stderr with timestamps

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
Stefano Brivio 2021-05-21 11:22:09 +02:00
parent 84a62b79a2
commit 7ab1b2a97a
2 changed files with 27 additions and 1 deletions

View file

@ -795,7 +795,7 @@ int main(int argc, char **argv)
} }
#if DEBUG #if DEBUG
openlog("passt", LOG_PERROR, LOG_DAEMON); openlog("passt", 0, LOG_DAEMON);
#else #else
openlog("passt", isatty(fileno(stdout)) ? 0 : LOG_PERROR, LOG_DAEMON); openlog("passt", isatty(fileno(stdout)) ? 0 : LOG_PERROR, LOG_DAEMON);
#endif #endif

26
util.c
View file

@ -22,10 +22,35 @@
#include <sys/epoll.h> #include <sys/epoll.h>
#include <syslog.h> #include <syslog.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h>
#include <time.h>
#include "passt.h" #include "passt.h"
#include "util.h" #include "util.h"
#ifdef DEBUG
#define logfn(name, level) \
void name(const char *format, ...) { \
char ts[sizeof("Mmm dd hh:mm:ss.")]; \
struct timespec tp; \
struct tm *tm; \
va_list args; \
\
clock_gettime(CLOCK_REALTIME, &tp); \
tm = gmtime(&tp.tv_sec); \
strftime(ts, sizeof(ts), "%b %d %T.", tm); \
\
fprintf(stderr, "%s%04lu: ", ts, tp.tv_nsec / (1000 * 1000)); \
va_start(args, format); \
vsyslog(level, format, args); \
va_end(args); \
va_start(args, format); \
vfprintf(stderr, format, args); \
va_end(args); \
if (format[strlen(format)] != '\n') \
fprintf(stderr, "\n"); \
}
#else
#define logfn(name, level) \ #define logfn(name, level) \
void name(const char *format, ...) { \ void name(const char *format, ...) { \
va_list args; \ va_list args; \
@ -34,6 +59,7 @@ void name(const char *format, ...) { \
vsyslog(level, format, args); \ vsyslog(level, format, args); \
va_end(args); \ va_end(args); \
} }
#endif
logfn(err, LOG_ERR) logfn(err, LOG_ERR)
logfn(warn, LOG_WARNING) logfn(warn, LOG_WARNING)