how to get caller of function within a function in lua?

coronasdk, lua

Solution

`debug.getinfo(2).name` will give you the name of the calling function if it has one and it is a string. If it's an anonymous function, you'll get `nil`, if it's stored in a table using something other than a string key, you'll get `?`.

function foo() print(debug.getinfo(2).name) end

-- _G["foo"] => function name is a string
function bar() foo() end
bar() --> 'bar'

-- _G[123] => function name is a number
_G[123] = function() foo() end
_G[123]() --> '?'

-- function has no name
(function() foo() end)()  --> 'nil'

Problem

how can one get the caller of function within a function in lua? Specifically I'm looking (for debug purposes in the command output, e.g. print) the ability to log when a common function is called, with an indication of where it was called from. This could be just the filename of where it was called from for example i.e. ``` File 1 - Has commonly used function File 2 - Calls of the the file one functions ``` PS Mud - I actually get a nil when doing this - is this normal then? No way to get more info in this case: Called file; ``` SceneBase = {} function SceneBase:new(options) end return SceneBase ``` Calling file: ``` require("views.scenes.scene_base") local scene = SceneBase.new() ```

Original source