6

In Lua I have a function called utils.debug() and what I would like to do is 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 throughout my Lua code to help me debug. As a result, I would like my C++ code to know where in the Lua code the utils.debug() was called from. I looked into lua_Debug and lua_getinfo and they seem pretty close to what I want, but I'm missing a piece:

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 what the lua_Debug struct is for or is there another facility or method I should use to do this?

2
  • 1
    fyi lua has a support mailing list you could also try if you dont get a satistfactory answer here. Commented Feb 10, 2013 at 16:09
  • 1
    I'm not clear on exactly what you're asking. Are you asking how to use lua_getinfo? Because the manual has pretty good documentation on what it does. Commented Feb 10, 2013 at 19:23

1 Answer 1

9

This is what I use to produce a Lua stack trace:

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 info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.