From dc9e9c06421eb9156baf425ac14ec8f50cb8b5ae Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Mon, 14 Apr 2025 17:40:07 +0200 Subject: Update logging --- src/lua/log.c | 31 +++++++++++-------------------- src/lua/log.h | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 src/lua/log.h (limited to 'src/lua') diff --git a/src/lua/log.c b/src/lua/log.c index b49c003..6811ec3 100644 --- a/src/lua/log.c +++ b/src/lua/log.c @@ -2,7 +2,6 @@ #include #include "log.h" -#include "../any_log.h" static lua_CFunction lua_log_getinfo = NULL; @@ -10,15 +9,15 @@ static lua_CFunction lua_log_stringformat = NULL; static lua_CFunction lua_log_tostring = NULL; -static inline void lua_log_debuginfo(lua_State *state, const char **source, const char **func, - char buffer[], size_t size) +static inline void lua_log_debuginfo(lua_State *state, char buffer[], size_t size) { lua_pushcfunction(state, lua_log_getinfo); // Ignore the first frame (the c function itself) lua_pushnumber(state, 2); + // S: source, l: currentline, n: name - lua_pushstring(state, "Sln"); + lua_pushstring(state, "Sl"); if (lua_pcall(state, 2, 1, 0) == LUA_OK) { lua_getfield(state, -1, "short_src"); @@ -29,16 +28,10 @@ static inline void lua_log_debuginfo(lua_State *state, const char **source, cons if (!strncmp(shortsrc, "[string ", 8)) { int length = strlen(shortsrc); - snprintf(buffer, size, "lua %.*s", length - 9, shortsrc + 8); + snprintf(buffer, size, "%.*s", length - 9, shortsrc + 8); } else { - snprintf(buffer, size, "lua %s:%d", shortsrc, line); + snprintf(buffer, size, "%s:%d", shortsrc, line); } - - lua_getfield(state, -3, "name"); - const char *name = lua_tostring(state, -1); - - *source = buffer; - *func = name ? name : "..."; } else { log_trace("Failed to retrieve Lua debug information"); } @@ -61,11 +54,10 @@ 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 *source = "?", *func = "?"; - char buffer[4096]; - lua_log_debuginfo(state, &source, &func, buffer, sizeof(buffer)); + char buffer[4096] = { "lua?" }; + lua_log_debuginfo(state, buffer, sizeof(buffer)); - any_log_format(level, source, func, "%s", message); + any_log_format(level, "lua_api", buffer, "%s", message); return 0; } @@ -113,11 +105,10 @@ 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 *source = "?", *func = "?"; - char buffer[4096]; - lua_log_debuginfo(state, &source, &func, buffer, sizeof(buffer)); + char buffer[4096] = { "lua?" }; + lua_log_debuginfo(state, buffer, sizeof(buffer)); - any_log_value(level, source, func, message, + any_log_value(level, "lua_api", buffer, message, "g:value", ANY_LOG_FORMATTER(lua_print_value), state, NULL); return 0; diff --git a/src/lua/log.h b/src/lua/log.h new file mode 100644 index 0000000..32c235b --- /dev/null +++ b/src/lua/log.h @@ -0,0 +1,15 @@ +#ifndef COMET_LUA_LOG_H +#define COMET_LUA_LOG_H + +#include "api.h" +#include "../log.h" + +// Print the first value on the stack +// +void lua_print_value(FILE *stream, lua_State *state); + +void lua_print_value_at(FILE *stream, lua_State *state, int index); + +int lua_log_library(lua_State *state); + +#endif -- cgit v1.2.3