Lua的基本信息调试(三)--lua

    技术2022-05-20  51

    在前面那篇文章Lua的基本信息调试(二)中,我使用了lua_getstack(L, 2, &debug);去取堆栈中的错误信息,但至于错误信息在stack中的层数并不清楚,经过信息打印发现,错误信息在stack的最深处,因此,修改函数为:

    int pcall_callback_err_fun(lua_State* L){    lua_Debug debug;

       

        //取得层数

        uint32_t level = 0;    while (lua_getstack(L, level, &debug)) {        level++;    }

        if (!level) {        return 0;    }

        lua_getstack(L, level, &debug);    lua_getinfo(L, "Sln", &debug);

        std::string err = lua_tostring(L, -1);    lua_pop(L, 1);    std::stringstream msg;    msg << debug.short_src << ":line " << debug.currentline;    if (debug.name != 0) {        msg << "(" << debug.namewhat << " " << debug.name << ")";    }

        msg << " [" << err << "]";    lua_pushstring(L, msg.str().c_str());    return 1;}

    这样我们就能准确的获得lua脚本在运行中的错误信息!


    最新回复(0)