#脱离连接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];}
阅读全文
0 0
- #脱离连接xcode记录自定义打印数据,方便分析
- xcode控制台打印Json数据
- ToStringBuilder,打印类成员,方便日志记录调试信息
- 自定义printf函数,方便设置打印函数到开关
- 方便记录
- 脱离文档流分析
- 脱离文档流分析
- xcode 打印数据不完整,只打印了一半
- 自定义数据连接池
- xcode静态分析工具使用练习记录
- 自定义下拉控件dropdownlist,绑定数据更方便
- 自定义SharedPreferences的工具类,方便保存数据
- Android 数据连接分析
- 自定义代码块在Xcode中的路径(方便下次换电脑时使用~)
- 问题 M: 打印学生的数据记录
- C#.NET自定义报表数据打印
- C#.NET自定义报表数据打印
- C#.NET自定义报表数据打印
- [LeetCode-Algorithms-40] "Combination Sum II" (2017.11.29-WEEK13)
- js 网站增加统计代码
- linux积累(一)---查看压缩文件的最后一行
- BZOJ5100[POI2018]Plan metra 构造
- android 之 activity(三、intent 详解)
- #脱离连接xcode记录自定义打印数据,方便分析
- 记录 VS 2012编译Qt 4.8.6
- python中的scipy库
- web应用从开发环境自动部署至tomcat的方法
- Android应用内多进程分析和研究
- 总结js中数据类型的bool值及其比较
- js 获取URL参数
- zookeeper启动失败,zkServer.sh status 出错
- C语言作业(3)