zoukankan      html  css  js  c++  java
  • IOS应用发布NSLog的注释及使用重定向,把控制台内容写入文件

    1 选择工程的Target -> Build Settings -> Preprocessor Macros.

    如图,默认 Debug项,是“DEBUG=1”.

    2 在程序中设置全局宏定义

    在程序的 ApplicationName-Prefix.pch 文件中,加入如下,很简单

    #ifdef DEBUG_MODE
    #define DLog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
    #else
    #define DLog( s, ... ) 
    #endif


    3 这样就设置好了,测试

    在任意ViewController.m中写入 

    DLog(@"1234");

    结果:

    2012-07-25 17:09:54.448 xxxx[7094:707] <0x28f790 ViewController.m:(64)> 1234

    这样发布的时候DLog就不会有输出了。 


    在Objective-c开发程序的时候,有专门的日志操作类NSLog,它将指定的输出,输出到(stderr),我们可以利用Xcode的日志输出窗口,那么既然是要记录到具体日志文件,我们就想输出日志写入到具体的日志文件即可。
     
    代码
    1、  宏定义(下面是我在程序中常用到的日志宏,用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
     
    可以看到,除了标准的用户定义输出外,我还加入了许多有用的信息,
    比如源程序文件位置,行号,类名,函数名等。具体的应用可以在具体的开发过程中添加、删除。
     
    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 都检查一下)。

    原文地址:http://www.xiaoyzz.com/2012/7/26/00415.html


    好岸园http://www.hopean.com/
    移动开发http://www.hopean.com/dev/yidongkaifa/
    编程语言http://www.hopean.com/dev/bianchengyuyan/
    js网页特效http://www.hopean.com/js/
    源码下载http://www.hopean.com/download/
    IT图书 http://www.hopean.com/book/

  • 相关阅读:
    SQL Server 内存中OLTP内部机制概述(四)
    SQL Server 内存中OLTP内部机制概述(三)
    SQL Server 内存中OLTP内部机制概述(二)
    SQL Server 内存中OLTP内部机制概述(一)
    用例图——远程网络教学系统
    jsp动作标签
    JavaBean
    JSP的内置对象——session
    JSP内置对象——response
    设计一个简单的网上答题及评测系统
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2781874.html
Copyright © 2011-2022 走看看