aboutgitcodebugslistschat
path: root/util.c
diff options
context:
space:
mode:
authorStefano Brivio <sbrivio@redhat.com>2021-05-21 11:22:09 +0200
committerStefano Brivio <sbrivio@redhat.com>2021-05-21 11:22:09 +0200
commit7ab1b2a97ab43ef58905801572e86b1bbb517973 (patch)
tree9f142c47f472537530d2953fa4d4c5010e156f67 /util.c
parent84a62b79a2bccb8b7db24e230854a7b160c0851b (diff)
downloadpasst-7ab1b2a97ab43ef58905801572e86b1bbb517973.tar
passt-7ab1b2a97ab43ef58905801572e86b1bbb517973.tar.gz
passt-7ab1b2a97ab43ef58905801572e86b1bbb517973.tar.bz2
passt-7ab1b2a97ab43ef58905801572e86b1bbb517973.tar.lz
passt-7ab1b2a97ab43ef58905801572e86b1bbb517973.tar.xz
passt-7ab1b2a97ab43ef58905801572e86b1bbb517973.tar.zst
passt-7ab1b2a97ab43ef58905801572e86b1bbb517973.zip
util: On -DDEBUG, log to stderr with timestamps
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'util.c')
-rw-r--r--util.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/util.c b/util.c
index bcec7be..59a0cb2 100644
--- a/util.c
+++ b/util.c
@@ -22,18 +22,44 @@
#include <sys/epoll.h>
#include <syslog.h>
#include <stdarg.h>
+#include <string.h>
+#include <time.h>
#include "passt.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) \
+void name(const char *format, ...) { \
+ va_list args; \
+ \
+ va_start(args, format); \
+ vsyslog(level, format, args); \
+ va_end(args); \
+}
+#endif
logfn(err, LOG_ERR)
logfn(warn, LOG_WARNING)