#脱离连接xcode记录自定义打印数据,方便分析

来源:互联网 发布:java实用教程 编辑:程序博客网 时间:2024/06/05 19:18

脱离连接xcode记录自定义打印数据,方便分析

最近在写一个行车记录模块,本模块中需要根据车速,行驶距离时间,限速等进行多方面显示,所以测试的时候比较负责,不可能时时刻刻去看整个行车过程中每个地方的数据及某一步处理过的数据,为了更快的找到对应算法错误或需要调整的地方进行了log记录方便分析,当然也可以直接发送到你的邮箱:


使用要求

请打开你项目中info.plist中文件共享的设置,Application supports iTunes file sharing请设置为NO,这样在你使用手机连接电脑的时候就可以在iTunes的文件共享中查看你手机的一些东西了.但是请记住在上线前把他设置回YES,否则的话其他人也能这样做很不安全哦。

请在appdelegate.m中的didFinishLaunchingWithOptions方法中增加调用方法redirectNSlogToDocumentFolder如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    [self redirectNSlogToDocumentFolder];    return YES;}
- (void)redirectNSlogToDocumentFolder{    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentDirectory = [paths objectAtIndex:0];    long current = [[NSDate date] timeIntervalSince1970]*1000;//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];//    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];//    NSString * dateStr = [dateFormatter stringFromDate: detaildate];    NSString *fileName = [NSString stringWithFormat:@"driving%ld.log",current];// 注意不是NSData!    NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];    // 先删除已经存在的文件    NSFileManager *defaultManager = [NSFileManager defaultManager];    [defaultManager removeItemAtPath:logFilePath error:nil];    // 将log输入到文件    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);    //未捕获的Objective-C异常日志    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandlerCY);}void UncaughtExceptionHandlerCY(NSException* exception){    NSString* name = [ exception name ];    NSString* reason = [ exception reason ];    NSArray* symbols = [ exception callStackSymbols ]; // 异常发生时的调用栈    NSMutableString* strSymbols = [ [ NSMutableString alloc ] init ]; //将调用栈拼成输出日志的字符串    for ( NSString* item in symbols )    {        [ strSymbols appendString: item ];        [ strSymbols appendString: @"\r\n" ];    }    //将crash日志保存到Document目录下的Log文件夹下    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"crashLog"];    NSFileManager *fileManager = [NSFileManager defaultManager];    if (![fileManager fileExistsAtPath:logDirectory]) {        [fileManager createDirectoryAtPath:logDirectory  withIntermediateDirectories:YES attributes:nil error:nil];    }    NSString *logFilePath = [logDirectory stringByAppendingPathComponent:@"UncaughtException.log"];    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    NSString *dateStr = [formatter stringFromDate:[NSDate date]];    NSString *crashString = [NSString stringWithFormat:@"<- %@ ->[ Uncaught Exception ]\r\nName: %@, Reason: %@\r\n[ Fe Symbols Start ]\r\n%@[ Fe Symbols End ]\r\n\r\n", dateStr, name, reason, strSymbols];    //把错误日志写到文件中    if (![fileManager fileExistsAtPath:logFilePath]) {        [crashString writeToFile:logFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];    }else{        NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:logFilePath];        [outFile seekToEndOfFile];        [outFile writeData:[crashString dataUsingEncoding:NSUTF8StringEncoding]];        [outFile closeFile];    }    //把错误日志发送到邮箱//            NSString *urlStr = [NSString stringWithFormat:@"mailto://467817900@qq.com?subject=bug报告&body=感谢您的配合!<br><br><br>错误详情:<br>%@",crashString ];//            NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];//            [[UIApplication sharedApplication] openURL:url];}