iPhone开发技巧之日志保存教程
来源:互联网 发布:用户数据安全保护 编辑:程序博客网 时间:2024/06/05 17:35
Objective-C开发程序的时候,有专门的日志操作类NSLog,它将指定的输出到标准的错误输出上(stderr)。我们可以利用它在Xcode的日志输出窗口,或者是输出到具体的文件当中。
AD:
iPhone开发技巧之日志保存教程是本文要介绍的内容,大部分人调试程序都是看日志吧,这里我就给大家总结一下iphone程序中添加保存日志的方法。
Objective-C开发程序的时候,有专门的日志操作类NSLog,它将指定的输出到标准的错误输出上(stderr)。我们可以利用它在Xcode的日志输出窗口,或者是输出到具体的文件当中。
下面是我在程序中常用到的日志宏,用DEBUG开关管理,也就是说只有在DEBUG模式下才让日志输出 :
- #ifdef DEBUG
- # define LOG(fmt, ...) do { \
- NSString* file = [[NSString alloc] initWithFormat:@"%s", __FILE__]; \
- NSLog((@"%@(%d) " fmt), [file lastPathComponent], __LINE__, ##__VA_ARGS__); \
- [file release]; \
- } while(0)
- # define LOG_METHOD NSLog(@"%s", __func__)
- # define LOG_CMETHOD NSLog(@"%@/%@", NSStringFromClass([self class]), NSStringFromSelector(_cmd))
- # define COUNT(p) NSLog(@"%s(%d): count = %d\n", __func__, __LINE__, [p retainCount]);
- # define LOG_TRACE(x) do {printf x; putchar('\n'); fflush(stdout);} while (0)
- #else
- # define LOG(...)
- # define LOG_METHOD
- # define LOG_CMETHOD
- # define COUNT(p)
- # define LOG_TRACE(x)
- #endif
可以看到,除了标准的用户定义输出外,我还加入了许多有用的信息,比如源程序文件位置,行号,类名,函数名等。具体的应用可以在具体的开发过程中添加、删除。
真机测试的时候,可以利用freopen将标准错误输出保存到指定的文件当中,这样就可以在问题发生后分析日志文件。
- - (void)redirectNSLogToDocumentFolder{
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]];
- NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
- freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
- }
- - (void)applicationDidFinishLaunching:(UIApplication *)application {
- // 真机测试时保存日志
- if ([CDeviceInfo getModelType] != SIMULATOR) {
- [self redirectNSLogToDocumentFolder];
- }
- .....
- }
小结:iPhone开发技巧之日志保存教程的内容介绍完了,希望通过本文的学习能对你有所帮助!
- iPhone开发技巧之日志保存教程
- iPhone开发技巧之调试篇— 保存日志
- iPhone开发技巧之调试篇(2)— 保存日志
- iPhone开发 — 保存日志
- iPhone开发教程之保存当前view到相册(截屏)的方法
- iPhone开发技巧之网络--- Web服务
- iPhone开发技巧之私有API--- UIBarButtonItem
- iPhone开发技巧之私有API--- UISegmentedControl
- iPhone开发技巧之私有API--- UITableView
- iPhone开发技巧之私有API--- UIApplication
- iPhone开发技巧之私有API--- UIBarButtonItem
- iPhone开发技巧之私有API--- UIBarButtonItem
- iPhone开发技巧之私有API
- iPhone开发之NSLog使用技巧
- iPhone开发技巧之私有API--- UIBarButtonItem
- iPhone开发之BSD Socket学习教程
- iPhone开发之BSD Socket学习教程
- 从头开发技巧之自定义日志工具
- NK 2122 太空飞行计划问题
- PHP 5.4.x 安装dedecms最新版后台登陆 网页一片空白
- C++中int转string方法合辑
- android 网络应用轻量框架
- 在VC中如何使用OCX控件
- iPhone开发技巧之日志保存教程
- Google 算是彻底了!中国SEO行业好荒诞!好荒谬!
- Windows快捷键的常见用法
- windows程序设计学习笔记(第一章)
- Mysql编译安装参数优化
- find all pairs of elements in a balanced BST that sum to a certain number
- poj3461 kmp 模板题
- 《coredump问题原理探究》windows版第一章前言
- win8/win2012上帝模式