From 178d4afec8db1ceee7d26f821ba5f7ac42733648 Mon Sep 17 00:00:00 2001 From: icex2 Date: Sun, 11 Jun 2023 16:47:13 +0200 Subject: [PATCH] feat(util/log): Expose proper log level definition with enum Internally, this was already used but it lacked a clean public interface to set different log levels from the outside. Useful for various command line tooling to implement verbosity levels, e.g. -v, -vv, -vvv --- src/main/util/log.c | 28 +++++++++++++--------------- src/main/util/log.h | 9 ++++++++- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/util/log.c b/src/main/util/log.c index f1f697e..7da1d63 100644 --- a/src/main/util/log.c +++ b/src/main/util/log.c @@ -10,14 +10,14 @@ static log_writer_t log_writer; static void *log_writer_ctx; -static unsigned int log_level; +static enum log_level log_level; static void log_builtin_fatal(const char *module, const char *fmt, ...); static void log_builtin_info(const char *module, const char *fmt, ...); static void log_builtin_misc(const char *module, const char *fmt, ...); static void log_builtin_warning(const char *module, const char *fmt, ...); static void log_builtin_format( - unsigned int msg_level, const char *module, const char *fmt, va_list ap); + enum log_level msg_level, const char *module, const char *fmt, va_list ap); #define IMPLEMENT_SINK(name, msg_level) \ static void name(const char *module, const char *fmt, ...) \ @@ -29,16 +29,16 @@ static void log_builtin_format( va_end(ap); \ } -IMPLEMENT_SINK(log_builtin_info, 2) -IMPLEMENT_SINK(log_builtin_misc, 3) -IMPLEMENT_SINK(log_builtin_warning, 1) +IMPLEMENT_SINK(log_builtin_info, LOG_LEVEL_INFO) +IMPLEMENT_SINK(log_builtin_misc, LOG_LEVEL_MISC) +IMPLEMENT_SINK(log_builtin_warning, LOG_LEVEL_WARNING) static void log_builtin_fatal(const char *module, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - log_builtin_format(0, module, fmt, ap); + log_builtin_format(LOG_LEVEL_FATAL, module, fmt, ap); va_end(ap); DebugBreak(); @@ -46,7 +46,7 @@ static void log_builtin_fatal(const char *module, const char *fmt, ...) } static void log_builtin_format( - unsigned int msg_level, const char *module, const char *fmt, va_list ap) + enum log_level msg_level, const char *module, const char *fmt, va_list ap) { static const char chars[] = "FWIM"; @@ -55,13 +55,11 @@ static void log_builtin_format( char msg[65536]; int result; - str_vformat(msg, sizeof(msg), fmt, ap); - result = str_format( - line, sizeof(line), "%c:%s: %s\n", chars[msg_level], module, msg); - - // TODO move this up because there is no reason to format and do all the - // above if disabled if (msg_level <= log_level) { + str_vformat(msg, sizeof(msg), fmt, ap); + result = str_format( + line, sizeof(line), "%c:%s: %s\n", chars[msg_level], module, msg); + log_writer(log_writer_ctx, line, result); } } @@ -98,7 +96,7 @@ void log_to_writer(log_writer_t writer, void *ctx) } } -void log_set_level(unsigned int new_level) +void log_set_level(enum log_level new_level) { log_level = new_level; } @@ -133,4 +131,4 @@ log_formatter_t log_impl_info = log_builtin_info; log_formatter_t log_impl_warning = log_builtin_warning; log_formatter_t log_impl_fatal = log_builtin_fatal; static log_writer_t log_writer = log_writer_null; -static unsigned int log_level = 4; +static enum log_level log_level = LOG_LEVEL_MISC; diff --git a/src/main/util/log.h b/src/main/util/log.h index a60cdb7..47603fa 100644 --- a/src/main/util/log.h +++ b/src/main/util/log.h @@ -59,6 +59,13 @@ extern log_formatter_t log_impl_info; extern log_formatter_t log_impl_warning; extern log_formatter_t log_impl_fatal; +enum log_level { + LOG_LEVEL_FATAL = 0, + LOG_LEVEL_WARNING = 1, + LOG_LEVEL_INFO = 2, + LOG_LEVEL_MISC = 3, +}; + void log_assert_body(const char *file, int line, const char *function); void log_to_external( log_formatter_t misc, @@ -67,7 +74,7 @@ void log_to_external( log_formatter_t fatal); void log_to_writer(log_writer_t writer, void *ctx); -void log_set_level(unsigned int new_level); +void log_set_level(enum log_level new_level); /* I tried to make this API match the function signature of the AVS log writer callback, but then the signature changed and the explicit line breaks