核心思路是用debug.sethook监听lua脚本中函数调用
local file_dir = Application.dataPath .. "/logs/"
local file_path = file_dir .. os.date("%Y-%m-%d.txt", os.time())
local function writelog(str_log)
trycatch(function()
local sw
if not System.IO.Directory.Exists(file_dir) then
System.IO.Directory.CreateDirectory(file_dir)
end
if not System.IO.File.Exists(file_path) then
sw = System.IO.File.CreateText(file_path)
else
sw = System.IO.File.AppendText(file_path)
end
sw:WriteLine(CJson_ToString(str_log))
sw:Flush()
sw:Close()
end,
function()
warn("lua vision low")
end)
end
debug.sethook(
function(event, line)
writelog(debug.getinfo(2).short_src .. ":" .. line)
end
, "l")
把上面代码随便贴如到lua脚本.运行工程,可查看log日志.会发现循环调用的位置.
好用啊!!! 记录下