background image

#else 
#  define LOG(...) 
#  define LOG_METHOD 
#  define LOG_CMETHOD 
#  define COUNT(p) 
#  define LOG_TRACE(x) 
#endif
 
可以看到,除了标准的用户定义输出外,我还加入了许多有用的信息,
比如源程序文件位置,行号,类名,函数名等。具体的应用可以在具体的开发过程中添加、
删除。
 
2、  应用:
- (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]; 

}
 
真机测试的时候,可以利用

freopen 将标准错误输出保存到指定的文件当中,

这样就可以在问题发生后分析日志文件。
 
3、  设置 DEBUG 标志是否正确定义
 
Xcode 一般会在 debug 运行配置项里面已经定义号了 DEBUG 标志,如果没定义我们就自
己写上,以我的

 Xcode 4 为例,在项目 get Info 中找到 PreProcessor Macros 这个属性,对于

 

Debug 配置我们给他写上 DEBUG,而在 Release 配置中把它留空。 这样我们刚才那段预处
理命令就可以根据这个标志来判断我们编译的时调试版本还是发布版本,从而控制

 NSLog 

的输出。

 (因为 Xcode 4 会把 debug/release 两个配置项同时对比展现出来,而 3.x 版本的只

能分别设置,如果你用的时

xcode 3.x 开发工具, 那么就分别对 Debug/Release 都检查一下)。

Objective-c 开发程序的时候,有专门的日志操作类 NSLog,它将指定的输出,输出到