集成 PLCrashReporter 上传日志
PLCrashReporter 是开源的,官网地址:https://www.plcrashreporter.org/
把 CrashReporter.framework 引入集成到项目中,
implementation AppDelegate
-(void)handleCrashReport {
PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
NSData *crashData;
NSError *error;
crashData = [crashReporter loadPendingCrashReportDataAndReturnError:&error];
if (crashData == nil) {
NSLog(@"Could not load crash report: %@", error);
[crashReporter purgePendingCrashReport];
return;
}
/* CrashData 可以直接上传到服务器上,下面的代码是保存到 Document 中 */
NSArray *docPathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [docPathArray firstObject];
NSString *path = [docPath stringByAppendingString:@"/crashdata.crash"];
[crashData writeToFile:path atomically:YES];
PLCrashReport *report = [[PLCrashReport alloc] initWithData:crashData error:&error];
if (report == nil) {
NSLog(@"Could not parse crash report: %@", error);
[crashReporter purgePendingCrashReport];
return;
}
/* CrashData 还需要用 PLCrashReporter 的工具 crashutil 解析,也可以直接保存成字符串*/
NSString *humanReadable = [PLCrashReportTextFormatter stringValueForCrashReport:report withTextFormat:PLCrashReportTextFormatiOS];
NSLog(@"Report: %@", humanReadable);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
NSError *error;
if ([crashReporter hasPendingCrashReport]) {
[self handleCrashReport];
}
if (![crashReporter enableCrashReporterAndReturnError:&error]) {
NSLog(@"Warning: Could not enable crash reporter: %@", error);
}
}
可以用 PLCrashReporter 收集到 crashData,然后用 PLCrashReporter 的工具转换为正常的 .crash 文件,在 Terminal 中的命令如下(./ 是必须要带的,哪怕已经 cd 到当前文件夹):
./plcrashutil convert --format=iphone example_report.plcrash > app.crash
也可以直接用 [PLCrashReportTextFormatter stringValueForCrashReport:report withTextFormat:PLCrashReportTextFormatiOS]
生成的字符串保存为 .crash 文件。
symbolicatecrash 分析
当获得到 .crash 日志后,就需要分析然后定位到具体的哪个文件的哪一行代码了,使用 symbolicatecrash 即可,这里有一篇教程:使用 symbolicatecrash 分析 .crash 文件
export DEVELOPER_DIR="/Applications/Xcode.app/Contents/Developer"
./symbolicatecrash report.crash AppName.app.dSYM > 1001.crash