tags:

views:

121

answers:

2

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!

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

Untested, credit goes here.

ChristopheD
Note that it is generally a bad idea to name global symbols starting with double underscore. Such names are usually belong to the language implementors.
Alexander Gladysh
+1  A: 

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.

Nick