tags:

views:

73

answers:

2

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?

+2  A: 

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.

lhf
Thank you, I'll experiment with your recommendations. Related to this I see some references on mailing lists to "pcall and coroutines don't get along". I do use coroutines, should I use coxpcall and copcall?
proFromDover
+1  A: 

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 )
daurnimator