微博以前的crash策略是log批量上传。。。话不多说,上代码,iOS crash 主要分为linux信号异常中断与普通异常捕获。。。
- (void)registerHandlerWithAppId:(NSString *)appId {
_appId = appId;
// 1 linux错误信号捕获
signal(SIGHUP, SignalHandler);
signal(SIGINT, SignalHandler);
signal(SIGQUIT, SignalHandler);
signal(SIGABRT, SignalHandler);
signal(SIGILL, SignalHandler);
signal(SIGSEGV, SignalHandler);
signal(SIGFPE, SignalHandler);
signal(SIGBUS, SignalHandler);
signal(SIGPIPE, SignalHandler);
// 2 objective-c未捕获异常的捕获
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
}
#pragma mark - handlers
void SignalHandler(int code) {
NSMutableString *mstr = [[NSMutableString alloc] init];
[mstr appendString:@"Stack:\n"];
void *callstack[128];
int i, frames = backtrace(callstack, 128);
char **strs = backtrace_symbols(callstack, frames);
for (i = 0; i < frames; ++i) {
[mstr appendFormat:@"%s\n", strs[i]];
}
CrashObject *crashObj = [[CrashObject alloc] init];
crashObj.name = @"signal caused crash";
crashObj.appId = instance.appId;
NSString *content =
[NSString stringWithFormat:@"%@---call stack:%@", @"signal", mstr];
crashObj.content = [instance removeSpaceAndNewline:content];
NSDictionary *sysDic = [instance getSysDic];
crashObj.versionCode = [sysDic objectForKey:@"CFBundleShortVersionString"];
crashObj.systemVersion = [[UIDevice currentDevice] systemVersion];
crashObj.model = [[UIDevice currentDevice] model];
crashObj.crashId = [instance getunixTimestamp];
crashObj.timeStap = [instance getCurrentTimes];
[instance appendNewCrash:crashObj];
}
void UncaughtExceptionHandler(NSException *exception) {
NSArray *arr = [exception callStackSymbols]; //得到当前调用栈信息
NSString *reason = [exception reason]; //非常重要,就是崩溃的原因
NSString *name = [exception name]; //异常类型
CrashObject *crashObj = [[CrashObject alloc] init];
crashObj.name = name;
crashObj.appId = instance.appId;
NSString *content =
[NSString stringWithFormat:@"%@---call stack:%@", reason,
[instance jsonStringFromArray:arr]];
crashObj.content = content;
NSDictionary *sysDic = [instance getSysDic];
crashObj.versionCode = [sysDic objectForKey:@"CFBundleShortVersionString"];
crashObj.systemVersion = [[UIDevice currentDevice] systemVersion];
crashObj.model = [[UIDevice currentDevice] model];
crashObj.crashId = [instance getunixTimestamp];
crashObj.timeStap = [instance getCurrentTimes];
[instance appendNewCrash:crashObj];
}