#脱离连接xcode记录自定义打印数据,方便分析
2017-11-29 11:00
323 查看
脱离连接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]; }
相关文章推荐
- 使用JDBC连接MySQL数据库--典型案例分析(八)----实现员工数据的分页查询
- 自学记录1#深入浅出数据分析
- 数据解析的xcode6.3配置and使用xib自定义cell高度
- *****MapReduce连接:重分区连接【里面分析了org.apache.hadoop.contrib.utils.join包中的基础数据join原理和优化后的抽象类】
- IMDB 5000 Movie Dataset 数据分析学习记录
- Tcp/ip学习笔记1_Tcp连接建立、数据传输、连接关闭分析
- 常规功能和模块自定义系统 (cfcmms)—037模块记录(数据)的权限设计(2)
- SSH整合-问题记录及分析:form表单接受数据不完整
- mysql中左连接后,最终的记录数大于左边表的记录分析
- 自定义printf函数,方便设置打印函数到开关
- 如何将数据添加到自定义的DataTable中并赋值给list实现打印功能。
- 为方便储户,某银行拟开发计算机储蓄系统。储户填写的存款单或取款单由业务员输入系统,如果是存款,系统记录存款人姓名、住址、存款类型、存款日期、利率等信息,并印出存款单给储户;如果是取款,系统计算利息并印出利息清单给储户。 写出问题定义并分析系统的可行性。
- 微信小程序自定义数据分析试水
- 小白学数据分析----->从购买记录分析道具支付环节
- 一步一步学MySQL----14 夺标数据记录查询之外连接
- 自学记录5#深入浅出数据分析
- Android 数据连接分析
- XCode使用记录—使用XCode自动向数据库插入测试数据
- go HTTP Client大量长连接保持(自定义client设置及源码简单分析)
- seoer应该记录分析哪些参考数据