Files
UnrealLuaDemo/Script/lua/share/base/traceback.lua
T

62 lines
1.7 KiB
Lua
Raw Normal View History

local ____exports = __module.env
---
-- @module_data
local activeHandler = nil
function ____exports.SetupTracebackHandler(handler)
activeHandler = handler
end
local function DefaultDump(msg)
if UnLua ~= nil and UnLua.LogError ~= nil then
UnLua.LogError(msg)
else
print("[ERROR] " .. msg)
end
end
local function OnError(err)
local errText = tostring(err)
local fullStack = debug.traceback(errText, 2)
if activeHandler ~= nil then
if activeHandler.LogTraceback ~= nil then
pcall(activeHandler.LogTraceback, errText, fullStack)
end
if activeHandler.DumpTraceback ~= nil then
pcall(activeHandler.DumpTraceback, fullStack)
else
DefaultDump(fullStack)
end
else
DefaultDump(fullStack)
end
return fullStack
end
--- 带完整堆栈上报的安全调用(对应 gamelogic 的 tbpcall),保留全部多返回值。
function ____exports.tbpcall(fn, ...)
return xpcall(fn, OnError, ...)
end
--- 调用带 self 的对象方法并捕获异常上报堆栈。
function ____exports.tbpcallMethod(fn, selfObj, ...)
return xpcall(fn, OnError, selfObj, ...)
end
function ____exports.ReportError(message)
OnError(message)
end
function __module.StashData(env, to)
local ____has = to.__has__
if not ____has then
____has = {}
to.__has__ = ____has
end
to.activeHandler = activeHandler
____has.activeHandler = true
end
function __module.RestoreData(env, from)
local ____has = from.__has__
if not ____has then
return
end
if ____has.activeHandler then
activeHandler = from.activeHandler
end
end
function __module.UpdateImports()
end