Add file/line reporting to redbean LuaLog

This commit is contained in:
Paul Kulchenko 2021-08-17 22:24:15 -07:00
parent 4486ad5c9e
commit 1ee0c76eef

View file

@ -4816,28 +4816,20 @@ static int LuaHidePath(lua_State *L) {
} }
static int LuaLog(lua_State *L) { static int LuaLog(lua_State *L) {
int level; int level, line;
lua_Debug ar; lua_Debug ar;
const char *msg, *module; const char *msg, *module;
level = luaL_checkinteger(L, 1); level = luaL_checkinteger(L, 1);
if (LOGGABLE(level)) { if (LOGGABLE(level)) {
/*
* TODO: There needs to be some reasonable way to get the source
* filename and line number.
*/
msg = luaL_checkstring(L, 2); msg = luaL_checkstring(L, 2);
lua_getstack(L, 0, &ar); if (lua_getstack(L, 1, &ar) && lua_getinfo(L, "Sl", &ar)) {
lua_getinfo(L, "nSl", &ar); module = ar.short_src;
if (!strcmp(ar.name, "main")) { line = ar.currentline;
if (ar.source) {
module = ar.source;
} else {
module = gc(strndup(effectivepath.p, effectivepath.n));
}
} else { } else {
module = ar.name; module = gc(strndup(effectivepath.p, effectivepath.n));
line = -1;
} }
flogf(level, module, ar.currentline, NULL, "%s", msg); flogf(level, module, line, NULL, "%s", msg);
} }
return 0; return 0;
} }