diff options
author | Danny Holman <dholman@gymli.org> | 2024-10-02 15:02:23 -0500 |
---|---|---|
committer | Danny Holman <dholman@gymli.org> | 2024-10-02 15:02:23 -0500 |
commit | f079eba34b36b4d76e5009c6f25e2d1dff3ae372 (patch) | |
tree | 78088648f377d4b8d4438b541240c6f44eea87cc /core/logging.c | |
parent | c840b1da6b0ea5ebf30eb028ecce6c47bcebf88c (diff) |
core: logging: make color output optional
Add functions that toggle color output from the logging framework.
Signed-off-by: Danny Holman <dholman@gymli.org>
Diffstat (limited to 'core/logging.c')
-rw-r--r-- | core/logging.c | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/core/logging.c b/core/logging.c index df97b4d..06fd95d 100644 --- a/core/logging.c +++ b/core/logging.c @@ -3,44 +3,65 @@ #include <string.h> #include <stdarg.h> -#define LSTR_FATAL "\033[31m[FATAL]" -#define LSTR_ERROR "\033[31m[ERROR]" -#define LSTR_WARN "\033[33m[WARNING]" -#define LSTR_INFO "\033[0m[INFO]" -#define LSTR_DEBUG "\033[32m[DEBUG]" +#define COLOR_NONE "" +#define COLOR_DEFAULT "\033[0m" +#define COLOR_RED "\033[31m" +#define COLOR_GREEN "\033[32m" +#define COLOR_YELLOW "\033[33m" +#define COLOR_BLUE "\033[34m" + +#define LSTR_FATAL "[FATAL]" +#define LSTR_ERROR "[ERROR]" +#define LSTR_WARN "[WARNING]" +#define LSTR_INFO "[INFO]" +#define LSTR_DEBUG "[DEBUG]" static int debug_enabled = 0; +static int color_enabled = 1; void log_output(int level, const char *fmt, ...) { - char out[8192]; + char out[4096]; memset(out, 0, sizeof(out)); va_list arg_ptr; va_start(arg_ptr, fmt); - vsnprintf(out, 8192, fmt, arg_ptr); + vsnprintf(out, 4096, fmt, arg_ptr); va_end(arg_ptr); char *lvl_str; + char *color = COLOR_NONE; switch (level) { case LOG_FATAL: + if (color_enabled == 1) + color = COLOR_RED; lvl_str = LSTR_FATAL; break; case LOG_ERROR: + if (color_enabled == 1) + color = COLOR_RED; lvl_str = LSTR_ERROR; break; case LOG_WARN: + if (color_enabled == 1) + color = COLOR_YELLOW; lvl_str = LSTR_WARN; break; case LOG_INFO: lvl_str = LSTR_INFO; break; case LOG_DEBUG: + if (color_enabled == 1) + color = COLOR_GREEN; if (debug_enabled == 0) return; lvl_str = LSTR_DEBUG; break; } - printf("%s %s\n%s", lvl_str, out, "\033[0m"); + + if (color_enabled == 0) + printf("%s %s\n", lvl_str, out); + else + printf("%s%s %s\n%s", color, lvl_str, out, COLOR_DEFAULT); } void enable_log_debug(void) { @@ -50,3 +71,11 @@ void enable_log_debug(void) { void disable_log_debug(void) { debug_enabled = 0; } + +void enable_log_color(void) { + color_enabled = 1; +} + +void disable_log_color(void) { + color_enabled = 0; +} |