From e5090b89f1212c2968dfa3625659bfa9f1e3edff Mon Sep 17 00:00:00 2001 From: chclvl Date: Fri, 15 Oct 2021 17:48:10 +0200 Subject: Add timestamps to debug logs --- src/debug.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/debug.cpp b/src/debug.cpp index 91f8550..9c1f25d 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -30,6 +30,13 @@ #include #include #include +#include +#include +#include + +static const long int THOUSAND_LONG_INT = 1000LU; +static const unsigned int LOCAL_TIME_CHAR_BUFFER_SIZE = 80U; +static const unsigned int TIMESTAMP_WITH_MS_CHAR_BUFFER_SIZE = LOCAL_TIME_CHAR_BUFFER_SIZE + sizeof(int); static void _debug_log_default(const char *format, ...) { @@ -42,8 +49,19 @@ static void _debug_log_default(const char *format, ...) va_list args; va_start(args, format); - fprintf(stderr, "dbus-c++: "); - vfprintf(stderr, format, args); + struct timeval cur_tv; + gettimeofday(&cur_tv,NULL); //tv.tv_sec // seconds; tv.tv_usec // microseconds + int milli = cur_tv.tv_usec/THOUSAND_LONG_INT; + char buffer[LOCAL_TIME_CHAR_BUFFER_SIZE]; + char timestamp[TIMESTAMP_WITH_MS_CHAR_BUFFER_SIZE] = ""; + strftime(buffer, LOCAL_TIME_CHAR_BUFFER_SIZE, "%Y-%m-%d %H:%M:%S", localtime(&cur_tv.tv_sec)); + sprintf(timestamp, "%s:%03d", buffer, milli); + + // Log format: epoch;Date;eventID;Event;Log type;Log level;Event type;Details + fprintf(stderr, "%lu.%03d;", cur_tv.tv_sec, milli); // Epoch + fprintf(stderr, "%s;", timestamp); // Date + fprintf(stderr, "-;-;dbus-c++;DEBUG;-;", timestamp); // EventId, Event, Log type, Log level, Event type + vfprintf(stderr, format, args); // Details fprintf(stderr, "\n"); va_end(args); -- cgit v1.2.1