From 793b9365ceec0dfccb86ea1377635e9e83053918 Mon Sep 17 00:00:00 2001 From: Davide Paro Date: Sun, 14 Feb 2021 18:05:33 +0100 Subject: [PATCH] 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;