Whenever an error occurs in a Lua script, I'd like it to write the values of all local and global variables to the screen/optionally to a file - in addition to the usual stack trace.
How could I get this to be the default behavior for all errors?
Whenever an error occurs in a Lua script, I'd like it to write the values of all local and global variables to the screen/optionally to a file - in addition to the usual stack trace.
How could I get this to be the default behavior for all errors?
If you're using the standard Lua interpreter, replace debug.traceback
with your own function. If you're embedding Lua in your program, use your traceback function in lua_pcall
.
A more proper solution would be to use xpcall around your whole code.
local function myerrhandler ( errobj )
print(debug.traceback())
for k,v in pairs(_G) do print("GLOBAL:" , k,v) end
return false
end
xpcall( function ()
--Your code here
end , myerrhandler )