开发者

lua equiv of __LINE__ and __FILE__?

开发者 https://www.devze.com 2022-12-29 03:30 出处:网络
I really like C\'s __LINE__开发者_运维百科 and __FILE__ ... does lua provide somethingsimilar? (I find it useful for tracking down printf\'s ... to know which file and which line the message comes fro

I really like C's __LINE__开发者_运维百科 and __FILE__ ... does lua provide something similar? (I find it useful for tracking down printf's ... to know which file and which line the message comes from).

Thanks!


function __FILE__() return debug.getinfo(2,'S').source end
function __LINE__() return debug.getinfo(2, 'l').currentline end

Untested, credit goes here.


I use something like this for getting the line number from the c side:

int lua_getline(lua_State* L, int level) {
    lua_Debug ar;
    lua_getstack(L, level, &ar);
    lua_getinfo(L, "l", &ar);
    return ar.currentline;
}

Calling lua_getinfo with "lS" will fill the source field of the lua_Debug struct although it may not always be a file name IIRC.

0

精彩评论

暂无评论...
验证码 换一张
取 消