From 3e5970eb1117dbeb3d682af8bff01ed6d0085255 Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Fri, 12 Feb 2021 00:22:47 +0100 Subject: [PATCH 01/14] GCC style printf attrib for checking correct usage of format arguments Defined RXI_LOGC_FMT_ATTRIB which expands to nothing in case __GNUC__ is not defined. It's purpose is to provide printf style checking enforced from the compiler where supported --- src/log.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/log.h b/src/log.h index b1fae24..64dccd7 100644 --- a/src/log.h +++ b/src/log.h @@ -44,6 +44,13 @@ void log_set_quiet(bool enable); int log_add_callback(log_LogFn fn, void *udata, int level); int log_add_fp(FILE *fp, int level); -void log_log(int level, const char *file, int line, const char *fmt, ...); +#define RXI_LOGC_FMT_ATTRIB(n, m) + +#if defined __GNUC__ +# define RXI_LOGC_FMT_ATTRIB(n, m) __attribute__((format(printf, n, m))) +#endif + + +void log_log(int level, const char *file, int line, const char *fmt, ...) RXI_LOGC_FMT_ATTRIB(4, 5); #endif From 815e0d909d35d3bf2522579f9f3bb9918a14dbdb Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Fri, 12 Feb 2021 00:25:34 +0100 Subject: [PATCH 02/14] Moved the define at the top --- src/log.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/log.h b/src/log.h index 64dccd7..bc48712 100644 --- a/src/log.h +++ b/src/log.h @@ -15,6 +15,14 @@ #define LOG_VERSION "0.1.0" +#define RXI_LOGC_PRINTF_ATTRIB(n, m) + +#if defined __GNUC__ +# define RXI_LOGC_PRINTF_ATTRIB(n, m) __attribute__((format(printf, n, m))) +#endif + + + typedef struct { va_list ap; const char *fmt; @@ -44,13 +52,7 @@ void log_set_quiet(bool enable); int log_add_callback(log_LogFn fn, void *udata, int level); int log_add_fp(FILE *fp, int level); -#define RXI_LOGC_FMT_ATTRIB(n, m) -#if defined __GNUC__ -# define RXI_LOGC_FMT_ATTRIB(n, m) __attribute__((format(printf, n, m))) -#endif - - -void log_log(int level, const char *file, int line, const char *fmt, ...) RXI_LOGC_FMT_ATTRIB(4, 5); +void log_log(int level, const char *file, int line, const char *fmt, ...) RXI_LOGC_PRINTF_ATTRIB(4, 5); #endif From a804dcb4b653b4b706a3c8c6816d972a8260524c Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Fri, 12 Feb 2021 00:28:12 +0100 Subject: [PATCH 03/14] Avoid GCC complaining about redefinition of macros --- src/log.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/log.h b/src/log.h index bc48712..be35651 100644 --- a/src/log.h +++ b/src/log.h @@ -15,10 +15,10 @@ #define LOG_VERSION "0.1.0" -#define RXI_LOGC_PRINTF_ATTRIB(n, m) - #if defined __GNUC__ # define RXI_LOGC_PRINTF_ATTRIB(n, m) __attribute__((format(printf, n, m))) +#else +# define RXI_LOGC_PRINTF_ATTRIB(n, m) #endif From e44bbdba867233887890bd8acb499bc301517a76 Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Sun, 14 Feb 2021 17:52:08 +0100 Subject: [PATCH 04/14] RXI_LOGC_DEFAULT_LEVEL --- src/log.h | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/log.h b/src/log.h index be35651..20bfeae 100644 --- a/src/log.h +++ b/src/log.h @@ -15,6 +15,11 @@ #define LOG_VERSION "0.1.0" +#ifndef RXI_LOGC_DEFAULT_LEVEL + # define RXI_LOGC_DEFAULT_LEVEL 0 +#endif + + #if defined __GNUC__ # define RXI_LOGC_PRINTF_ATTRIB(n, m) __attribute__((format(printf, n, m))) #else @@ -36,14 +41,21 @@ typedef struct { typedef void (*log_LogFn)(log_Event *ev); typedef void (*log_LockFn)(bool lock, void *udata); -enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL }; +enum { + LOG_DEBUG = -10, + LOG_TRACE = 0, + LOG_INFO = 32, + LOG_WARN = 64, + LOG_ERROR = 96, + LOG_FATAL = 128, +}; -#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__) -#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__) -#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__) -#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__) -#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__) -#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__) +#define log_debug(...) do { if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__) } while(0) +#define log_trace(...) do { if (LOG_TRACE >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__) } while(0) +#define log_info(...) do { if (LOG_INFO >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__) } while(0) +#define log_warn(...) do { if (LOG_WARN >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__) } while(0) +#define log_error(...) do { if (LOG_ERROR >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__) } while(0) +#define log_fatal(...) do { if (LOG_FATAL >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__) } while(0) const char* log_level_string(int level); void log_set_lock(log_LockFn fn, void *udata); From f0933c40a14ed152554b0a93ff7fec128ceab8a9 Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Sun, 14 Feb 2021 17:54:54 +0100 Subject: [PATCH 05/14] Small fix for RXI logc. --- src/log.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/log.h b/src/log.h index 20bfeae..19c1cfc 100644 --- a/src/log.h +++ b/src/log.h @@ -42,7 +42,7 @@ typedef void (*log_LogFn)(log_Event *ev); typedef void (*log_LockFn)(bool lock, void *udata); enum { - LOG_DEBUG = -10, + LOG_DEBUG = -32, LOG_TRACE = 0, LOG_INFO = 32, LOG_WARN = 64, @@ -50,12 +50,12 @@ enum { LOG_FATAL = 128, }; -#define log_debug(...) do { if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__) } while(0) -#define log_trace(...) do { if (LOG_TRACE >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__) } while(0) -#define log_info(...) do { if (LOG_INFO >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__) } while(0) -#define log_warn(...) do { if (LOG_WARN >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__) } while(0) -#define log_error(...) do { if (LOG_ERROR >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__) } while(0) -#define log_fatal(...) do { if (LOG_FATAL >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__) } while(0) +#define log_debug(...) do { if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__); } while(0) +#define log_trace(...) do { if (LOG_TRACE >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__); } while(0) +#define log_info(...) do { if (LOG_INFO >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__); } while(0) +#define log_warn(...) do { if (LOG_WARN >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__); } while(0) +#define log_error(...) do { if (LOG_ERROR >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__); } while(0) +#define log_fatal(...) do { if (LOG_FATAL >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__); } while(0) const char* log_level_string(int level); void log_set_lock(log_LockFn fn, void *udata); From 793b9365ceec0dfccb86ea1377635e9e83053918 Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Sun, 14 Feb 2021 18:05:33 +0100 Subject: [PATCH 06/14] Fixed bug --- src/log.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/log.c b/src/log.c index 1a7626e..0939b4a 100644 --- a/src/log.c +++ b/src/log.c @@ -40,28 +40,42 @@ static struct { static const char *level_strings[] = { - "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" + "DEBUG", "TRACE", "INFO", "WARN", "ERROR", "FATAL" }; + +static inline const char *get_level_string(int level) +{ + return level_strings[(level + 32) / 32]; +} + #ifdef LOG_USE_COLOR static const char *level_colors[] = { - "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m" + "\x1b[36m", "\x1b[94m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m" }; + +static inline const char *get_level_color(int level) +{ + return level_colors[(level + 32) / 32]; +} #endif + + + static void stdout_callback(log_Event *ev) { char buf[16]; buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0'; #ifdef LOG_USE_COLOR fprintf( ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", - buf, level_colors[ev->level], level_strings[ev->level], + buf, get_level_color(ev->level), get_level_string(ev->level), ev->file, ev->line); #else fprintf( ev->udata, "%s %-5s %s:%d: ", - buf, level_strings[ev->level], ev->file, ev->line); + buf, get_level_string(ev->level), ev->file, ev->line); #endif vfprintf(ev->udata, ev->fmt, ev->ap); fprintf(ev->udata, "\n"); @@ -74,7 +88,7 @@ static void file_callback(log_Event *ev) { buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0'; fprintf( ev->udata, "%s %-5s %s:%d: ", - buf, level_strings[ev->level], ev->file, ev->line); + buf, get_level_string(ev->level), ev->file, ev->line); vfprintf(ev->udata, ev->fmt, ev->ap); fprintf(ev->udata, "\n"); fflush(ev->udata); @@ -91,11 +105,6 @@ static void unlock(void) { } -const char* log_level_string(int level) { - return level_strings[level]; -} - - void log_set_lock(log_LockFn fn, void *udata) { L.lock = fn; L.udata = udata; From b89f2203e7cd0caa850ef825f5c7f9f6589ab4e4 Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Sun, 14 Feb 2021 20:48:08 +0100 Subject: [PATCH 07/14] Nicer logging --- src/log.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/log.c b/src/log.c index 0939b4a..97ed1cf 100644 --- a/src/log.c +++ b/src/log.c @@ -40,7 +40,7 @@ static struct { static const char *level_strings[] = { - "DEBUG", "TRACE", "INFO", "WARN", "ERROR", "FATAL" + "[[DEBUG]]", "[[TRACE]]", "[[ INFO]]", "[[ WARN]]", "[[ERROR]]", "[[FATAL]]" }; @@ -69,12 +69,12 @@ static void stdout_callback(log_Event *ev) { buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0'; #ifdef LOG_USE_COLOR fprintf( - ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", + 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, ev->line); #else fprintf( - ev->udata, "%s %-5s %s:%d: ", + ev->udata, "%s %-7s %s:%d: ", buf, get_level_string(ev->level), ev->file, ev->line); #endif vfprintf(ev->udata, ev->fmt, ev->ap); @@ -87,7 +87,7 @@ 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 %-5s %s:%d: ", + 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"); From 783a0070e5abb9fcea61a18cdaf9d0c0ca33b12b Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Mon, 18 Oct 2021 18:49:29 +0200 Subject: [PATCH 08/14] Changed log level. --- src/log.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/log.h b/src/log.h index 19c1cfc..d8dc2b5 100644 --- a/src/log.h +++ b/src/log.h @@ -42,8 +42,8 @@ typedef void (*log_LogFn)(log_Event *ev); typedef void (*log_LockFn)(bool lock, void *udata); enum { - LOG_DEBUG = -32, - LOG_TRACE = 0, + LOG_DEBUG = -64, + LOG_TRACE = -32, LOG_INFO = 32, LOG_WARN = 64, LOG_ERROR = 96, From add04ef1d674a199a392b2e4e0e1f19a185e71e9 Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Tue, 19 Oct 2021 18:37:06 +0200 Subject: [PATCH 09/14] Fixed some stuff. --- src/log.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/log.c b/src/log.c index 97ed1cf..12938e9 100644 --- a/src/log.c +++ b/src/log.c @@ -36,7 +36,13 @@ static struct { int level; bool quiet; Callback callbacks[MAX_CALLBACKS]; -} L; +} L = { + NULL, + NULL, + RXI_LOGC_DEFAULT_LEVEL, + false, + {0}, +}; static const char *level_strings[] = { From b6699aa05aab462b098758dba27c0d4178cb19cb Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Tue, 19 Oct 2021 18:41:37 +0200 Subject: [PATCH 10/14] Fixed some stuff. --- src/log.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/log.h b/src/log.h index d8dc2b5..93894a6 100644 --- a/src/log.h +++ b/src/log.h @@ -16,7 +16,7 @@ #define LOG_VERSION "0.1.0" #ifndef RXI_LOGC_DEFAULT_LEVEL - # define RXI_LOGC_DEFAULT_LEVEL 0 + # define RXI_LOGC_DEFAULT_LEVEL 1 #endif @@ -42,8 +42,8 @@ typedef void (*log_LogFn)(log_Event *ev); typedef void (*log_LockFn)(bool lock, void *udata); enum { - LOG_DEBUG = -64, - LOG_TRACE = -32, + LOG_DEBUG = -32, + LOG_TRACE = 0, LOG_INFO = 32, LOG_WARN = 64, LOG_ERROR = 96, From 8c549ec6385f9944796600e2757a623c14149466 Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Thu, 16 Dec 2021 21:19:15 +0100 Subject: [PATCH 11/14] Updated LOG_C --- src/log.h | 79 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/src/log.h b/src/log.h index 93894a6..262e954 100644 --- a/src/log.h +++ b/src/log.h @@ -16,31 +16,15 @@ #define LOG_VERSION "0.1.0" #ifndef RXI_LOGC_DEFAULT_LEVEL - # define RXI_LOGC_DEFAULT_LEVEL 1 +#define RXI_LOGC_DEFAULT_LEVEL LOG_TRACE #endif - #if defined __GNUC__ -# define RXI_LOGC_PRINTF_ATTRIB(n, m) __attribute__((format(printf, n, m))) +#define RXI_LOGC_PRINTF_ATTRIB(n, m) __attribute__((format(printf, n, m))) #else -# define RXI_LOGC_PRINTF_ATTRIB(n, m) +#define RXI_LOGC_PRINTF_ATTRIB(n, m) #endif - - -typedef struct { - va_list ap; - const char *fmt; - const char *file; - struct tm *time; - void *udata; - int line; - int level; -} log_Event; - -typedef void (*log_LogFn)(log_Event *ev); -typedef void (*log_LockFn)(bool lock, void *udata); - enum { LOG_DEBUG = -32, LOG_TRACE = 0, @@ -50,21 +34,60 @@ enum { LOG_FATAL = 128, }; -#define log_debug(...) do { if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__); } while(0) -#define log_trace(...) do { if (LOG_TRACE >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__); } while(0) -#define log_info(...) do { if (LOG_INFO >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__); } while(0) -#define log_warn(...) do { if (LOG_WARN >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__); } while(0) -#define log_error(...) do { if (LOG_ERROR >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__); } while(0) -#define log_fatal(...) do { if (LOG_FATAL >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__); } while(0) +typedef struct { + va_list ap; + const char *fmt; + const char *file; + struct tm *time; + void *udata; + int line; + int level; +} log_Event; -const char* log_level_string(int level); +typedef void (*log_LogFn)(log_Event *ev); +typedef void (*log_LockFn)(bool lock, void *udata); + + + +#define log_debug(...) \ + do { \ + if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) +#define log_trace(...) \ + do { \ + if (LOG_TRACE >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) +#define log_info(...) \ + do { \ + if (LOG_INFO >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) +#define log_warn(...) \ + do { \ + if (LOG_WARN >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) +#define log_error(...) \ + do { \ + if (LOG_ERROR >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) +#define log_fatal(...) \ + do { \ + if (LOG_FATAL >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) + +const char *log_level_string(int level); void log_set_lock(log_LockFn fn, void *udata); void log_set_level(int level); void log_set_quiet(bool enable); int log_add_callback(log_LogFn fn, void *udata, int level); int log_add_fp(FILE *fp, int level); - -void log_log(int level, const char *file, int line, const char *fmt, ...) RXI_LOGC_PRINTF_ATTRIB(4, 5); +void log_log(int level, const char *file, int line, const char *fmt, ...) + RXI_LOGC_PRINTF_ATTRIB(4, 5); #endif From d2f9d62d19e3231f1ff99d6ab844978fd938c69d Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Thu, 16 Dec 2021 21:19:49 +0100 Subject: [PATCH 12/14] Updated LOG_C --- src/log.h | 79 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/src/log.h b/src/log.h index 93894a6..262e954 100644 --- a/src/log.h +++ b/src/log.h @@ -16,31 +16,15 @@ #define LOG_VERSION "0.1.0" #ifndef RXI_LOGC_DEFAULT_LEVEL - # define RXI_LOGC_DEFAULT_LEVEL 1 +#define RXI_LOGC_DEFAULT_LEVEL LOG_TRACE #endif - #if defined __GNUC__ -# define RXI_LOGC_PRINTF_ATTRIB(n, m) __attribute__((format(printf, n, m))) +#define RXI_LOGC_PRINTF_ATTRIB(n, m) __attribute__((format(printf, n, m))) #else -# define RXI_LOGC_PRINTF_ATTRIB(n, m) +#define RXI_LOGC_PRINTF_ATTRIB(n, m) #endif - - -typedef struct { - va_list ap; - const char *fmt; - const char *file; - struct tm *time; - void *udata; - int line; - int level; -} log_Event; - -typedef void (*log_LogFn)(log_Event *ev); -typedef void (*log_LockFn)(bool lock, void *udata); - enum { LOG_DEBUG = -32, LOG_TRACE = 0, @@ -50,21 +34,60 @@ enum { LOG_FATAL = 128, }; -#define log_debug(...) do { if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__); } while(0) -#define log_trace(...) do { if (LOG_TRACE >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__); } while(0) -#define log_info(...) do { if (LOG_INFO >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__); } while(0) -#define log_warn(...) do { if (LOG_WARN >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__); } while(0) -#define log_error(...) do { if (LOG_ERROR >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__); } while(0) -#define log_fatal(...) do { if (LOG_FATAL >= RXI_LOGC_DEFAULT_LEVEL) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__); } while(0) +typedef struct { + va_list ap; + const char *fmt; + const char *file; + struct tm *time; + void *udata; + int line; + int level; +} log_Event; -const char* log_level_string(int level); +typedef void (*log_LogFn)(log_Event *ev); +typedef void (*log_LockFn)(bool lock, void *udata); + + + +#define log_debug(...) \ + do { \ + if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) +#define log_trace(...) \ + do { \ + if (LOG_TRACE >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) +#define log_info(...) \ + do { \ + if (LOG_INFO >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) +#define log_warn(...) \ + do { \ + if (LOG_WARN >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) +#define log_error(...) \ + do { \ + if (LOG_ERROR >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) +#define log_fatal(...) \ + do { \ + if (LOG_FATAL >= RXI_LOGC_DEFAULT_LEVEL) \ + log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) + +const char *log_level_string(int level); void log_set_lock(log_LockFn fn, void *udata); void log_set_level(int level); void log_set_quiet(bool enable); int log_add_callback(log_LogFn fn, void *udata, int level); int log_add_fp(FILE *fp, int level); - -void log_log(int level, const char *file, int line, const char *fmt, ...) RXI_LOGC_PRINTF_ATTRIB(4, 5); +void log_log(int level, const char *file, int line, const char *fmt, ...) + RXI_LOGC_PRINTF_ATTRIB(4, 5); #endif From a885027ff1669b638314c36d01078369321d71a6 Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Tue, 4 Jan 2022 18:20:35 +0100 Subject: [PATCH 13/14] Fixed thread race condition --- src/log.c | 183 +++++++++++++++++++++++------------------------------- src/log.h | 3 +- 2 files changed, 79 insertions(+), 107 deletions(-) diff --git a/src/log.c b/src/log.c index 12938e9..9128554 100644 --- a/src/log.c +++ b/src/log.c @@ -25,159 +25,132 @@ #define MAX_CALLBACKS 32 typedef struct { - log_LogFn fn; - void *udata; - int level; + log_LogFn fn; + void *udata; + int level; } Callback; static struct { - void *udata; - log_LockFn lock; - int level; - bool quiet; - Callback callbacks[MAX_CALLBACKS]; + void *udata; + log_LockFn lock; + int level; + bool quiet; + Callback callbacks[MAX_CALLBACKS]; } L = { - NULL, - NULL, - RXI_LOGC_DEFAULT_LEVEL, - false, - {0}, + NULL, NULL, RXI_LOGC_DEFAULT_LEVEL, false, {0}, }; +static const char *level_strings[] = {"[[DEBUG]]", "[[TRACE]]", "[[ INFO]]", + "[[ WARN]]", "[[ERROR]]", "[[FATAL]]"}; -static const char *level_strings[] = { - "[[DEBUG]]", "[[TRACE]]", "[[ INFO]]", "[[ WARN]]", "[[ERROR]]", "[[FATAL]]" -}; - - -static inline const char *get_level_string(int level) -{ +static inline const char *get_level_string(int level) { return level_strings[(level + 32) / 32]; } #ifdef LOG_USE_COLOR -static const char *level_colors[] = { - "\x1b[36m", "\x1b[94m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m" -}; +static const char *level_colors[] = {"\x1b[36m", "\x1b[94m", "\x1b[32m", + "\x1b[33m", "\x1b[31m", "\x1b[35m"}; -static inline const char *get_level_color(int level) -{ +static inline const char *get_level_color(int level) { return level_colors[(level + 32) / 32]; } #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'; #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, ev->line); + 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, + ev->line); #else - fprintf( - ev->udata, "%s %-7s %s:%d: ", - buf, get_level_string(ev->level), ev->file, ev->line); + fprintf(ev->udata, "%s %-7s %s:%d: ", buf, get_level_string(ev->level), + ev->file, ev->line); #endif - vfprintf(ev->udata, ev->fmt, ev->ap); - fprintf(ev->udata, "\n"); - fflush(ev->udata); + vfprintf(ev->udata, ev->fmt, ev->ap); + fprintf(ev->udata, "\n"); + fflush(ev->udata); } - 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); - vfprintf(ev->udata, ev->fmt, ev->ap); - fprintf(ev->udata, "\n"); - fflush(ev->udata); + 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); + vfprintf(ev->udata, ev->fmt, ev->ap); + fprintf(ev->udata, "\n"); + fflush(ev->udata); } - -static void lock(void) { - if (L.lock) { L.lock(true, L.udata); } +static void lock(void) { + if (L.lock) { + L.lock(true, L.udata); + } } - static void unlock(void) { - if (L.lock) { L.lock(false, L.udata); } + if (L.lock) { + L.lock(false, L.udata); + } } - void log_set_lock(log_LockFn fn, void *udata) { - L.lock = fn; - L.udata = udata; + L.lock = fn; + L.udata = udata; } +void log_set_level(int level) { L.level = level; } -void log_set_level(int level) { - L.level = level; -} - - -void log_set_quiet(bool enable) { - L.quiet = enable; -} - +void log_set_quiet(bool enable) { L.quiet = enable; } int log_add_callback(log_LogFn fn, void *udata, int level) { - for (int i = 0; i < MAX_CALLBACKS; i++) { - if (!L.callbacks[i].fn) { - L.callbacks[i] = (Callback) { fn, udata, level }; - return 0; + for (int i = 0; i < MAX_CALLBACKS; i++) { + if (!L.callbacks[i].fn) { + L.callbacks[i] = (Callback){fn, udata, level}; + return 0; + } } - } - return -1; + return -1; } - int log_add_fp(FILE *fp, int level) { - return log_add_callback(file_callback, fp, level); + return log_add_callback(file_callback, fp, level); } - static void init_event(log_Event *ev, void *udata) { - if (!ev->time) { - time_t t = time(NULL); - ev->time = localtime(&t); - } - ev->udata = udata; + if (!ev->time) { + time_t t = time(NULL); + localtime_r(&t, &ev->time_buf); + } + ev->udata = udata; } - void log_log(int level, const char *file, int line, const char *fmt, ...) { - log_Event ev = { - .fmt = fmt, - .file = file, - .line = line, - .level = level, - }; + log_Event ev = { + .fmt = fmt, + .file = file, + .line = line, + .level = level, + }; - lock(); + lock(); - if (!L.quiet && level >= L.level) { - init_event(&ev, stderr); - va_start(ev.ap, fmt); - stdout_callback(&ev); - va_end(ev.ap); - } - - for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) { - Callback *cb = &L.callbacks[i]; - if (level >= cb->level) { - init_event(&ev, cb->udata); - va_start(ev.ap, fmt); - cb->fn(&ev); - va_end(ev.ap); + if (!L.quiet && level >= L.level) { + init_event(&ev, stderr); + va_start(ev.ap, fmt); + stdout_callback(&ev); + va_end(ev.ap); } - } - unlock(); + for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) { + Callback *cb = &L.callbacks[i]; + if (level >= cb->level) { + init_event(&ev, cb->udata); + va_start(ev.ap, fmt); + cb->fn(&ev); + va_end(ev.ap); + } + } + + unlock(); } diff --git a/src/log.h b/src/log.h index 262e954..c01f9be 100644 --- a/src/log.h +++ b/src/log.h @@ -39,6 +39,7 @@ typedef struct { const char *fmt; const char *file; struct tm *time; + struct tm time_buf; void *udata; int line; int level; @@ -47,8 +48,6 @@ typedef struct { typedef void (*log_LogFn)(log_Event *ev); typedef void (*log_LockFn)(bool lock, void *udata); - - #define log_debug(...) \ do { \ if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) \ From a8ffd1c6895cbad8e9c0403acba61680ee20ddf9 Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Tue, 4 Jan 2022 18:22:47 +0100 Subject: [PATCH 14/14] Forgot this line in previous commit --- src/log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/log.c b/src/log.c index 9128554..ab09208 100644 --- a/src/log.c +++ b/src/log.c @@ -120,7 +120,7 @@ int log_add_fp(FILE *fp, int level) { static void init_event(log_Event *ev, void *udata) { if (!ev->time) { time_t t = time(NULL); - localtime_r(&t, &ev->time_buf); + ev->time = localtime_r(&t, &ev->time_buf); } ev->udata = udata; }