本文参考了 这篇博客 Lua语法小贴士(十一)debug库_凯奥斯的博客-CSDN博客_debug.sethook
当执行lua脚本时,为了跟踪调试当前的运行情况,可以通过debug.sethook和debug.getinfo这2个函数来实现,例子如下:
function tracelog(event,prm)
local dbgStr = event
local info = debug.getinfo(2)
for k,v in pairs(info) do
dbgStr = dbgStr..","..k..":"..tostring(v)
end
print(dbgStr)
end
function test(t)
for k,v in pairs(t) do
print(k..":"..v)
end
end
debug.sethook(tracelog,"l") --设置钩子
test({"a","b","c"})
debug.sethook() --卸载钩子