From 0aed18911e27b80a4dbe1a252f9485382a4073c7 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sun, 13 Apr 2025 03:37:01 +0200 Subject: Handle formatted string in log --- example.lua | 6 ++++++ src/comet.c | 16 ++++------------ src/lua/log.c | 33 ++++++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/example.lua b/example.lua index 46829c2..cba5308 100644 --- a/example.lua +++ b/example.lua @@ -5,4 +5,10 @@ function maybe() return log.panic("it does") end +log.info("%d sus", 10000) + maybe() + +log.info("%d BIGGER %f?", 0-1, 0/0) + +log.warn("%u %d","a") diff --git a/src/comet.c b/src/comet.c index 70c25ff..e8dc5e5 100644 --- a/src/comet.c +++ b/src/comet.c @@ -78,18 +78,10 @@ int main(int argc, char **argv) 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"); + if (luaL_dofile(lua.state, "example.lua") != LUA_OK) { + const char *error_message = lua_tostring(lua.state, -1); + log_error("Lua script failed: %s", error_message); + } const char *config_path = "comet.conf"; FILE *config_file = fopen(config_path, "rb"); diff --git a/src/lua/log.c b/src/lua/log.c index d02775c..4bf4e16 100644 --- a/src/lua/log.c +++ b/src/lua/log.c @@ -4,8 +4,20 @@ #include "api.h" #include "../any_log.h" +static lua_CFunction lua_log_stringformat = NULL; + static int lua_log_format(lua_State *state, any_log_level_t level) { - const char *value = luaL_checkstring(state, 1); + + // Use string.format to print safely + // + int n_args = lua_gettop(state); + lua_pushcfunction(state, lua_log_stringformat); + + for (int i = 1; i <= n_args; ++i) + lua_pushvalue(state, i); + + lua_call(state, n_args, 1); + const char *message = lua_tostring(state, -1); lua_getglobal(state, "debug"); lua_getfield(state, -1, "getinfo"); @@ -15,8 +27,8 @@ static int lua_log_format(lua_State *state, any_log_level_t level) { // S: source, l: currentline, n: name lua_pushstring(state, "Sln"); - const char *source = "x", *func = "y"; - char buffer[4096] = { 0 }; + const char *source = "?", *func = "?"; + char buffer[4096]; if (lua_pcall(state, 2, 1, 0) == LUA_OK) { @@ -33,18 +45,15 @@ static int lua_log_format(lua_State *state, any_log_level_t level) { 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; + source = buffer; + func = name ? name : "..."; } lua_pop(state, 3); - any_log_format(level, source, func, "%s", value); + any_log_format(level, source, func, "%s", message); return 0; } @@ -92,6 +101,12 @@ int lua_log_library(lua_State *state) { "trace", lua_log_trace }, { NULL, NULL }, }; + + lua_getglobal(state, "string"); + lua_getfield(state, -1, "format"); + lua_log_stringformat = lua_tocfunction(state, -1); + lua_pop(state, 2); + luaL_newlib(state, library); return 1; } -- cgit v1.2.3