From 9bec435336aa677cbd5a0129537a1f958847f2be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rostislav=20St=C5=99=C3=ADbrn=C3=BD?= Date: Fri, 4 Nov 2022 12:45:14 +0100 Subject: [PATCH] Microsecond support added + fixing TRACE level logging --- src/log.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++------ src/log.h | 4 +++- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/log.c b/src/log.c index b112041..10db922 100644 --- a/src/log.c +++ b/src/log.c @@ -20,6 +20,7 @@ * IN THE SOFTWARE. */ +#include #include "log.h" #define MAX_CALLBACKS 32 @@ -40,6 +41,20 @@ static struct { NULL, NULL, RXI_LOGC_DEFAULT_LEVEL, false, {{0}}, }; +static void time_to_str(char *); + +static int year; +static int month; +static int day; +static int hour; +static int minutes; +static int seconds; +static int usec; + +static struct timeval tv; +static struct tm *tm; + + static const char *level_strings[] = {"[[TRACE]]", "[[DEBUG]]", "[[ INFO]]", "[[ WARN]]", "[[ERROR]]", "[[FATAL]]"}; @@ -57,8 +72,10 @@ static inline const char *get_level_color(int level) { #endif static void stdout_callback(log_Event *ev) { - char buf[16]; - buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0'; + //char buf[16]; + //buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0'; + char *buf = ev->time_str; + #ifdef LOG_USE_COLOR fprintf(ev->udata, "%s %s%-7s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", buf, get_level_color(ev->level), get_level_string(ev->level), ev->file, @@ -73,10 +90,12 @@ static void stdout_callback(log_Event *ev) { } static void file_callback(log_Event *ev) { - char buf[64]; - buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0'; - fprintf(ev->udata, "%s %-7s %s:%d: ", buf, get_level_string(ev->level), - ev->file, ev->line); + //char buf[64]; + //buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0'; + char *buf = ev->time_str; + + fprintf(ev->udata, "%s %-7s %s:%d: ", buf, get_level_string(ev->level), ev->file, ev->line); + vfprintf(ev->udata, ev->fmt, ev->ap); fprintf(ev->udata, "\n"); fflush(ev->udata); @@ -135,6 +154,8 @@ void log_log(int level, const char *file, int line, const char *fmt, ...) { lock(); + time_to_str(ev.time_str); + if (!L.quiet && level >= L.level) { init_event(&ev, stderr); va_start(ev.ap, fmt); @@ -154,3 +175,31 @@ void log_log(int level, const char *file, int line, const char *fmt, ...) { unlock(); } + +/* Write time to buf in format YYYY-MM-DD HH:MM:SS.ms */ +static void time_to_str(char *buf) +{ + gettimeofday(&tv, NULL); + tm = localtime(&tv.tv_sec); + /* Add 1900 to get the right year value read the manual page for localtime() */ + year = tm->tm_year + 1900; + /* Months are 0 indexed in struct tm */ + month = tm->tm_mon + 1; + day = tm->tm_mday; + hour = tm->tm_hour; + minutes = tm->tm_min; + seconds = tm->tm_sec; + usec = tv.tv_usec; + // msec + // buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt)] = '\0'; + int len = sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d.%06d ", + year, + month, + day, + hour, + minutes, + seconds, + usec); + buf[len] = '\0'; +} + diff --git a/src/log.h b/src/log.h index ee5a6df..172ce2a 100644 --- a/src/log.h +++ b/src/log.h @@ -11,12 +11,13 @@ #include #include #include +#include #include #define LOG_VERSION "0.1.0" #ifndef RXI_LOGC_DEFAULT_LEVEL -#define RXI_LOGC_DEFAULT_LEVEL LOG_DEBUG +#define RXI_LOGC_DEFAULT_LEVEL LOG_TRACE #endif #if defined __GNUC__ @@ -40,6 +41,7 @@ typedef struct { const char *file; struct tm *time; struct tm time_buf; + char time_str[32]; void *udata; int line; int level;