aboutsummaryrefslogtreecommitdiff
path: root/src/Logger/LuaLogger.c
diff options
context:
space:
mode:
authorfrosty <gabriel@bwaaa.monster>2026-09-16 16:32:06 -0400
committerfrosty <gabriel@bwaaa.monster>2026-09-16 16:32:06 -0400
commit4c8d764d171ec77ff626cec273c44ec7e7a995fc (patch)
treedc36193e611ece53c8f7e74d1dea71f8d9e7004e /src/Logger/LuaLogger.c
downloadyapssg-4c8d764d171ec77ff626cec273c44ec7e7a995fc.tar.gz
init: initHEADmaster
Diffstat (limited to 'src/Logger/LuaLogger.c')
-rw-r--r--src/Logger/LuaLogger.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/Logger/LuaLogger.c b/src/Logger/LuaLogger.c
new file mode 100644
index 0000000..5739791
--- /dev/null
+++ b/src/Logger/LuaLogger.c
@@ -0,0 +1,60 @@
+#include "Logger/LuaLogger.h"
+
+#include "Logger/Logger.h"
+
+#include <lauxlib.h>
+#include <lua.h>
+
+static void add_value(lua_State *lua, luaL_Buffer *buffer, int index) {
+ int type = lua_type(lua, index);
+
+ if (type == LUA_TSTRING || type == LUA_TNUMBER) {
+ luaL_addstring(buffer, lua_tostring(lua, index));
+ } else if (type == LUA_TBOOLEAN) {
+ luaL_addstring(buffer, lua_toboolean(lua, index) ? "true" : "false");
+ } else if (type == LUA_TNIL) {
+ luaL_addstring(buffer, "nil");
+ } else {
+ luaL_addchar(buffer, '<');
+ luaL_addstring(buffer, lua_typename(lua, type));
+ luaL_addchar(buffer, '>');
+ }
+}
+
+static int log_from_lua(lua_State *lua) {
+ LogLevel level = (LogLevel)lua_tointeger(lua, lua_upvalueindex(1));
+ int argument_count = lua_gettop(lua);
+ int index;
+ luaL_Buffer buffer;
+
+ if (!logger_is_enabled(level)) {
+ return 0;
+ }
+
+ luaL_buffinit(lua, &buffer);
+ for (index = 1; index <= argument_count; ++index) {
+ if (index > 1) {
+ luaL_addchar(&buffer, '\t');
+ }
+ add_value(lua, &buffer, index);
+ }
+ luaL_pushresult(&buffer);
+ logger_log(level, "%s", lua_tostring(lua, -1));
+ lua_pop(lua, 1);
+ return 0;
+}
+
+static void set_log_function(lua_State *lua, const char *name, LogLevel level) {
+ lua_pushinteger(lua, (lua_Integer)level);
+ lua_pushcclosure(lua, log_from_lua, 1);
+ lua_setfield(lua, -2, name);
+}
+
+void lua_logger_register(lua_State *lua) {
+ lua_newtable(lua);
+ set_log_function(lua, "error", LOG_LEVEL_ERROR);
+ set_log_function(lua, "warn", LOG_LEVEL_WARNING);
+ set_log_function(lua, "info", LOG_LEVEL_INFO);
+ set_log_function(lua, "debug", LOG_LEVEL_DEBUG);
+ lua_setglobal(lua, "log");
+}