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日常报错打印日志

本文介绍如何在Cocos2d-x项目中集成Bugly SDK进行Lua错误日志采集,包括注册Bugly用户、接入SDK、注册Lua方法及上传堆栈信息等步骤。
7659

被折叠的 条评论
为什么被折叠?



