diff options
| author | Federico Angelilli <code@fedang.net> | 2025-08-23 00:01:31 +0200 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2025-08-23 00:01:31 +0200 |
| commit | a262dc8acd581052ee6b5d5430af69148540a843 (patch) | |
| tree | c6eafa7d46f717fdf22eb5565d62e82d80de5fe1 /src/lua | |
| parent | efdf4a4883937c8a2fad278fbd4ffc207ef981dc (diff) | |
Move to luajit and fix logsrefactor
Diffstat (limited to 'src/lua')
| -rw-r--r-- | src/lua/api.c | 18 | ||||
| -rw-r--r-- | src/lua/api.h | 4 | ||||
| -rw-r--r-- | src/lua/log.c | 29 | ||||
| -rw-r--r-- | src/lua/log.h | 4 |
4 files changed, 35 insertions, 20 deletions
diff --git a/src/lua/api.c b/src/lua/api.c index e2fdd30..47194ad 100644 --- a/src/lua/api.c +++ b/src/lua/api.c @@ -4,12 +4,26 @@ bool lua_api_init(lua_api_t *lua) { lua->state = luaL_newstate(); + if (!lua->state) + return false; + luaL_openlibs(lua->state); - luaL_requiref(lua->state, "log", lua_log_library, 1); - lua_pop(lua->state, 1); + + lua_api_require(lua, "log", lua_log_library); return true; } +void lua_api_require(lua_api_t *lua, const char *name, lua_library_t lib_fn) +{ + lua_pushcfunction(lua->state, lib_fn); + lua_pushstring(lua->state, name); + lua_call(lua->state, 1, 1); + + lua_pushvalue(lua->state, -1); + lua_setglobal(lua->state, name); + lua_pop(lua->state, 1); +} + void lua_api_close(lua_api_t *lua) { lua_close(lua->state); diff --git a/src/lua/api.h b/src/lua/api.h index e817fa1..7b5e742 100644 --- a/src/lua/api.h +++ b/src/lua/api.h @@ -10,8 +10,12 @@ typedef struct { lua_State *state; } lua_api_t; +typedef int (*lua_library_t)(lua_State *state); + bool lua_api_init(lua_api_t *lua); +void lua_api_require(lua_api_t *lua, const char *name, lua_library_t lib_fn); + void lua_api_close(lua_api_t *lua); #endif diff --git a/src/lua/log.c b/src/lua/log.c index 2b6d068..ee85369 100644 --- a/src/lua/log.c +++ b/src/lua/log.c @@ -18,12 +18,7 @@ static void lua_log_debuginfo(lua_State *state, const char **module, const char lua_pushnumber(state, 2); // S: source, l: currentline, n: name - const char *query = -#ifdef _RELEASE - "Sl"; -#else - "Sln"; -#endif + const char *query = "Sln"; lua_pushstring(state, query); if (lua_pcall(state, 2, 1, 0) == LUA_OK) { @@ -33,21 +28,21 @@ static void lua_log_debuginfo(lua_State *state, const char **module, const char lua_getfield(state, -2, "currentline"); int line = lua_tointeger(state, -1); -#ifndef _RELEASE lua_getfield(state, -3, "name"); const char *name = lua_tostring(state, -1); - if (name) - *func = name; -#endif if (!strncmp(shortsrc, "[string ", 8)) { int length = strlen(shortsrc); snprintf(buffer, size, "%.*s", length - 9, shortsrc + 8); + + *func = buffer; + *module = "lua"; } else { snprintf(buffer, size, "%s:%d", shortsrc, line); - } - *module = buffer; + *func = name ? name : "?"; + *module = buffer; + } } else { log_trace("Failed to retrieve Lua debug information"); } @@ -70,11 +65,11 @@ static int lua_log_wrapper(lua_State *state, int skip_args, any_log_level_t leve lua_call(state, n_args, 1); const char *message = lua_tostring(state, -1); - const char *module = "?", *func = ""; + const char *module, *func; char buffer[4096]; lua_log_debuginfo(state, &module, &func, buffer, sizeof(buffer)); - any_log_format(level, "lua", module, func, "%s", message); + any_log_format(level, module, func, "%s", message); return 0; } @@ -122,11 +117,11 @@ static int lua_log_wrapperv(lua_State *state, any_log_level_t level) lua_remove(state, 1); luaL_checktype(state, 1, LUA_TTABLE); - const char *module = "?", *func = ""; + const char *module, *func; char buffer[4096]; lua_log_debuginfo(state, &module, &func, buffer, sizeof(buffer)); - any_log_value(level, "lua", module, func, message, + any_log_value(level, module, func, message, "g:value", ANY_LOG_FORMATTER(lua_print_value), state, NULL); return 0; @@ -206,7 +201,7 @@ void lua_print_value_at(FILE *stream, lua_State *state, int index) first = false; if (lua_isnumber(state, -2)) { - fprintf(stream, "[%lld] = ", lua_tointeger(state, -2)); + fprintf(stream, "[%ld] = ", lua_tointeger(state, -2)); } else { fprintf(stream, "%s = ", lua_tostring(state, -2)); } diff --git a/src/lua/log.h b/src/lua/log.h index 32c235b..a736355 100644 --- a/src/lua/log.h +++ b/src/lua/log.h @@ -2,12 +2,14 @@ #define COMET_LUA_LOG_H #include "api.h" -#include "../log.h" +#include "../any_log.h" // Print the first value on the stack // void lua_print_value(FILE *stream, lua_State *state); +// Print the index-th value on the stack +// void lua_print_value_at(FILE *stream, lua_State *state, int index); int lua_log_library(lua_State *state); |
