美文网首页iOS bug修复iOS 开发 iOS Developer
iOS NSLog去掉时间戳及其他输出样式

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

作者: 没了蜡笔de小新 | 来源:发表于2016-09-01 10:51 被阅读654次
    1. 一般项目中我的NSLog会在Prefix.pch文件添加如下代码,已保证在非调试状态下NSLog不工作
    #ifdef DEBUG
    #define NSLog(...) NSLog(__VA_ARGS__)
    #else
    #define NSLog(...)
    #endif```
    
    2. 在项目中如果没做任何处理的话会输出如下信息,前面有一个时间戳
    
    **2016-09-01 10:42:16.787 test_tabBarController[19226:1694499] <UIView: 0x7fc430d3db20; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x7fc430d12ba0>>**
    
    ![出现时间戳的](http:https://img.haomeiwen.com/i1184094/7a3711bf53bf84bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    
    我们修改下宏如下:
    

    ifdef DEBUG

    define NSLog(FORMAT, ...) fprintf(stderr,"%s\n",[[NSString stringWithFormat:FORMAT, ##VA_ARGS] UTF8String]);

    else

    define NSLog(...)

    endif```

    经过上面的修改我们可以输出 纯净的内容如下:

    去掉时间戳了的

    3.我们可以用更好的版本我推荐用这个打印我们的日志:

    #ifdef DEBUG
    #define NSLog(FORMAT, ...) fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
    #else
    #define NSLog(...)
    #endif```
    这样我们的输出就是这样:
    他可以打印出行号 和 对应的class类名 很强大有木有
    ![](http:https://img.haomeiwen.com/i1184094/af6c361aa3377884.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    > 如果本文帮到了你,敬请给出喜欢❤️  感谢你的小手✋ 关注作者😍 后续更新iOS刀法
    [传送门](http://www.jianshu.com/users/29e2c915351f/latest_articles)

    相关文章

      网友评论

      本文标题:iOS NSLog去掉时间戳及其他输出样式

      本文链接:https://www.haomeiwen.com/subject/sbmcettx.html