Lua 如何实现表到函数的跟踪函数?
我有一张这样的桌子Lua 如何实现表到函数的跟踪函数?,lua,luajit,Lua,Luajit,我有一张这样的桌子 local ftable = { getPinId = app.getPinId } ftable被传递给另一个函数,该函数将其导出为RPC接口。 这是可行的,但现在我想将函数调用跟踪添加到日志文件中 简单的方法是 local ftable = { getPinId = function(...) print("getPinId") app.getPinId(...) end } 但是,这并不特别好。 我想说的是: local trace = function(f
local ftable = {
getPinId = app.getPinId
}
ftable被传递给另一个函数,该函数将其导出为RPC接口。
这是可行的,但现在我想将函数调用跟踪添加到日志文件中
简单的方法是
local ftable = {
getPinId = function(...) print("getPinId") app.getPinId(...) end
}
但是,这并不特别好。
我想说的是:
local trace = function(func, ...)
return function(...) print(func) func(...) end
end
local ftable = {
getPinId = trace(app.getPinId)
}
但这并没有产生理想的结果。没有传递参数
另一种选择是使用如下图元表:
local ftable = {}
setmetatable(ftable, {
__index = function(_, k)
printf("Call: app.%s\n", k) return app[k] end
})
这很有效。但是如果可能的话,我也希望能够打印传递的参数
有什么建议吗?
如果有什么不同的话,我只使用luajit。使用_调用元方法:
M = { __call =
function (t,...) print("calling ",t.name,...) return t.func(...) end
}
trace = function(func,name)
return setmetatable({func=func,name=name},M)
end
function f(...)
print("in f",...)
end
g=trace(f,"f")
g(10,20,30)
在Lua中包装函数调用很容易:
local function wrap( f )
local function after( ... )
-- code to execute *after* function call to f
print( "return values:", ... )
return ...
end
return function( ... )
-- code to execute *before* function call to f
print( "arguments:", ... )
return after( f( ... ) )
end
end
local function f( a, b, c )
return a+b, c-a
end
local f_wrapped = wrap( f )
f_wrapped( 1, 2, 3 )
输出为:
arguments: 1 2 3
return values: 3 2
calling ftable.getPinId: 1 2 3
ftable.getPinId returns: x 1 2 3
calling ftable.nested.getPinId: 2 3 4
ftable.nested.getPinId returns: y 2 3 4
记录/跟踪的一个问题是Lua值(包括函数)本身没有名称。调试库通过检查函数的调用方式或存储位置,试图为函数找到合适的名称,但如果您想确定,必须自己提供名称。但是,如果函数存储在(嵌套的)表中(如注释中所示),则可以编写一个函数来迭代嵌套的表,并使用表键作为名称包装它找到的所有函数:
local function trace( name, value )
local t = type( value )
if t == "function" then -- do the wrapping
local function after( ... )
print( name.." returns:", ... )
return ...
end
return function( ... )
print( "calling "..name..":", ... )
return after( value( ... ) )
end
elseif t == "table" then -- recurse into subtables
local copy = nil
for k,v in pairs( value ) do
local nv = trace( name.."."..tostring( k ), v )
if nv ~= v then
copy = copy or setmetatable( {}, { __index = value } )
copy[ k ] = nv
end
end
return copy or value
else -- other values are ignored (returned as is)
return value
end
end
local ftable = {
getPinId = function( ... ) return "x", ... end,
nested = {
getPinId = function( ... ) return "y", ... end
}
}
local ftableTraced = trace( "ftable", ftable )
ftableTraced.getPinId( 1, 2, 3 )
ftableTraced.nested.getPinId( 2, 3, 4 )
输出为:
arguments: 1 2 3
return values: 3 2
calling ftable.getPinId: 1 2 3
ftable.getPinId returns: x 1 2 3
calling ftable.nested.getPinId: 2 3 4
ftable.nested.getPinId returns: y 2 3 4
需要注意的一些事项:
您的代码应该可以工作,但它将打印函数的地址,而不是名称。请给出一个您报告的失败示例。没错,我尝试了debug.getinfo函数,但没有帮助。它将其视为一个匿名函数。我想我可以把名字传过去。现在我使用的是metatable方法,它大大缩短了代码。Lua函数和所有值一样,没有名称。这就是@lhf在其答案中将字符串与函数值关联的原因。使用metamethod\uuu index方法非常方便,因为我不需要显式地在每个方法中键入。有没有办法把两者结合起来。i、 e.用它添加一个调用方法?@Matt,试试
setmetatable(ftable,{{uuu index=function(t,k)local f=trace(app[k],k);t[k]=f;return f end})
.hmm,不确定为什么,但在我的例子中,这不起作用。我的表格有点复杂。它包括表中的表,但我将其附加到表中的表。哦,好吧,我会留到另一天再弄清楚的。