转: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

  1. //连接xcode时可以从监视器中看日志 没连接时Log日志会输出到文件中,
  2. [self redirectNSLogToDocumentFolder];
  1. - (void)redirectNSLogToDocumentFolder
  2. {
  3. //如果已经连接Xcode调试则不输出到文件
  4. if(isatty(STDOUT_FILENO)) {
  5. return;
  6. }
  7. UIDevice *device = [UIDevice currentDevice];
  8. if([[device model] hasSuffix:@"Simulator"]){ //在模拟器不保存到文件中
  9. return;
  10. }
  11. //将NSlog打印信息保存到Document目录下的Log文件夹下
  12. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  13. NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Log"];
  14. NSFileManager *fileManager = [NSFileManager defaultManager];
  15. BOOL fileExists = [fileManager fileExistsAtPath:logDirectory];
  16. if (!fileExists) {
  17. [fileManager createDirectoryAtPath:logDirectory  withIntermediateDirectories:YES attributes:nil error:nil];
  18. }
  19. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  20. [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
  21. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //每次启动后都保存一个新的日志文件中
  22. NSString *dateStr = [formatter stringFromDate:[NSDate date]];
  23. NSString *logFilePath = [logDirectory stringByAppendingFormat:@"/%@.log",dateStr];
  24. // 将log输入到文件
  25. freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
  26. freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
  27. //未捕获的Objective-C异常日志
  28. NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
  29. }
  30. void UncaughtExceptionHandler(NSException* exception)
  31. {
  32. NSString* name = [ exception name ];
  33. NSString* reason = [ exception reason ];
  34. NSArray* symbols = [ exception callStackSymbols ]; // 异常发生时的调用栈
  35. NSMutableString* strSymbols = [ [ NSMutableString alloc ] init ]; //将调用栈拼成输出日志的字符串
  36. for ( NSString* item in symbols )
  37. {
  38. [ strSymbols appendString: item ];
  39. [ strSymbols appendString: @"\r\n" ];
  40. }
  41. //将crash日志保存到Document目录下的Log文件夹下
  42. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  43. NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Log"];
  44. NSFileManager *fileManager = [NSFileManager defaultManager];
  45. if (![fileManager fileExistsAtPath:logDirectory]) {
  46. [fileManager createDirectoryAtPath:logDirectory  withIntermediateDirectories:YES attributes:nil error:nil];
  47. }
  48. NSString *logFilePath = [logDirectory stringByAppendingPathComponent:@"UncaughtException.log"];
  49. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  50. [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
  51. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  52. NSString *dateStr = [formatter stringFromDate:[NSDate date]];
  53. 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];
  54. //把错误日志写到文件中
  55. if (![fileManager fileExistsAtPath:logFilePath]) {
  56. [crashString writeToFile:logFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  57. }else{
  58. NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:logFilePath];
  59. [outFile seekToEndOfFile];
  60. [outFile writeData:[crashString dataUsingEncoding:NSUTF8StringEncoding]];
  61. [outFile closeFile];
  62. }
  63. //把错误日志发送到邮箱
  64. //    NSString *urlStr = [NSString stringWithFormat:@"mailto://test@163.com?subject=bug报告&body=感谢您的配合!<br><br><br&gt;错误详情:<br>%@",crashString ];
  65. //    NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  66. //    [[UIApplication sharedApplication] openURL:url];
  67. }

iOS - NSLog、UncaughtException日志保存到文件的更多相关文章

  1. 【代码笔记】iOS-将log日志保存到文件

    代码: #import "AppDelegate.h" #import "RootViewController.h" @implementation AppDe ...

  2. logstash自己的日志保存到文件中:log4j2.properties

    status = error dest = err name = PropertiesConfig property.filename = /gwlog/data/logstash/logs appe ...

  3. C# log4net 配置及使用详解--日志保存到文件和Access(转)

    按语: 最近项目要求选用Access数据库,但日志管理采用log4net,但保存到数据库一直没有成功,后按照如下配置在程序退出时可以成功保存. 开始新建文件应用log4net.dll  ,重新编译就报 ...

  4. ios NSLog常见使用

    NSLog常见输出格式 Table 1  Format specifiers supported by the NSString formatting methods and CFString for ...

  5. 如何将Log4Net 日志保存到mongodb数据库之实践

    log4net的大名早有耳闻,一直没真正用过,这次开发APP项目准备在服务端使用log4net. 日志的数据量较大,频繁的写数据库容易影响系统整体性能,所以独立将日志写到mongodb数据库是不错的选 ...

  6. iOS - NSLog的使用方法

    NSLog的定义 NSLog定义在NSObjCRuntime.h中,如下所示: void NSLog(NSString *format, …); 基本上,NSLog很像printf,同样会在conso ...

  7. IOS NSLog 打印bool值

    输出BOOL值的方法:NSLog(@"%@",YES?@"YES":@"NO");%@输出字符串. NSLog(@"ifReadO ...

  8. iOS NSLog去掉时间戳及其他输出样式

    1.一般项目中我的NSLog会在Prefix.pch文件添加如下代码,已保证在非调试状态下NSLog不工作   1 2 3 4 5 #ifdef DEBUG #define NSLog(...) NS ...

  9. iOS NSLog各种打印

    %@ 对象 %d,%i 整型 (%i的老写法) %hd 短整型 %ld , %lld 长整型 %u 无符整型 %f 浮点型和double型 %0.2f 精度浮点数,只保留两位小数 %x:    为32 ...

随机推荐

  1. Graham's Scan算法

    原文链接:http://www.cnblogs.com/devymex/archive/2010/08/09/1795392.html C++/STL实现: #include <algorith ...

  2. POJ 2400 Supervisor, Supervisee(KM)

    題目鏈接 題意 :N个部门和N个员工,每个部门要雇佣一个工人,部门对每个工人打分,从1~N,1表示很想要,N表示特别不想要,每个工人对部门打分,从1~N.1表示很想去这个部门,N表示特别不想去这个部门 ...

  3. hdu 3441 Rotation

    总的来说,这题要2次用到polya定理. 由题目条件A*A=B*B+1,变形为(A-1)*(A+1)=K*B*B; 分别分解A-1和A+1的质因数,在合并在一起. 第一步:搜索B,对B*B的正方形涂色 ...

  4. DP方程及意义

    01背包 有N件物品和一个容量为V的背包.第i件物品的费用(即体积,下同)是w[i],价值是c[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 基本思路: 这是最基 ...

  5. 李洪强iOS开发之【零基础学习iOS开发【01-前言】03-前景和难易度分析

    一.iOS开发的前景 2012年3月份,苹果公司的市值已经突破5000亿美元,成为世界上市值最大的公司.5000亿是神马概念呢? 可以帮助陷入欧债危机的8个国家偿还债务 可以买下35个天安门广场.34 ...

  6. Apache与Tomcat整合

    Apache与Tomcat整合   一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache ...

  7. Go语言的优点(oschina讨论)

    Go语言的优点:并发/网络/性能/工具(fmt/pprof/test)/标准库(http/json/log/flags/atomic)/GoogleGo语言垃圾回收器真正致命的缺陷是,会导致整个进程不 ...

  8. java:装饰者模式,节点流和处理流

    接口: interface Worker { public void dowork(); } 清洁工:被装饰者,节点流 class Clean implements Worker { public v ...

  9. Spring Boot实现一个监听用户请求的拦截器

    项目中需要监听用户具体的请求操作,便通过一个拦截器来监听,并继续相应的日志记录 项目构建与Spring Boot,Spring Boot实现一个拦截器很容易. Spring Boot的核心启动类继承W ...

  10. java学习笔记[2015-1-19]

    想到哪儿说到哪儿: 1.关于import,全限定名导入和按需导入(即只使用*),影响的只是编译速度,编译过后生成的是全限定名,全限定名还一个好处是不容易产生冲突 2.每个java文件只能有一个publ ...