In Lua, how can you print the name of the current function, like the C99 __func__ identifier?

前端 未结 7 1783
别那么骄傲
别那么骄傲 2021-02-13 03:49

Something like this:

function foo()
    print( __func__ )
   ...
end

How can it be done?

7条回答
  •  Happy的楠姐
    2021-02-13 04:13

    function __FILE__() return debug.getinfo(2, 'S').source end
    function __LINE__() return debug.getinfo(2, 'l').currentline end
    function __FUNC__() return debug.getinfo(2, 'n').name end
    
    function printlinefilefunc()
        print("Line at "..__LINE__()..", FILE at "..__FILE__()..", in func: "..__FUNC__())
    end
    

    Output:

    Line at 8, FILE at @./andydebug.lua, in func: printlinefilefunc

提交回复
热议问题