bugly 集成 回调时上传lua堆栈信息

5 篇文章 0 订阅
5 篇文章 0 订阅

9.5-9.6

1.首先bugly版本是ios版本 而不是cocos版本 因为cocos版本没有回调函数
2.注册bugly用户 拿到 appid, 按照官方文档接入sdk ,集成方式为单独集成 而不是pods方式集成
注意:文档是在AppDelegate.mm文件接入 而cocos的启动是AppController.mm所以我们在这里接入 方式相同
3.注册lua方法 用于日常lua错误采集

在AppController.mm中加入

int reportLuaException(lua_State *L) {
    const char* msg = lua_tostring(L, 1);

    //    CrashReport::reportException(CATEGORY_LUA_EXCEPTION, type, msg, traceback, quit);
    [Bugly reportException:[NSException exceptionWithName:
                            [[NSString alloc] initWithUTF8String:msg]
                            reason:@"" userInfo:nil]];
    return 0;
}

    //注册lua方法  只要在调用一次就好

    auto engine = cocos2d::LuaEngine::getInstance();
    cocos2d::ScriptEngineManager::getInstance()->setScriptEngine(engine);
    lua_State* L = engine->getLuaStack()->getLuaState();
    lua_register(L, "buglyReportLuaException", reportLuaException);

最后在main.lua 的全局函数中调用
buglyReportLuaException


function __G__TRACKBACK__(errorMessage)
    print("----------------------------------------")
    print("LUA ERROR: " .. tostring(errorMessage) .. "\n")
    print(debug.traceback("", 2))
    buglyReportLuaException(errorMessage)  --加入接口
    print("----------------------------------------")
end

4.//崩溃时上传lua堆栈信息
现在main.lua中加入getlistvars全局方法以便输出到崩溃日志

function debug.getlistvars()
    local str = ''
    local function stradd( value,i ,j,strtype)
        if value then
            if type(value) == 'userdata' then
                str = str..strtype..'      userdata  ,'
            else
                str = str..strtype..'     "'..tostring(value)..'"   ,'
            end
        else
            str =     str..strtype..'     "'..'nil'..'"   ,'
        end
    end
    for i=2,3 do
        for j=1,3 do
            if debug.getlocal(i,j) then
                local x,y =debug.getlocal(i,j)
                stradd(x,i,j,'name')
                stradd(y,i,j,'value')
                -- str= str..'\n'
            end
                -- str= str..'--------------------\n'
        end
    end
    -- str= str..'--------------------  end\n\n'
    return str
end

然后
在AppController.mm 的bugly回调方法 attachmentForException方法中加入

#pragma mark - BuglyDelegate
- (NSString *)attachmentForException:(NSException *)exception {
    auto engine = cocos2d::LuaEngine::getInstance();
    cocos2d::ScriptEngineManager::getInstance()->setScriptEngine(engine);
    lua_State* tolua_S = engine->getLuaStack()->getLuaState();
    //得到堆栈信息
    lua_getglobal(tolua_S, "debug");//将全局table压入堆栈
    lua_getfield(tolua_S, -1, "traceback");//调用方法
    int iError = lua_pcall( tolua_S,    //VMachine
                               0,    //参数个数
                               1,    //返回值
                               0 );
    const char* sz = lua_tostring(tolua_S, -1);
    std::string str1(sz);
    //得到堆栈方法调用参数 以及值
    lua_getglobal(tolua_S, "debug");
    lua_getfield(tolua_S, -1, "getlistvars");
    int iError1 = lua_pcall( tolua_S, 0, 1, 0 );
    const char* sz1 = lua_tostring(tolua_S, -1);
    std::string sdesc(sz1);
    std::string dest2 = sdesc + str1;
    NSLog(@"bugly liqiang %@",[[NSString alloc] initWithUTF8String:dest2.c_str()]);
    return [[NSString alloc] initWithUTF8String:dest2.c_str()];
}

总结:
1.总共修改了两个文件
AppController.mm
main.lua
2.实现基于bugly的
带lua堆栈的崩溃日志输出
lua日常报错打印日志

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值