iOS 将崩溃Log日志重定向输出到文件中保存
2015-07-28 17:55
489 查看
对于真机,日志没法保存,不好分析问题。所以有必要将日志保存到应用的Docunment目录下,并设置成共享文件,这样才能取出分析。
首先是日志输出,分为c的printf和标准的NSLog输出,printf会向标准输出(sedout)打印,而NSLog则是向标准出错(stderr),我们需要同时让他们都将日志打印到一个文件中。
例子:
freopen("xx.log","a+",stdout);
freopen("xx.log","a+",stderr);
具体做法:
// 将NSlog打印信息保存到Document目录下的文件中
- (void)redirectNSlogToDocumentFolder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"dr.log"];// 注意不是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);
}
此函数要在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中调用,这个函数在AppDelegate.m中实现的。
/*******************************************************************************/
// 当真机连接Mac调试的时候把这些注释掉,否则log只会输入到文件中,而不能从xcode的监视器中看到。
// 如果是真机就保存到Document目录下的drm.log文件中
UIDevice *device = [UIDevice currentDevice];
if (![[device model] isEqualToString:@"iPad Simulator"]) {
// 开始保存日志文件
[self redirectNSlogToDocumentFolder];
}
/*******************************************************************************/
最后配置共享文件夹:
在应用程序的Info.plist文件中添加UIFileSharingEnabled键,并将键值设置为YES。将您希望共享的文件放在应用程序的Documents目录。一旦设备插入到用户计算机,iTunes 9.1就会在选中设备的Apps标签中显示一个File Sharing区域。此后,用户就可以向该目录添加文件或者将文件移动到桌面计算机中。如果应用程序支持文件共享,当文件添加到Documents目录后,应用程序应该能够识别并做出适当响应。例如说,应用程序可以将新文件的内容显示界面上。请不要向用户展现目录的文件列表并询问他们希望对文件执行什么操作。
就是说,一旦设备连接上电脑,可以通过iTune查看指定应用程序的共享文件夹,将文件拷贝到你的电脑上看。
参考连接:
1.http://hi.baidu.com/ios_zb_com/item/ea528f4a8b1100d8c1a59217
2.http://www.cnblogs.com/ThankForYou/archive/2012/09/12/2681739.html
3.http://dingran.iteye.com/blog/1773573
首先是日志输出,分为c的printf和标准的NSLog输出,printf会向标准输出(sedout)打印,而NSLog则是向标准出错(stderr),我们需要同时让他们都将日志打印到一个文件中。
例子:
freopen("xx.log","a+",stdout);
freopen("xx.log","a+",stderr);
具体做法:
// 将NSlog打印信息保存到Document目录下的文件中
- (void)redirectNSlogToDocumentFolder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"dr.log"];// 注意不是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);
}
此函数要在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中调用,这个函数在AppDelegate.m中实现的。
/*******************************************************************************/
// 当真机连接Mac调试的时候把这些注释掉,否则log只会输入到文件中,而不能从xcode的监视器中看到。
// 如果是真机就保存到Document目录下的drm.log文件中
UIDevice *device = [UIDevice currentDevice];
if (![[device model] isEqualToString:@"iPad Simulator"]) {
// 开始保存日志文件
[self redirectNSlogToDocumentFolder];
}
/*******************************************************************************/
最后配置共享文件夹:
在应用程序的Info.plist文件中添加UIFileSharingEnabled键,并将键值设置为YES。将您希望共享的文件放在应用程序的Documents目录。一旦设备插入到用户计算机,iTunes 9.1就会在选中设备的Apps标签中显示一个File Sharing区域。此后,用户就可以向该目录添加文件或者将文件移动到桌面计算机中。如果应用程序支持文件共享,当文件添加到Documents目录后,应用程序应该能够识别并做出适当响应。例如说,应用程序可以将新文件的内容显示界面上。请不要向用户展现目录的文件列表并询问他们希望对文件执行什么操作。
就是说,一旦设备连接上电脑,可以通过iTune查看指定应用程序的共享文件夹,将文件拷贝到你的电脑上看。
参考连接:
1.http://hi.baidu.com/ios_zb_com/item/ea528f4a8b1100d8c1a59217
2.http://www.cnblogs.com/ThankForYou/archive/2012/09/12/2681739.html
3.http://dingran.iteye.com/blog/1773573
相关文章推荐
- Xcode 配置文件 xcconfig 使用实践
- iOS中ARC内部原理
- iOS正则表达式(二)
- IOS:Warning: Attempt to dismiss from view controller <xxxxxxxxxController: 0x15bd67f0> while a prese
- iOS省市区字典封装
- IOS编程常用字体
- iOS事件机制(一)
- iOS- xib(nib) 的重用(在有些情况下有利于加快项目功能的实现)
- [iOS]调用layoutSubViews方法(有的时候为啥会调用2遍)
- iOS 应用程序的生命周期详解
- iOS中IMP指针的运用
- IOS- 得到系统版本
- iOS多态
- iOS开发使用nib进行界面设计并跳转
- iOS—修改AFNetworking源文件可接收text/plain的方法
- IOS-写文件到本地
- [iOS]NSString到底使用Copy还是使用Strong属性,有什么区别
- iOS 中如何预览HTML中的图片
- iOS中的KVC
- Gios WORD .NET Library (using RTF specification)