使用 PLCrashReporter 上传崩溃日志, symbolicatecrash 分析日志

集成 PLCrashReporter 上传日志

PLCrashReporter 是开源的,官网地址:https://www.plcrashreporter.org/html

把 CrashReporter.framework 引入集成到项目中,ios

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 文件。app

symbolicatecrash 分析

当得到到 .crash 日志后,就须要分析而后定位到具体的哪一个文件的哪一行代码了,使用 symbolicatecrash 便可,这里有一篇教程:使用 symbolicatecrash 分析 .crash 文件iphone

export DEVELOPER_DIR="/Applications/Xcode.app/Contents/Developer"工具

./symbolicatecrash report.crash AppName.app.dSYM > 1001.crashatom

相关文章
相关标签/搜索