summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2021-09-25 10:45:36 -0500
committersanine <sanine.not@pm.me>2021-09-25 10:45:36 -0500
commit732aca2a13ebab4f4fb3418b103b91d57e698ccb (patch)
tree05a89605a1b836c63c44a21c0c8c4f0ad17c8427
parent78e262b448e034e96aa75193a76d36cd0062daf7 (diff)
add honey_info() log function
-rw-r--r--src/logging/logging.c9
-rw-r--r--src/logging/logging.h4
-rw-r--r--src/test/logging/logging_tests.c34
3 files changed, 44 insertions, 3 deletions
diff --git a/src/logging/logging.c b/src/logging/logging.c
index 82895d4..2a63a77 100644
--- a/src/logging/logging.c
+++ b/src/logging/logging.c
@@ -7,7 +7,7 @@
struct honey_log_info_t honey_log_info;
-const char * honey_log_level_str()
+static const char * honey_log_level_str_(enum honey_log_level_t level)
{
switch(honey_log_info.log_level) {
case DEBUG:
@@ -36,6 +36,10 @@ const char * honey_log_level_str()
}
}
+const char * honey_log_level_str() {
+ return honey_log_level_str_(honey_log_info.log_level);
+}
+
void honey_log_set_level(enum honey_log_level_t level)
{
@@ -71,7 +75,8 @@ void honey_log(enum honey_log_level_t required_level,
if (honey_log_info.log_level >= required_level) {
va_list args;
va_start(args, fmt);
- fprintf(honey_log_info.log_file, prefix);
+ fprintf(honey_log_info.log_file, "%-8s",
+ 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 6589533..2d50b67 100644
--- a/src/logging/logging.h
+++ b/src/logging/logging.h
@@ -15,6 +15,7 @@ extern struct honey_log_info_t honey_log_info;
const char * honey_log_level_str();
+
void honey_log_set_level(enum honey_log_level_t level);
enum honey_log_level_t honey_log_get_level();
@@ -25,6 +26,7 @@ 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__)
+#define honey_debug(...) honey_log(DEBUG, "[DEBUG]", __VA_ARGS__)
+#define honey_info(...) honey_log(INFO, "[INFO]", __VA_ARGS__)
#endif
diff --git a/src/test/logging/logging_tests.c b/src/test/logging/logging_tests.c
index b2dacf3..f50cbfb 100644
--- a/src/test/logging/logging_tests.c
+++ b/src/test/logging/logging_tests.c
@@ -12,6 +12,7 @@
mu_test test_log_get_level();
mu_test test_log_set_defaults();
mu_test test_log_debug();
+mu_test test_log_info();
/* main suite */
@@ -20,6 +21,7 @@ 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);
}
@@ -72,6 +74,7 @@ mu_test test_log_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!");
@@ -79,3 +82,34 @@ mu_test test_log_debug()
return 0;
}
+
+
+mu_test test_log_info()
+{
+ 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_info("hello, %s!", "world");
+ fflush(stream);
+ mu_assert_streq(buffer, "");
+
+ honey_log_set_level(INFO);
+ mu_assert_equal(honey_log_get_level(), INFO);
+
+ honey_info("hello, %s!", "world");
+ fclose(stream);
+ mu_assert_streq(buffer, "[INFO] hello, world!");
+ free(buffer);
+
+ return 0;
+}