improve logging from scripts

This commit is contained in:
asrael 2025-12-03 09:41:33 -06:00
parent 3313c800a9
commit 4587ba7266
7 changed files with 99 additions and 60 deletions

View file

@ -21,25 +21,34 @@ function core.get_height()
return C.pxl8_gfx_get_height(core.gfx)
end
function core.info(msg)
C.pxl8_lua_info(msg)
local function is_user_script(info)
local src = info and info.short_src
return src and (src:match("%.fnl$") or src:match("%.lua$"))
end
function core.warn(msg)
C.pxl8_lua_warn(msg)
local function get_caller_info()
for level = 2, 10 do
local info = debug.getinfo(level, "Sl")
if not info then break end
if is_user_script(info) then
return info.short_src, info.currentline or 0
end
end
return "?", 0
end
function core.error(msg)
C.pxl8_lua_error(msg)
local function make_logger(level)
return function(msg)
local src, line = get_caller_info()
C.pxl8_lua_log(level, src, line, msg)
end
end
function core.debug(msg)
C.pxl8_lua_debug(msg)
end
function core.trace(msg)
C.pxl8_lua_trace(msg)
end
core.info = make_logger(0)
core.warn = make_logger(1)
core.error = make_logger(2)
core.debug = make_logger(3)
core.trace = make_logger(4)
function core.quit()
C.pxl8_set_running(core.sys, false)