From 921f1a46bbccbdf9dcadbd7017ec0c575d2bed1f Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 23 Sep 2021 23:45:47 -0500 Subject: fix debug required logging level --- src/logging/logging.h | 12 +++++++++--- src/test/logging/logging_tests.c | 6 ++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/logging/logging.h b/src/logging/logging.h index 4357ae1..8f4d174 100644 --- a/src/logging/logging.h +++ b/src/logging/logging.h @@ -5,7 +5,7 @@ struct honey_log_info { - enum { DEBUG, INFO, WARN, ERROR, FATAL } log_level; + enum { FATAL, ERROR, WARN, INFO, DEBUG } log_level; FILE *debug_out; FILE *info_out; FILE *warn_out; @@ -15,8 +15,14 @@ struct honey_log_info { const char * honey_log_level_str_(struct honey_log_info *info); -#define honey_debug_(info, ...) \ - fprintf(info.debug_out, "[DEBUG] " __VA_ARGS__) +#define honey_log_(info, required_level, prefix, ...) do { \ + if (info.log_level >= required_level) { \ + fprintf(info.debug_out, prefix " " __VA_ARGS__); \ + } \ + } while(0) + +#define honey_debug_(info, ...) \ + honey_log_(info, DEBUG, "[DEBUG]", __VA_ARGS__) #endif diff --git a/src/test/logging/logging_tests.c b/src/test/logging/logging_tests.c index ee00d8e..2d4ced9 100644 --- a/src/test/logging/logging_tests.c +++ b/src/test/logging/logging_tests.c @@ -58,7 +58,13 @@ mu_test test_log_debug() struct honey_log_info info; info.debug_out = stream; + info.log_level = FATAL; + + honey_debug_(info, "hello, world!"); + fflush(stream); + mu_assert_streq(buffer, ""); + info.log_level = DEBUG; honey_debug_(info, "hello, world!"); fclose(stream); mu_assert_streq(buffer, "[DEBUG] hello, world!"); -- cgit v1.2.1