diff options
author | sanine <sanine.not@pm.me> | 2021-09-24 22:45:39 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2021-09-24 22:45:39 -0500 |
commit | 78e262b448e034e96aa75193a76d36cd0062daf7 (patch) | |
tree | 52aadee7d06727b762cf9d9a2defc1136e8ddbd3 | |
parent | 92fe2dc01578533a7ecd506ada134b471380a90a (diff) |
add getters and setters for log level/file
-rw-r--r-- | src/logging/logging.c | 37 | ||||
-rw-r--r-- | src/logging/logging.h | 26 | ||||
-rw-r--r-- | src/test/logging/logging_tests.c | 25 |
3 files changed, 65 insertions, 23 deletions
diff --git a/src/logging/logging.c b/src/logging/logging.c index 77c648c..82895d4 100644 --- a/src/logging/logging.c +++ b/src/logging/logging.c @@ -37,13 +37,42 @@ const char * honey_log_level_str() } -void honey_debug(const char *fmt, ...) +void honey_log_set_level(enum honey_log_level_t level) { - if (honey_log_info.log_level >= DEBUG) { + honey_log_info.log_level = level; +} + + +enum honey_log_level_t honey_log_get_level() +{ + return honey_log_info.log_level; +} + + +void honey_log_set_file(FILE* file) +{ + honey_log_info.log_file = file; +} + + +FILE * honey_log_get_file() +{ + return honey_log_info.log_file; +} + + +void honey_log(enum honey_log_level_t required_level, + const char *prefix, + const char *fmt, ...) +{ + if (honey_log_info.log_file == NULL) + return; + + if (honey_log_info.log_level >= required_level) { va_list args; va_start(args, fmt); - fprintf(honey_log_info.debug_out, "[DEBUG] "); - vfprintf(honey_log_info.debug_out, fmt, args); + fprintf(honey_log_info.log_file, prefix); + vfprintf(honey_log_info.log_file, fmt, args); va_end(args); } } diff --git a/src/logging/logging.h b/src/logging/logging.h index 69ea88e..6589533 100644 --- a/src/logging/logging.h +++ b/src/logging/logging.h @@ -3,24 +3,28 @@ #include <stdio.h> +enum honey_log_level_t + { FATAL, ERROR, WARN, INFO, DEBUG }; struct honey_log_info_t { - enum { FATAL, ERROR, WARN, INFO, DEBUG } log_level; - FILE *debug_out; - FILE *info_out; - FILE *warn_out; - FILE *error_out; - FILE *fatal_out; + enum honey_log_level_t log_level; + FILE *log_file; }; extern struct honey_log_info_t honey_log_info; const char * honey_log_level_str(); -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, ...); +void honey_log_set_level(enum honey_log_level_t level); +enum honey_log_level_t honey_log_get_level(); + +void honey_log_set_file(FILE *file); +FILE * honey_log_get_file(); + +void honey_log(enum honey_log_level_t required_level, + const char *prefix, + const char *fmt, ...); + +#define honey_debug(...) honey_log(DEBUG, "[DEBUG] ", __VA_ARGS__) #endif diff --git a/src/test/logging/logging_tests.c b/src/test/logging/logging_tests.c index e0aa5ef..b2dacf3 100644 --- a/src/test/logging/logging_tests.c +++ b/src/test/logging/logging_tests.c @@ -27,19 +27,24 @@ void honey_logging_tests() mu_test test_log_get_level() { - honey_log_info.log_level = DEBUG; + honey_log_set_level(DEBUG); + mu_assert_equal(DEBUG, honey_log_get_level()); mu_assert_streq("DEBUG", honey_log_level_str()); - honey_log_info.log_level = INFO; + honey_log_set_level(INFO); + mu_assert_equal(INFO, honey_log_get_level()); mu_assert_streq("INFO", honey_log_level_str()); - honey_log_info.log_level = WARN; + honey_log_set_level(WARN); + mu_assert_equal(WARN, honey_log_get_level()); mu_assert_streq("WARN", honey_log_level_str()); - honey_log_info.log_level = ERROR; + honey_log_set_level(ERROR); + mu_assert_equal(ERROR, honey_log_get_level()); mu_assert_streq("ERROR", honey_log_level_str()); - honey_log_info.log_level = FATAL; + honey_log_set_level(FATAL); + mu_assert_equal(FATAL, honey_log_get_level()); mu_assert_streq("FATAL", honey_log_level_str()); return 0; @@ -55,14 +60,18 @@ mu_test test_log_debug() stream = open_memstream(&buffer, &len); mu_assert_unequal(stream, NULL); - honey_log_info.debug_out = stream; - honey_log_info.log_level = FATAL; + honey_log_set_file(stream); + mu_assert_equal(honey_log_get_file(), stream); + + honey_log_set_level(FATAL); + mu_assert_equal(honey_log_get_level(), FATAL); honey_debug("hello, %s!", "world"); fflush(stream); mu_assert_streq(buffer, ""); - honey_log_info.log_level = DEBUG; + honey_log_set_level(DEBUG); + mu_assert_equal(honey_log_get_level(), DEBUG); honey_debug("hello, %s!", "world"); fclose(stream); mu_assert_streq(buffer, "[DEBUG] hello, world!"); |