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

@ -25,6 +25,7 @@ struct pxl8_script {
char main_path[PXL8_MAX_PATH];
char watch_dir[PXL8_MAX_PATH];
time_t latest_mod_time;
int repl_env_ref;
bool repl_mode;
};
@ -210,12 +211,7 @@ static const char* pxl8_ffi_cdefs =
"void pxl8_set_cursor(pxl8* sys, pxl8_cursor cursor);\n"
"void pxl8_set_relative_mouse_mode(pxl8* sys, bool enabled);\n"
"void pxl8_set_running(pxl8* sys, bool running);\n"
"void pxl8_lua_debug(const char* msg);\n"
"void pxl8_lua_error(const char* msg);\n"
"void pxl8_lua_info(const char* msg);\n"
"void pxl8_lua_trace(const char* msg);\n"
"void pxl8_lua_warn(const char* msg);\n"
"\n"
"void pxl8_lua_log(int level, const char* file, int line, const char* msg);\n"
"typedef u32 pxl8_tile;\n"
"typedef struct pxl8_tilemap pxl8_tilemap;\n"
"typedef struct pxl8_tilesheet pxl8_tilesheet;\n"
@ -423,24 +419,15 @@ static const char* pxl8_ffi_cdefs =
"i32 pxl8_save_delete(pxl8_save* save, u8 slot);\n"
"const char* pxl8_save_get_directory(pxl8_save* save);\n";
void pxl8_lua_info(const char* msg) {
pxl8_info("%s", msg);
}
void pxl8_lua_warn(const char* msg) {
pxl8_warn("%s", msg);
}
void pxl8_lua_error(const char* msg) {
pxl8_error("%s", msg);
}
void pxl8_lua_debug(const char* msg) {
pxl8_debug("%s", msg);
}
void pxl8_lua_trace(const char* msg) {
pxl8_trace("%s", msg);
void pxl8_lua_log(int level, const char* file, int line, const char* msg) {
if (file && (file[0] == '?' || file[0] == '\0')) file = NULL;
switch (level) {
case 0: pxl8_log_write_info("%s", msg); break;
case 1: pxl8_log_write_warn(file, line, "%s", msg); break;
case 2: pxl8_log_write_error(file, line, "%s", msg); break;
case 3: pxl8_log_write_debug(file, line, "%s", msg); break;
case 4: pxl8_log_write_trace(file, line, "%s", msg); break;
}
}
static void pxl8_script_set_error(pxl8_script* script, const char* error) {
@ -526,15 +513,11 @@ pxl8_script* pxl8_script_create(bool repl_mode) {
lua_getglobal(script->L, "fennel");
lua_getfield(script->L, -1, "install");
if (lua_isfunction(script->L, -1)) {
// Pass options table for fennel.install()
// lambdaAsFn: removes arity checking overhead from fn/lambda
// correlate: aligns Lua line numbers with Fennel source
// useMetadata: enables docstrings (only in REPL mode for perf)
lua_newtable(script->L);
lua_pushboolean(script->L, 1);
lua_setfield(script->L, -2, "lambdaAsFn");
lua_pushboolean(script->L, 1);
lua_setfield(script->L, -2, "correlate");
lua_pushboolean(script->L, 1);
lua_setfield(script->L, -2, "lambdaAsFn");
if (script->repl_mode) {
lua_pushboolean(script->L, 1);
lua_setfield(script->L, -2, "useMetadata");
@ -573,12 +556,26 @@ pxl8_script* pxl8_script_create(bool repl_mode) {
}
script->last_error[0] = '\0';
script->repl_env_ref = LUA_NOREF;
if (script->repl_mode) {
lua_newtable(script->L);
lua_newtable(script->L);
lua_getglobal(script->L, "_G");
lua_setfield(script->L, -2, "__index");
lua_setmetatable(script->L, -2);
script->repl_env_ref = luaL_ref(script->L, LUA_REGISTRYINDEX);
}
return script;
}
void pxl8_script_destroy(pxl8_script* script) {
if (!script) return;
if (script->L) {
if (script->repl_env_ref != LUA_NOREF) {
luaL_unref(script->L, LUA_REGISTRYINDEX, script->repl_env_ref);
}
lua_close(script->L);
}
free(script);
@ -771,9 +768,15 @@ static pxl8_result pxl8_script_eval_internal(pxl8_script* script, const char* co
lua_pushstring(script->L, code);
lua_newtable(script->L);
lua_pushstring(script->L, "useMetadata");
lua_pushboolean(script->L, true);
lua_settable(script->L, -3);
lua_setfield(script->L, -2, "useMetadata");
if (repl_mode && script->repl_env_ref != LUA_NOREF) {
lua_rawgeti(script->L, LUA_REGISTRYINDEX, script->repl_env_ref);
lua_setfield(script->L, -2, "env");
lua_pushboolean(script->L, false);
lua_setfield(script->L, -2, "allowedGlobals");
}
if (lua_pcall(script->L, 2, 1, 0) != 0) {
const char* error = lua_tostring(script->L, -1);