Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua equiv of __LINE__ and __FILE__?

Tags:

lua

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!

like image 988
anon Avatar asked May 06 '10 10:05

anon


2 Answers

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

Untested, credit goes here.

like image 196
ChristopheD Avatar answered Nov 20 '22 00:11

ChristopheD


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.

like image 20
Nick Van Brunt Avatar answered Nov 20 '22 00:11

Nick Van Brunt