From 92fe2dc01578533a7ecd506ada134b471380a90a Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 24 Sep 2021 00:04:34 -0500 Subject: update logging to use real functions --- src/logging/logging.c | 20 ++++++++++++++++++-- src/logging/logging.h | 18 ++++++++---------- src/test/logging/logging_tests.c | 36 +++++++++++++++++------------------- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/logging/logging.c b/src/logging/logging.c index 429f222..77c648c 100644 --- a/src/logging/logging.c +++ b/src/logging/logging.c @@ -1,11 +1,15 @@ #include +#include #include "logging/logging.h" -const char * honey_log_level_str_(struct honey_log_info *info) +struct honey_log_info_t honey_log_info; + + +const char * honey_log_level_str() { - switch(info->log_level) { + switch(honey_log_info.log_level) { case DEBUG: return "DEBUG"; break; @@ -31,3 +35,15 @@ const char * honey_log_level_str_(struct honey_log_info *info) break; } } + + +void honey_debug(const char *fmt, ...) +{ + if (honey_log_info.log_level >= DEBUG) { + va_list args; + va_start(args, fmt); + fprintf(honey_log_info.debug_out, "[DEBUG] "); + vfprintf(honey_log_info.debug_out, fmt, args); + va_end(args); + } +} diff --git a/src/logging/logging.h b/src/logging/logging.h index 8f4d174..69ea88e 100644 --- a/src/logging/logging.h +++ b/src/logging/logging.h @@ -4,7 +4,7 @@ #include -struct honey_log_info { +struct honey_log_info_t { enum { FATAL, ERROR, WARN, INFO, DEBUG } log_level; FILE *debug_out; FILE *info_out; @@ -13,16 +13,14 @@ struct honey_log_info { FILE *fatal_out; }; -const char * honey_log_level_str_(struct honey_log_info *info); +extern struct honey_log_info_t honey_log_info; -#define honey_log_(info, required_level, prefix, ...) do { \ - if (info.log_level >= required_level) { \ - fprintf(info.debug_out, prefix " " __VA_ARGS__); \ - } \ - } while(0) +const char * honey_log_level_str(); -#define honey_debug_(info, ...) \ - honey_log_(info, DEBUG, "[DEBUG]", __VA_ARGS__) - +void honey_debug(const char *fmt, ...); +void honey_info(const char *fmt, ...); +void honey_warn(const char *fmt, ...); +void honey_error(const char *fmt, ...); +void honey_fatal(const char *fmt, ...); #endif diff --git a/src/test/logging/logging_tests.c b/src/test/logging/logging_tests.c index 2d4ced9..e0aa5ef 100644 --- a/src/test/logging/logging_tests.c +++ b/src/test/logging/logging_tests.c @@ -10,6 +10,7 @@ /* test declarations */ mu_test test_log_get_level(); +mu_test test_log_set_defaults(); mu_test test_log_debug(); @@ -22,26 +23,24 @@ void honey_logging_tests() } -/* test definitions */ +/* ~~~~~~~~ test definitions ~~~~~~~~ */ mu_test test_log_get_level() { - struct honey_log_info info; + honey_log_info.log_level = DEBUG; + mu_assert_streq("DEBUG", honey_log_level_str()); - info.log_level = DEBUG; - mu_assert_streq("DEBUG", honey_log_level_str_(&info)); + honey_log_info.log_level = INFO; + mu_assert_streq("INFO", honey_log_level_str()); - info.log_level = INFO; - mu_assert_streq("INFO", honey_log_level_str_(&info)); + honey_log_info.log_level = WARN; + mu_assert_streq("WARN", honey_log_level_str()); - info.log_level = WARN; - mu_assert_streq("WARN", honey_log_level_str_(&info)); + honey_log_info.log_level = ERROR; + mu_assert_streq("ERROR", honey_log_level_str()); - info.log_level = ERROR; - mu_assert_streq("ERROR", honey_log_level_str_(&info)); - - info.log_level = FATAL; - mu_assert_streq("FATAL", honey_log_level_str_(&info)); + honey_log_info.log_level = FATAL; + mu_assert_streq("FATAL", honey_log_level_str()); return 0; } @@ -56,16 +55,15 @@ mu_test test_log_debug() stream = open_memstream(&buffer, &len); mu_assert_unequal(stream, NULL); - struct honey_log_info info; - info.debug_out = stream; - info.log_level = FATAL; + honey_log_info.debug_out = stream; + honey_log_info.log_level = FATAL; - honey_debug_(info, "hello, world!"); + honey_debug("hello, %s!", "world"); fflush(stream); mu_assert_streq(buffer, ""); - info.log_level = DEBUG; - honey_debug_(info, "hello, world!"); + honey_log_info.log_level = DEBUG; + honey_debug("hello, %s!", "world"); fclose(stream); mu_assert_streq(buffer, "[DEBUG] hello, world!"); free(buffer); -- cgit v1.2.1