Lua & C ++ API gets executable information

In Lua, I have a function called utils.debug() , and I would like to use it in my Lua code as follows:

 function Foo:doSomething if (/* something */) then print("Success!") else utils.debug() end end function Foo:doSomethingElse if (/* something else */) then print("Awesome!") else utils.debug() end end 

I would like to use it in all my Lua code to help me debug. As a result, I would like my C ++ code to know where utils.debug() is called in the Lua code. I looked at lua_Debug and lua_getinfo , and they seem pretty close to what I want, but I miss the part:

 int MyLua::debug(lua_State* L) { lua_Debug ar; lua_getstack(L, 1, &ar); lua_getinfo(L, ??????, &ar); // print out relevant info from 'ar' // such as in what function it was called, line number, etc } 

Is this a lua_Debug structure for or is there another tool or method that I should use for this?

+6
source share
1 answer

This is what I use to create a trace of the Lua stack:

 lua_Debug info; int level = 0; while (lua_getstack(l, level, &info)) { lua_getinfo(l, "nSl", &info); fprintf(stderr, " [%d] %s:%d -- %s [%s]\n", level, info.short_src, info.currentline, (info.name ? info.name : "<unknown>"), info.what); ++level; } 

See the documentation for lua_getinfo for more information.

+8
source

All Articles