iOS - NSLog、UncaughtException日志保存到文件
转:http://blog.csdn.net/marujunyy/article/details/12005767
对于真机,日志没法保存,不好分析问题。所以有必要将日志保存到应用的Docunment目录下,方便取出分析。
首先是日志输出,分为c的printf和标准的NSLog输出,printf会向标准输出(sedout)打印,而NSLog则是向标准出错(stderr),我们需要同时让他们都将日志打印到一个文件中。 其次是Crash问题;Crash分为两种,一种是由EXC_BAD_ACCESS引起的,原因是访问了不属于本进程的内存地址,有可能是访问已被释放的内存;另一种是未被捕获的Objective-C异常(NSException),导致程序向自身发送了SIGABRT信号而崩溃。其实对于未捕获的Objective-C异常,我们是有办法将它记录下来的,如果日志记录得当,能够解决绝大部分崩溃的问题。
我写了两个函数用于写NSLog日志和Crash日志,这个两个函数都必须在AppDelegate文件中下面的函数里添加
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- //连接xcode时可以从监视器中看日志 没连接时Log日志会输出到文件中,
- [self redirectNSLogToDocumentFolder];
- - (void)redirectNSLogToDocumentFolder
- {
- //如果已经连接Xcode调试则不输出到文件
- if(isatty(STDOUT_FILENO)) {
- return;
- }
- UIDevice *device = [UIDevice currentDevice];
- if([[device model] hasSuffix:@"Simulator"]){ //在模拟器不保存到文件中
- return;
- }
- //将NSlog打印信息保存到Document目录下的Log文件夹下
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Log"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- BOOL fileExists = [fileManager fileExistsAtPath:logDirectory];
- if (!fileExists) {
- [fileManager createDirectoryAtPath:logDirectory withIntermediateDirectories:YES attributes:nil error:nil];
- }
- 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 *logFilePath = [logDirectory stringByAppendingFormat:@"/%@.log",dateStr];
- // 将log输入到文件
- freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
- freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
- //未捕获的Objective-C异常日志
- NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
- }
- void UncaughtExceptionHandler(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:@"Log"];
- 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://test@163.com?subject=bug报告&body=感谢您的配合!<br><br><br>错误详情:<br>%@",crashString ];
- // NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
- // [[UIApplication sharedApplication] openURL:url];
- }
iOS - NSLog、UncaughtException日志保存到文件的更多相关文章
- 【代码笔记】iOS-将log日志保存到文件
代码: #import "AppDelegate.h" #import "RootViewController.h" @implementation AppDe ...
- logstash自己的日志保存到文件中:log4j2.properties
status = error dest = err name = PropertiesConfig property.filename = /gwlog/data/logstash/logs appe ...
- C# log4net 配置及使用详解--日志保存到文件和Access(转)
按语: 最近项目要求选用Access数据库,但日志管理采用log4net,但保存到数据库一直没有成功,后按照如下配置在程序退出时可以成功保存. 开始新建文件应用log4net.dll ,重新编译就报 ...
- ios NSLog常见使用
NSLog常见输出格式 Table 1 Format specifiers supported by the NSString formatting methods and CFString for ...
- 如何将Log4Net 日志保存到mongodb数据库之实践
log4net的大名早有耳闻,一直没真正用过,这次开发APP项目准备在服务端使用log4net. 日志的数据量较大,频繁的写数据库容易影响系统整体性能,所以独立将日志写到mongodb数据库是不错的选 ...
- iOS - NSLog的使用方法
NSLog的定义 NSLog定义在NSObjCRuntime.h中,如下所示: void NSLog(NSString *format, …); 基本上,NSLog很像printf,同样会在conso ...
- IOS NSLog 打印bool值
输出BOOL值的方法:NSLog(@"%@",YES?@"YES":@"NO");%@输出字符串. NSLog(@"ifReadO ...
- iOS NSLog去掉时间戳及其他输出样式
1.一般项目中我的NSLog会在Prefix.pch文件添加如下代码,已保证在非调试状态下NSLog不工作 1 2 3 4 5 #ifdef DEBUG #define NSLog(...) NS ...
- iOS NSLog各种打印
%@ 对象 %d,%i 整型 (%i的老写法) %hd 短整型 %ld , %lld 长整型 %u 无符整型 %f 浮点型和double型 %0.2f 精度浮点数,只保留两位小数 %x: 为32 ...
随机推荐
- wordpress数据库优化-关闭日志修订
每次在wordpress网站修改文章的时候都会产生一个修订版本,wp_posts会产生一个post_type为“REVISIONS”的记录,修改次数一多的话,那修订版本就有几万条记录了 在functi ...
- leetcode Largest Rectangle in Histogram 解法二
上一篇文章讲了该题的一个解法.后来又发现一个更好的解法. 首先依旧考虑一个升序的数列,例如1,2,3,4,5.那么它的最大矩形显然是有5种可能,即 1*5,2*4,3*3,4*2,1*5.所以最大的矩 ...
- IOS委托设计模式(摘自IOS开发指南)
- WPF中log4net的用法
WPF中如何使用log4nethttp://www.cnblogs.com/C-Sharp2/archive/2013/04/12/WPF-LOG4NET.html Apache log4net Ma ...
- [主席树]ZOJ2112 && BZOJ1901 Dynamic Rankings
题意:n个数,q个询问 (n<=50000, q<=10000) Q x y z 代表询问[x, y]区间里的第z小的数 C x y 代表将(从左往右数)第x个数变成y 上篇介绍了在 ...
- 关于in与exists的效率讨论
关于in与exists的效率讨论1).select * from A where id in (select id from B)以上查询使用了in语句,in只执行一次,他查出B表的所有id字段并缓存 ...
- lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...
- React 性能调优原理
一.React影响性能的两个地方 二.调优原理
- 对象的类型转换P109
类作为一种应用数据类型,和基本数据类型的变量一样.不同类中存在对象与对象之间的类型转问题,对象的类型转换只能在 具有继承关系的 父类对象-----子类对象 之间进行 子类通常比父类拥有更多的域和 ...
- *两个关键字static和final
static关键字:可以用于修饰属性,也可以用于修饰方法,还可以用于修饰类. static 修饰属性: 无论一个类生成了多少个对象,所有这些对象共同使用唯一一份静态的成员变量:一个对象对该静态成员变量 ...