diff options
author | chclvl <chclvl@users.sourceforge.net> | 2021-10-15 17:48:51 +0200 |
---|---|---|
committer | chclvl <chclvl@users.sourceforge.net> | 2021-10-15 17:48:51 +0200 |
commit | 076a2a5981197467a5b567367ab8c170a228c0d6 (patch) | |
tree | 5ea0587e6de11b4303f52649afe779d1f1b518d3 | |
parent | fe5ca2fd155d5f292a4fc5a098ec5cbf0da2816f (diff) | |
parent | e5090b89f1212c2968dfa3625659bfa9f1e3edff (diff) | |
download | dbus-c++-develop.tar.gz |
Merge branch 'feature-add_timestamps_to_debug_logs' into developdevelop
-rw-r--r-- | src/debug.cpp | 22 |
1 files 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 <stdarg.h> #include <cstdio> #include <stdlib.h> +#include <stdio.h> +#include <sys/time.h> +#include <time.h> + +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); |