summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorstefonzo <stevester.robinson@gmail.com>2022-07-10 20:01:24 -0500
committerstefonzo <stevester.robinson@gmail.com>2022-07-10 20:01:24 -0500
commit261e3f991221fbad6bbf262f5e65b773e4b6c73e (patch)
treeb8b8745d9c5a3aa7775a1d12ed2293f30a18bafe /src
parentde97d73a33ee3c1e2fe325b0cff90f079519bb36 (diff)
adds honey binding for honey.log.fatal
Diffstat (limited to 'src')
-rw-r--r--src/logging/logging.c27
-rw-r--r--src/logging/logging.test.c44
2 files changed, 69 insertions, 2 deletions
diff --git a/src/logging/logging.c b/src/logging/logging.c
index 255da4c..371066e 100644
--- a/src/logging/logging.c
+++ b/src/logging/logging.c
@@ -8,12 +8,37 @@ void honey_set_log_level(int level)
_honey_log_level = level;
}
+int log_fatal(lua_State *L)
+{
+ //validate arguments
+ int count = lua_gettop(L);
+ if (count == 0) {
+ hs_throw_error(L, "no arguments provided");
+ }
+ if (!lua_isstring(L, 1)) {
+ hs_throw_error(L, "first argument must be a string");
+ }
+ lua_getglobal(L, "string");
+ if (lua_isnil(L, -1)) hs_throw_error(L, "'string' table is nil!");
+ lua_getfield(L, -1, "format"); //pushed string.format (function) to the lua stack
+ for (int i = 1; i <= count; i++) {
+ lua_pushvalue(L, i);
+ }
+ lua_call(L, count, 1);
+ const char *str = lua_tostring(L, -1); //getting result into a string
+ honey_log(HONEY_FATAL, "[FATAL] %s\n", str);
+ lua_pop(L, 2); //cleaned up stack
+ return 0;
+}
void honey_log(int level, const char *fmt, ...) {
+
if (level > _honey_log_level) return;
-
+
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
+
+
diff --git a/src/logging/logging.test.c b/src/logging/logging.test.c
index 7e53074..01c8b20 100644
--- a/src/logging/logging.test.c
+++ b/src/logging/logging.test.c
@@ -1,7 +1,10 @@
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
-
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+#include <honeysuckle.h>
#include "test/honey-test.h"
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -70,11 +73,14 @@ void clean_mock(lily_mock_t **m)
void level_fatal_log_fatal_succeeds();
void level_neg_log_fatal_fails();
+void test_honey_log_fatal();
+void test_honey_log_fatal_fail();
void suite_logging()
{
lily_run_test(level_neg_log_fatal_fails);
lily_run_test(level_fatal_log_fatal_succeeds);
+ lily_run_test(test_honey_log_fatal);
CLEAN_MOCK(mock_vfprintf);
}
@@ -117,3 +123,39 @@ void level_neg_log_fatal_fails()
honey_log_fatal("some message");
lily_assert_int_equal(mock_vfprintf_data->n_calls, 0);
}
+
+//error logging test functions
+void test_honey_log_fatal()
+{
+ USE_MOCK(mock_vfprintf);
+ //first test (with no error surpressions)
+ lua_State *L = luaL_newstate();
+ luaL_openlibs(L);
+ lua_pushcfunction(L, log_fatal); //function is on the stack
+ lua_pushstring(L, "fatal error test");
+ int result = hs_call(L, 1, 0);
+ if (result != 0) {
+ const char *error = lua_tostring(L, -1);
+ fprintf(stderr, "%s", error);
+ }
+ lily_assert_int_equal(result, 0); //testing that error function got string pushed to it
+ lily_assert_int_equal(mock_vfprintf_data->n_calls, 1); //testing that error printing function works
+}
+
+void test_honey_log_fatal_fail()
+{
+ USE_MOCK(mock_vfprintf);
+ honey_set_log_level(-1);
+ lua_State *L = luaL_newstate();
+ luaL_openlibs(L);
+ lua_pushcfunction(L, log_fatal);
+ lua_pushstring(L, "fatal error test");
+ int result = hs_call(L, 1, 0);
+ if (result != 0) {
+ const char *error = lua_tostring(L, -1);
+ fprintf(stderr, "%s", error);
+ }
+ lily_assert_int_equal(result, 0);
+ lily_assert_int_equal(mock_vfprintf_data->n_calls, 0);
+}
+