diff options
author | sanine <sanine.not@pm.me> | 2021-09-25 10:53:35 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2021-09-25 10:53:35 -0500 |
commit | 1895d16f8ad5487db25650c628f4d9eaf8542e80 (patch) | |
tree | 78d6ae10f327250b8ec4d0781a97e6bd58b83f21 | |
parent | 732aca2a13ebab4f4fb3418b103b91d57e698ccb (diff) |
add honey_warn(), honey_error(), and honey_fatal() logging macros
-rw-r--r-- | src/logging/logging.h | 3 | ||||
-rw-r--r-- | src/test/logging/logging_tests.c | 100 |
2 files changed, 103 insertions, 0 deletions
diff --git a/src/logging/logging.h b/src/logging/logging.h index 2d50b67..c8a5864 100644 --- a/src/logging/logging.h +++ b/src/logging/logging.h @@ -28,5 +28,8 @@ void honey_log(enum honey_log_level_t required_level, #define honey_debug(...) honey_log(DEBUG, "[DEBUG]", __VA_ARGS__) #define honey_info(...) honey_log(INFO, "[INFO]", __VA_ARGS__) +#define honey_warn(...) honey_log(WARN, "[WARN]", __VA_ARGS__) +#define honey_error(...) honey_log(ERROR, "[ERROR]", __VA_ARGS__) +#define honey_fatal(...) honey_log(FATAL, "[FATAL]", __VA_ARGS__) #endif diff --git a/src/test/logging/logging_tests.c b/src/test/logging/logging_tests.c index f50cbfb..e267b5d 100644 --- a/src/test/logging/logging_tests.c +++ b/src/test/logging/logging_tests.c @@ -13,6 +13,9 @@ mu_test test_log_get_level(); mu_test test_log_set_defaults(); mu_test test_log_debug(); mu_test test_log_info(); +mu_test test_log_warn(); +mu_test test_log_error(); +mu_test test_log_fatal(); /* main suite */ @@ -22,6 +25,9 @@ void honey_logging_tests() mu_run_test("get log level strings", test_log_get_level); mu_run_test("print debug message", test_log_debug); mu_run_test("print info message", test_log_info); + mu_run_test("print warning message", test_log_warn); + mu_run_test("print error message", test_log_error); + mu_run_test("print fatal error message", test_log_fatal); } @@ -113,3 +119,97 @@ mu_test test_log_info() return 0; } + + +mu_test test_log_warn() +{ + FILE *stream; + char *buffer; + size_t len; + + stream = open_memstream(&buffer, &len); + mu_assert_unequal(stream, NULL); + + 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_warn("hello, %s!", "world"); + fflush(stream); + mu_assert_streq(buffer, ""); + + honey_log_set_level(WARN); + mu_assert_equal(honey_log_get_level(), WARN); + + honey_warn("hello, %s!", "world"); + fclose(stream); + mu_assert_streq(buffer, "[WARN] hello, world!"); + free(buffer); + + return 0; +} + + +mu_test test_log_error() +{ + FILE *stream; + char *buffer; + size_t len; + + stream = open_memstream(&buffer, &len); + mu_assert_unequal(stream, NULL); + + 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_error("hello, %s!", "world"); + fflush(stream); + mu_assert_streq(buffer, ""); + + honey_log_set_level(ERROR); + mu_assert_equal(honey_log_get_level(), ERROR); + + honey_error("hello, %s!", "world"); + fclose(stream); + mu_assert_streq(buffer, "[ERROR] hello, world!"); + free(buffer); + + return 0; +} + + +mu_test test_log_fatal() +{ + FILE *stream; + char *buffer; + size_t len; + + stream = open_memstream(&buffer, &len); + mu_assert_unequal(stream, NULL); + + honey_log_set_file(stream); + mu_assert_equal(honey_log_get_file(), stream); + + honey_log_set_level(-1); + mu_assert_equal(honey_log_get_level(), -1); + + honey_fatal("hello, %s!", "world"); + fflush(stream); + // all error levels will print fatal messagesf + mu_assert_streq(buffer, "[FATAL] hello, world!"); + + honey_log_set_level(FATAL); + mu_assert_equal(honey_log_get_level(), FATAL); + + honey_fatal("hello, %s!", "world"); + fclose(stream); + mu_assert_streq(buffer, "[FATAL] hello, world![FATAL] hello, world!"); + free(buffer); + + return 0; +} |