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

25,077

Solution 1

#!/usr/bin/lua

local function myFunc()
 print(debug.getinfo(1, "n").name);
end
myFunc()

Solution 2

You can't. In lua, functions are first class variables. So they don't have names. You might as well ask "what the name of 2" is. Just because some variable was assigned the value '2' doesn't make that variable the name of 2. Likewise "someFunc" is a variable - potentially one of many - that holds a particular function.

Solution 3

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

Solution 4

While I agree with Ephraim's answer, that code will not always report the same name as pointed out by Chris Becke. When the function is assigned to another variable, the "name" would be changed.

Here is another alternative. It just uses a string to identify the function. This method solves the changing name problem, but introduces a maintenance issue. The string would need to be kept in sync with the function name with future refactorization.

function foo()
  local __func__ = "foo"
  print( __func__ )
  --...
end

Alternatively, if the location of the function is more important than the name, the following may be better. It will give a name to the function that is based on the source and line number.

function getfunctionlocation()
  local w = debug.getinfo(2, "S")
  return w.short_src..":"..w.linedefined
end

function foo()
  print(getfunctionlocation()) --> foo.lua:6
  --...
end

If the __func__ still seems better, and standard Lua is not important, then the Lua parser can be modified as it is in this example for __FILE__ and __LINE__.

Solution 5

Use the Debug Library. It provides a getinfo(func) function that returns a table with information about the function.

Share:
25,077

Related videos on Youtube

x-x
Author by

x-x

Updated on July 09, 2022

Comments

  • x-x
    x-x almost 2 years

    Something like this:

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

    How can it be done?

  • Admin
    Admin over 13 years
    in stacktraces, these functions appear as </path/to/filename.lua:$LINE>, , where $LINE is the line where the function appears.
  • Potassium Ion
    Potassium Ion almost 8 years
    Minor note (yes, I know this is an old thread): using debug.getinfo(1) or "" will work the same as debug.getinfo(1) and debug.getinfo(1) or ""
  • phil294
    phil294 almost 6 years
    how does your answer comply with the accepted answer?
  • Chris Becke
    Chris Becke over 5 years
    It doesn't. Both answers are technically correct. If - by name - you want the unique unchanging assigned name of a lua function - what FUNC provides for C programs - you can't get it. If by name you simply want the name of an variable that has the function as its value, use the debug methods to get that, remembering that variables can be reassigned, and multiple variables can contain the same value - so the "name" returned this way is neither unique nor unchanging.
  • Деян Добромиров
    Деян Добромиров over 5 years
    Why Did you vote down when you know it. I am just trying to help you ??!!
  • Potassium Ion
    Potassium Ion over 5 years
    1) This was two and a half years ago. 2) I didn’t downvote, someone else did.
  • TarmoPikaro
    TarmoPikaro about 5 years
    Depends on lua interpretter / version. debug.getinfo(2, 'S').source gave me whole source code of .lua script.
  • AF7
    AF7 over 4 years
    This currently returns "nil" in Lua 5.3.
  • Roland Illig
    Roland Illig over 3 years
    In Lua, functions are not variables. They are values that can be passed around.
  • tsh
    tsh about 3 years
    First class citizen is not a valid reason to prevent access its name. For example, in Python, you can access it by myFunc.__name__. In JavaScript, it would be myFunc.name. But in C, after linking, there is nothing about function name for you. And we all know Python and JavaScript support first class function, but not C.

Related