74 lines
1.7 KiB
Lua
74 lines
1.7 KiB
Lua
local MODNAME = core.get_current_modname()
|
|
local MODPATH = core.get_modpath(MODNAME)
|
|
|
|
local mod = {
|
|
MODNAME = MODNAME,
|
|
MODPATH = MODPATH,
|
|
}
|
|
_G[MODNAME] = mod
|
|
|
|
local pack = table.pack or function(...) return {n = select("#", ...), ...} end
|
|
local unpack = unpack or table.unpack
|
|
|
|
local fmt = string.format
|
|
|
|
mod.traced_funcs = {}
|
|
|
|
local function log(dir, label, args, trace)
|
|
local fmt_args = {}
|
|
print(dump(args))
|
|
if args.n then
|
|
for i=1,args.n do
|
|
fmt_args[i] = fmt("%s", args[i])
|
|
end
|
|
end
|
|
|
|
core.log("action", fmt("%s%s(%s) %s", dir, label, table.concat(fmt_args, ','), trace))
|
|
end
|
|
|
|
local function trace(t, key)
|
|
local func = t[key]
|
|
if type(func) ~= "function" then
|
|
return nil
|
|
end
|
|
local wrapper = function(...)
|
|
local args = pack(...)
|
|
log(">", key, args, debug.traceback("==============",2))
|
|
local ret = pack(func(...))
|
|
log("<", key, ret, "")
|
|
return unpack(ret)
|
|
end
|
|
mod.traced_funcs[wrapper] = {
|
|
t = t,
|
|
key = key,
|
|
func = func,
|
|
}
|
|
t[key] = wrapper
|
|
return wrapper
|
|
end
|
|
|
|
local function untrace(func)
|
|
local orig = mod.traced_funcs[func]
|
|
orig.t[orig.key] = orig.func
|
|
return orig.func
|
|
end
|
|
|
|
mod.trace = function(first, second)
|
|
if not second then
|
|
if type(first) ~= "string" then
|
|
return nil
|
|
end
|
|
core.log("action", fmt("* Tracing global %s()", first))
|
|
return trace(_G, first)
|
|
else
|
|
if type(first) ~= "table" or type(second) ~= "string" then
|
|
return nil
|
|
end
|
|
return trace(first, second)
|
|
end
|
|
end
|
|
|
|
mod.untrace = function(func)
|
|
return untrace(func)
|
|
end
|