From 91149e6c41a094bf29ed6a1aa00c0fa6c1015a24 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sun, 13 Apr 2025 02:36:18 +0200 Subject: Add log bindings for lua --- src/comet.c | 21 ++++++++++--- src/lua/api.c | 13 +++++--- src/lua/api.h | 4 +-- src/lua/log.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 src/lua/log.c (limited to 'src') diff --git a/src/comet.c b/src/comet.c index fec831e..70c25ff 100644 --- a/src/comet.c +++ b/src/comet.c @@ -65,19 +65,32 @@ int main(int argc, char **argv) { setlocale(LC_CTYPE, ""); - any_log_level_t log_level = ANY_LOG_DEBUG; + any_log_level_t log_level = ANY_LOG_INFO; if (argc != 1 && !strcmp(argv[1], "--trace")) log_level = ANY_LOG_TRACE; any_log_init(stdout, log_level); - lua_api_t lua; - lua_api_init(&lua); - config_t config; config_init(&config); log_debug("Copied default config"); + lua_api_t lua; + lua_api_init(&lua); + + + char *str = + "function sigma()\n" + " log.info('ssussy')\n" + " log.panic('az')\n" + "end\n" + "sigma()\n"; + + + (void)luaL_dostring(lua.state, "log.info('%d%n%x')"); + (void)luaL_dostring(lua.state, str); + (void)luaL_dofile(lua.state, "example.lua"); + const char *config_path = "comet.conf"; FILE *config_file = fopen(config_path, "rb"); diff --git a/src/lua/api.c b/src/lua/api.c index 5a29cca..3f68b1e 100644 --- a/src/lua/api.c +++ b/src/lua/api.c @@ -1,14 +1,17 @@ #include "api.h" +int lua_log_library(lua_State *state); -bool lua_api_init(lua_api_t *api) +bool lua_api_init(lua_api_t *lua) { - api->state = luaL_newstate(); - luaL_openlibs(api->state); + lua->state = luaL_newstate(); + luaL_openlibs(lua->state); + luaL_requiref(lua->state, "log", lua_log_library, 1); + lua_pop(lua->state, 1); return true; } -void lua_api_close(lua_api_t *api) +void lua_api_close(lua_api_t *lua) { - lua_close(api->state); + lua_close(lua->state); } diff --git a/src/lua/api.h b/src/lua/api.h index a134aa0..e817fa1 100644 --- a/src/lua/api.h +++ b/src/lua/api.h @@ -10,8 +10,8 @@ typedef struct { lua_State *state; } lua_api_t; -bool lua_api_init(lua_api_t *api); +bool lua_api_init(lua_api_t *lua); -void lua_api_close(lua_api_t *api); +void lua_api_close(lua_api_t *lua); #endif diff --git a/src/lua/log.c b/src/lua/log.c new file mode 100644 index 0000000..d02775c --- /dev/null +++ b/src/lua/log.c @@ -0,0 +1,97 @@ +#include +#include + +#include "api.h" +#include "../any_log.h" + +static int lua_log_format(lua_State *state, any_log_level_t level) { + const char *value = luaL_checkstring(state, 1); + + lua_getglobal(state, "debug"); + lua_getfield(state, -1, "getinfo"); + + // Ignore the first frame (lua_log_format itself) + lua_pushnumber(state, 2); + // S: source, l: currentline, n: name + lua_pushstring(state, "Sln"); + + const char *source = "x", *func = "y"; + char buffer[4096] = { 0 }; + + if (lua_pcall(state, 2, 1, 0) == LUA_OK) { + + lua_getfield(state, -1, "short_src"); + const char *shortsrc = lua_tostring(state, -1); + + lua_getfield(state, -2, "currentline"); + int line = lua_tointeger(state, -1); + + if (!strncmp(shortsrc, "[string ", 8)) { + int length = strlen(shortsrc); + snprintf(buffer, sizeof(buffer), "lua %.*s", length - 9, shortsrc + 8); + } else { + snprintf(buffer, sizeof(buffer), "lua %s:%d", shortsrc, line); + } + + source = buffer; + + lua_getfield(state, -3, "name"); + const char *name = lua_tostring(state, -1); + + func = name == NULL + ? "..." + : name; + } + + lua_pop(state, 3); + any_log_format(level, source, func, "%s", value); + return 0; +} + +static int lua_log_log(lua_State *state) { + const char *level = luaL_checkstring(state, 1); + lua_pop(state, 1); + any_log_level_t l = any_log_level_from_string(level); + assert(l > ANY_LOG_ALL); + return lua_log_format(state, l); +} + +static int lua_log_panic(lua_State *state) { + return lua_log_format(state, ANY_LOG_PANIC); +} + +static int lua_log_error(lua_State *state) { + return lua_log_format(state, ANY_LOG_ERROR); +} + +static int lua_log_warn(lua_State *state) { + return lua_log_format(state, ANY_LOG_WARN); +} + +static int lua_log_info(lua_State *state) { + return lua_log_format(state, ANY_LOG_INFO); +} + +static int lua_log_debug(lua_State *state) { + return lua_log_format(state, ANY_LOG_DEBUG); +} + +static int lua_log_trace(lua_State *state) { + return lua_log_format(state, ANY_LOG_TRACE); +} + +int lua_log_library(lua_State *state) +{ + static const luaL_Reg library[] = { + { "log", lua_log_log }, + { "panic", lua_log_panic }, + { "error", lua_log_error }, + { "warn", lua_log_warn }, + { "info", lua_log_info }, + { "debug", lua_log_debug }, + { "trace", lua_log_trace }, + { NULL, NULL }, + }; + luaL_newlib(state, library); + return 1; +} -- cgit v1.2.3