Mac开发-使用PLCrashReporter来保存崩溃日志信息

文章目录

  • 前言
    • 1.下载导入
    • 2.CocoaPods导入
  • 使用

前言

1.下载导入

首先去官网下载编译好的库
https://www.plcrashreporter.org/

2.CocoaPods导入

也可以使用CocoaPods安装: pod 'PLCrashReporter', '~> 1.2'


-> PLCrashReporter (1.2.0)
   Reliable, open-source crash reporting for iOS and Mac OS X.
   pod 'PLCrashReporter', '~> 1.2.0'
   - Homepage: https://www.plcrashreporter.org
   - Source:  
   https://www.plcrashreporter.org/static/downloads/PLCrashReporter-1.2.zip
   - Versions: 1.2.0, 1.2-rc5, 1.2-rc4, 1.2-rc2 [master repo]

使用

其实使用方法在下载的文件中都有,CrashReporter.h更是以注释的方式列举了使用方法

- (void) handleCrashReport {
    PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
    NSData *crashData;
    NSError *error;
    PLCrashReport *report;
    
    //write to file
    NSString *humanText;
    NSString *lastfile;
    //Try loading the crash report
    crashData = [crashReporter loadPendingCrashReportDataAndReturnError: &error];
    if (crashData == nil) {
        NSLog(@"Could not load crash report: %@", error);
        goto finish;
    }
    
    //We could send the report from here, but we'll just print out
    //some debugging info instead
    report = [[PLCrashReport alloc] initWithData: crashData error: &error];
    if (report == nil) {
        NSLog(@"Could not parse crash report");
        goto finish;
    }
    
    NSLog(@"[AppDelegate+Crash] Crashed on %@", report.systemInfo.timestamp);
    NSLog(@"[AppDelegate+Crash] Crashed with signal %@ (code %@, address=%0llx)", report.signalInfo.name,
          report.signalInfo.code, report.signalInfo.address);
    
    //Purge the report
    humanText = [PLCrashReportTextFormatter stringValueForCrashReport:report withTextFormat:PLCrashReportTextFormatiOS];
    //我这里是将崩溃日志写入我自己的日志末尾,这样存在于一个文件,好上传也好分析
    lastfile= [[NSUserDefaults standardUserDefaults] objectForKey:APP_LOGFILE];
    if (lastfile) {
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:lastfile];
        [fileHandle seekToEndOfFile];
        NSData* stringData  = [humanText dataUsingEncoding:NSUTF8StringEncoding];
        [fileHandle writeData:stringData]; //追加写入数据
        [fileHandle closeFile];
    }else {
        NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *genseePath = [[array objectAtIndex:0] stringByAppendingPathComponent:@"Webcast"];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyyMMdd-HHmmss"];
        NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.crash",dateString];
        NSString *logFilePath = [genseePath stringByAppendingPathComponent:fileName];
        NSError * err = nil;
        [humanText writeToFile:logFilePath atomically:YES encoding:NSUTF8StringEncoding error:&err];
        NSLog(@"[AppDelegate+Crash] log path not exist ,create file %@",logFilePath);
    }
    
finish:
    [crashReporter purgePendingCrashReport];
    return;
}

- (void)installCrashUnit {
    //xcode单步调试下不能使用,否则xcode断开
    // PLCrash can`t run while use xcode
    if (isatty(STDOUT_FILENO)) {
        NSLog(@"The demo crash app should be run without a debugger present. Exiting ...");
        return;
    }
    
    PLCrashReporterConfig *config = [[PLCrashReporterConfig alloc] initWithSignalHandlerType: PLCrashReporterSignalHandlerTypeMach symbolicationStrategy: PLCrashReporterSymbolicationStrategyAll];
    PLCrashReporter *crashReporter = [[PLCrashReporter alloc] initWithConfiguration:config];

//    PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
    NSError *error;
    //Check if we previously crashed
    if ([crashReporter hasPendingCrashReport]) {
        [self handleCrashReport];
    }
    //Enable the Crash Reporter
    if (![crashReporter enableCrashReporterAndReturnError: &error])
        NSLog(@"Warning: Could not enable crash reporter: %@", error);
    
}

这里指的注意的就是goto语法后面不能直接跟变量声明。
原示例中使用的是

PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];

这里使用了

PLCrashReporterConfig *config = [[PLCrashReporterConfig alloc] initWithSignalHandlerType: PLCrashReporterSignalHandlerTypeMach symbolicationStrategy: PLCrashReporterSymbolicationStrategyAll];
PLCrashReporter *crashReporter = [[PLCrashReporter alloc] initWithConfiguration:config];

PLCrashReporterSignalHandlerTypeMach 表示是Mach形式的异常处理实现,根据注释中的说明,Mac上是适合使用的
PLCrashReporterSymbolicationStrategyAll 启用所有可用的符号化策略。

这样导致了链接xcode时会卡死,原示例中的方式并不会。

这样的处理只能在下次启动时,处理上次的崩溃信息。

你可能感兴趣的:(Mac开发,iOS学习笔记)