Unity tolua 堆栈日志补充

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/akof1314/article/details/81319883

在 Unity 中集成 tolua 框架,业务逻辑在 lua 中进行编写,日志的输出也由 lua 传递到 unity,将日志进行封装强化可以更直观的排查开发过程中的问题。

Lua

日志模块处理

--输出日志--
function log(str)
    if 1 >= logDebugLevel then
        Util.Log(traceLast(str, 3))
    end
end

--错误日志--
function error(str, lev)
    if 4 >= logDebugLevel then
        Util.LogError(traceLast(str, lev or 3))
    end
end

--警告日志--
function warn(str)
    if 3 >= logDebugLevel then
        Util.LogWarning(traceLast(str, 3))
    end
end

function traceLast(str, lev)
    local info = debug.getinfo(lev, "Sl")
    if not info then
        return str
    end
    return string.format("[%s:%d] %s", info.short_src, info.currentline, str)
end

异常捕获

function trycatch(func, ...)
    if EditorTest then
        local args = { paramCount = select('#', ...), ... }
        local func2 = function() func(unpack(args, 1, args.paramCount)) end
        local ret, errMessage = xpcall(func2, debug.traceback)
        if not ret then
            error("ret:" ..(ret and "true" or "false") .. " errMessage:" ..(errMessage or "null"), 4)
        end
    else
        func(...)
    end
end

C#

调用c#函数出错的堆栈,修改 LuaDLL.cs 代码

public static int toluaL_exception(IntPtr L, Exception e)
{
    LuaException.luaStack = new LuaException(e.Message, e, 2);
    string msg = string.Format("{0}\r\n{1}", LuaException.luaStack.Message, LuaException.luaStack.StackTrace);
    return tolua_error(L, msg);
}

猜你喜欢

转载自blog.csdn.net/akof1314/article/details/81319883