From 9e45032bd53d07e78c2ec41f1ae9d0d75e4910ce Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 Apr 2012 23:10:46 +0200 Subject: Add timestamps to log messages --- src/fastd.h | 14 +++++++++++++- src/printf.c | 8 ++++---- 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/fastd.h b/src/fastd.h index e48929d..8f53409 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -144,7 +144,19 @@ static inline int fastd_rand(fastd_context *ctx, int min, int max) { #define pr_log(ctx, level, prefix, args...) do { \ if ((ctx)->conf == NULL || (level) <= (ctx)->conf->loglevel) { \ - fputs(prefix, stderr); fastd_printf(ctx, args); fputs("\n", stderr); \ + char timestr[100]; \ + time_t t; \ + struct tm tm; \ + \ + t = time(NULL); \ + if (localtime_r(&t, &tm) != NULL) { \ + if (strftime(timestr, sizeof(timestr), "%F %T %z --- ", &tm) > 0) \ + fputs(timestr, stderr); \ + } \ + \ + fputs(prefix, stderr); \ + fastd_printf(ctx, args); \ + fputs("\n", stderr); \ } \ } while(0) diff --git a/src/printf.c b/src/printf.c index 53298bf..7c922c5 100644 --- a/src/printf.c +++ b/src/printf.c @@ -195,10 +195,10 @@ void fastd_printf(const fastd_context *ctx, const char *format, ...) { if (p) { if (inet_ntop(flag_l ? AF_INET6 : AF_INET, p, addr_buf, sizeof(addr_buf))) - fprintf(stderr, "%s", addr_buf); + fputs(addr_buf, stderr); } else { - fprintf(stderr, "(null)"); + fputs("(null)", stderr); } break; @@ -211,7 +211,7 @@ void fastd_printf(const fastd_context *ctx, const char *format, ...) { eth_addr->data[3], eth_addr->data[4], eth_addr->data[5]); } else { - fprintf(stderr, "(null)"); + fputs("(null)", stderr); } break; @@ -221,7 +221,7 @@ void fastd_printf(const fastd_context *ctx, const char *format, ...) { if (peer) print_peer_str(ctx, peer); else - fprintf(stderr, "(null)"); + fputs("(null)", stderr); break; default: -- cgit v1.2.3