From 1ca109e1770b56095bdf4c664f3b99b7ac93776d Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 23 Sep 2021 23:11:23 -0500 Subject: add honey_log_level_str_() --- src/logging/logging.c | 33 +++++++++++++++++++++++++++++++++ src/logging/logging.h | 10 ++++++++++ src/test/logging/logging_tests.c | 40 ++++++++++++++++++++++++++++++++++++++-- src/test/minunit.h | 17 ++++++++++------- 4 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 src/logging/logging.c create mode 100644 src/logging/logging.h (limited to 'src') diff --git a/src/logging/logging.c b/src/logging/logging.c new file mode 100644 index 0000000..429f222 --- /dev/null +++ b/src/logging/logging.c @@ -0,0 +1,33 @@ +#include + +#include "logging/logging.h" + + +const char * honey_log_level_str_(struct honey_log_info *info) +{ + switch(info->log_level) { + case DEBUG: + return "DEBUG"; + break; + + case INFO: + return "INFO"; + break; + + case WARN: + return "WARN"; + break; + + case ERROR: + return "ERROR"; + break; + + case FATAL: + return "FATAL"; + break; + + default: + return NULL; + break; + } +} diff --git a/src/logging/logging.h b/src/logging/logging.h new file mode 100644 index 0000000..87436e5 --- /dev/null +++ b/src/logging/logging.h @@ -0,0 +1,10 @@ +#ifndef HONEY_LOGGING_H +#define HONEY_LOGGING_H + +struct honey_log_info { + enum { DEBUG, INFO, WARN, ERROR, FATAL } log_level; +}; + +const char * honey_log_level_str_(struct honey_log_info *info); + +#endif diff --git a/src/test/logging/logging_tests.c b/src/test/logging/logging_tests.c index 2e92d68..2a07d30 100644 --- a/src/test/logging/logging_tests.c +++ b/src/test/logging/logging_tests.c @@ -1,6 +1,42 @@ -#include "../minunit.h" -#include "../suites.h" +#include "test/minunit.h" +#include "test/suites.h" + +#include "logging/logging.h" + + +/* test declarations */ + +mu_test test_log_get_level(); + + +/* main suite */ void honey_logging_tests() { + mu_run_test("get log level strings", test_log_get_level); +} + + +/* test definitions */ + +mu_test test_log_get_level() +{ + struct honey_log_info info; + + info.log_level = DEBUG; + mu_assert_streq("DEBUG", honey_log_level_str_(&info)); + + info.log_level = INFO; + mu_assert_streq("INFO", honey_log_level_str_(&info)); + + info.log_level = WARN; + mu_assert_streq("WARN", honey_log_level_str_(&info)); + + 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)); + + return 0; } diff --git a/src/test/minunit.h b/src/test/minunit.h index b1bfda7..ed5e57d 100644 --- a/src/test/minunit.h +++ b/src/test/minunit.h @@ -2,6 +2,7 @@ #define MINUNIT_H #include +#include #define STR_IMPL(x) #x #define STR(x) STR_IMPL(x) @@ -9,7 +10,7 @@ #define MU_INDENT " " /* minunit testing macros from /www.jera.com/techinfo/jtns/jtn002.html */ -#define mu_assert(test, message) do { if (!(test)) return message "\n" MU_INDENT "[" __FILE__ ":" STR(__LINE__) "]"; } while (0) +#define mu_assert(test, message) do { if (!(test)) return message "\n" MU_INDENT " [" __FILE__ ":" STR(__LINE__) "]"; } while (0) #define mu_assert_equal(a, b) mu_assert(a == b, "'" #a "' is not equal to '" #b "'") #define mu_assert_unequal(a, b) mu_assert(a != b, "'" #a "' is equal to '" #b "'") #define mu_assert_streq(a, b) mu_assert(strcmp(a, b) == 0, "'" #a "' is not equal to '" #b "'") @@ -22,12 +23,14 @@ tests_failed++; \ } \ } while (0) -#define mu_run_suite(suite) do { \ - int tests_run_old = tests_run; \ - printf("suite: " #suite "\n"); \ - suite(); \ - printf(MU_INDENT "ran %d tests\n\n", \ - tests_run - tests_run_old); \ +#define mu_run_suite(suite) do { \ + int run_old = tests_run; \ + int failed_old = tests_failed; \ + printf("suite: " #suite "\n"); \ + suite(); \ + printf(MU_INDENT "ran %d tests, %d failed\n\n", \ + tests_run - run_old, \ + tests_failed - failed_old); \ } while(0) #define mu_tests_finished() do { \ printf("ran %d tests, %d failed\n", tests_run, tests_failed); \ -- cgit v1.2.1