From eb89dafd0d72a1c3816b9b3bb79f10e43d023056 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 20:12:35 -0800 Subject: [PATCH] Add --log-timestamps --- adsbus/adsbus.c | 5 +++++ adsbus/log.c | 21 ++++++++++++++++++++- adsbus/log.h | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index 06a4888..88353ea 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -78,6 +78,7 @@ static bool parse_opts(int argc, char *argv[]) { {"stdin", no_argument, 0, 'i'}, {"stdout", required_argument, 0, 'o'}, {"log-file", required_argument, 0, '1'}, + {"log-timestamps", no_argument, 0, '2'}, {0, 0, 0, 0 }, }; @@ -153,6 +154,10 @@ static bool parse_opts(int argc, char *argv[]) { handler = log_reopen; break; + case '2': + handler = log_enable_timestamps; + break; + case 'h': default: print_usage(argv[0]); diff --git a/adsbus/log.c b/adsbus/log.c index cfc7595..2fe02f1 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -1,11 +1,14 @@ #include #include #include +#include #include #include #include #include +#include #include +#include #include #include "log.h" @@ -19,6 +22,7 @@ static char *log_path = NULL; static uint8_t log_id[UUID_LEN]; static int log_rotate_fd; static struct peer log_rotate_peer; +static bool log_timestamps = false; static char log_module = 'L'; @@ -90,10 +94,25 @@ bool log_reopen(const char *path) { return true; } +bool log_enable_timestamps(const char __attribute__ ((unused)) *arg) { + log_timestamps = true; + return true; +} + void log_write(char type, const char *loc, const uint8_t *id, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - assert(fprintf(log_stream, "%c [%18s] %s: ", type, loc, id) > 0); + + char datetime[64] = ""; + if (log_timestamps) { + time_t now; + assert(time(&now) >= 0); + struct tm tmnow; + assert(gmtime_r(&now, &tmnow)); + assert(strftime(datetime, sizeof(datetime), "%FT%TZ ", &tmnow) > 0); + } + + assert(fprintf(log_stream, "%s[%18s] %c %s: ", datetime, loc, type, id) > 0); assert(vfprintf(log_stream, fmt, ap) > 0); assert(fprintf(log_stream, "\n") == 1); va_end(ap); diff --git a/adsbus/log.h b/adsbus/log.h index 47002da..5c9c5a8 100644 --- a/adsbus/log.h +++ b/adsbus/log.h @@ -11,5 +11,6 @@ void log_init(void); void log_init2(void); void log_cleanup(void); bool log_reopen(const char *); +bool log_enable_timestamps(const char *); void log_write(char, const char *, const uint8_t *, const char *, ...) __attribute__ ((__format__ (__printf__, 4, 5)));