summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-08-24 11:14:51 -0500
committersanine <sanine.not@pm.me>2022-08-24 11:14:51 -0500
commite172f568e3a04ba97cf48e20dc8ee555b4ac29dc (patch)
tree73eb302a523420c5bd3e460645398f4a12dfe5e1
parent2cb3c3df4099297b0a0554bb482e2de04fe86b5c (diff)
add logging bindings
-rw-r--r--src/logging/logging.c58
-rw-r--r--src/logging/logging.h4
-rw-r--r--src/main.c2
3 files changed, 40 insertions, 24 deletions
diff --git a/src/logging/logging.c b/src/logging/logging.c
index 111d524..f7583c0 100644
--- a/src/logging/logging.c
+++ b/src/logging/logging.c
@@ -1,38 +1,50 @@
#include <stdio.h>
#include <stdarg.h>
#include <lua.h>
+#include <honeysuckle.h>
#include "logging/logging.h"
int _honey_log_level = HONEY_WARN;
-void honey_set_log_level(int level)
+
+#define LUA_LOG(level) \
+int log_ ## level (lua_State *L) { \
+ char *msg; \
+ hs_parse_args(L, hs_str(msg)); \
+ honey_log_ ## level ("%s\n", msg); \
+ return 0; \
+}
+
+
+LUA_LOG(fatal);
+LUA_LOG(error);
+LUA_LOG(warn);
+LUA_LOG(info);
+LUA_LOG(debug);
+LUA_LOG(trace);
+
+
+void setup_logging(lua_State *L, int honey_tbl)
{
- _honey_log_level = level;
+ hs_create_table(L,
+ hs_str_cfunc("fatal", log_fatal),
+ hs_str_cfunc("error", log_error),
+ hs_str_cfunc("warn", log_warn),
+ hs_str_cfunc("info", log_info),
+ hs_str_cfunc("debug", log_debug),
+ hs_str_cfunc("trace", log_trace),
+ );
+
+ lua_setfield(L, honey_tbl, "log");
}
-int log_fatal(lua_State *L)
+
+void honey_set_log_level(int level)
{
- //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;
+ _honey_log_level = level;
}
+
void honey_log(int level, const char *fmt, ...) {
if (level > _honey_log_level) return;
@@ -42,5 +54,3 @@ void honey_log(int level, const char *fmt, ...) {
vfprintf(stderr, fmt, args);
va_end(args);
}
-
-
diff --git a/src/logging/logging.h b/src/logging/logging.h
index f829169..758d7bc 100644
--- a/src/logging/logging.h
+++ b/src/logging/logging.h
@@ -1,6 +1,8 @@
#ifndef HONEY_LOGGING_H
#define HONEY_LOGGING_H
+#include <lua.h>
+
#define HONEY_FATAL 0
#define HONEY_ERROR 1
#define HONEY_WARN 2
@@ -10,6 +12,8 @@
extern int _honey_log_level;
+void setup_logging(lua_State *L, int honey_tbl);
+
void honey_set_log_level(int level);
void honey_log(int level, const char *fmt, ...);
diff --git a/src/main.c b/src/main.c
index 304d91d..f73da4c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,6 +6,7 @@
#include "image/image.h"
#include "glm/glm.h"
#include "options/options.h"
+#include "logging/logging.h"
int main(int argc, char **argv)
@@ -27,6 +28,7 @@ int main(int argc, char **argv)
setup_window(L, honey_index);
setup_image(L, honey_index);
setup_glm(L, honey_index);
+ setup_logging(L, honey_index);
lua_setglobal(L, "honey");
/* load main script */