Xcode 日志打印不全

2020-06-07  本文已影响0人  天空中的球

突然发现我们Xcode 日志请求响应打印不全了,只打印了一小半。

#ifdef DEBUG // 处于开发阶段
#define DDLog(...) NSLog(__VA_ARGS__)
#else // 处于发布阶段
#define DDLog(...)
#endif

宏连接符 注意下:
__VA_ARGS__代表 C99 编译器标准允许定义可变参数宏(variadic macros),这样就使用拥有可以变化的参数表的宏。

而此处是 #define DDLog(...) NSLog(__VA_ARGS__), 关于这种如何处理呢,

方法一:直接替换

#ifdef DEBUG
#define DDLog(FORMAT, ...) fprintf(stderr, "%s:%zd\t%s\n", [[[NSString stringWithUTF8String: __FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat: FORMAT, ## __VA_ARGS__] UTF8String]);
#else
#define DDLog(FORMAT, ...) nil
#endif

验证确实是有效的

方法二:号称高级版本

#ifdef DEBUG
#define DDLog(FORMAT, ...) {\
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];\
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];\
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];\
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];\
[dateFormatter setTimeZone:timeZone];\
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSSZ"];\
NSString *str = [dateFormatter stringFromDate:[NSDate date]];\
fprintf(stderr,"TIME:%s【FILE:%s--LINE:%d】FUNCTION:%s\n%s\n",[str UTF8String],[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__,__PRETTY_FUNCTION__,[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);\
}
#else
#define DDLog(...);
#endif

亲测也是有效的。

笔记来源:自定义的打印语句NSLog在控制台输出不完整的完美解决

上一篇 下一篇

猜你喜欢

热点阅读