干净的NSLog - 无时间戳和程序名称 [英] Clean NSLog - No timestamp and program name
问题描述
我几乎整理干净的NSLog这个code:
I almost finishing a clean NSLog with this code:
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, __VA_ARGS__] UTF8String]);
这正常工作,如果我这样做:
This work fine if I do this:
NSLog(@"Show %@ message", @"this");
但是,将失败,如果我是用户
But, will fail if I user it
NSLog(@"One argument");
由于 __ __ VA_ARGS
不算什么,所以它产生
because __VA_ARGS__
is nothing, so it produce
printf("%s\n", [[NSString stringWithFormat:@"One argument",] UTF8String]);
那么,问题是逗号。因为这是宏, __ __ VA_ARGS
是什么。所以我不能做这样 __的事情__ VA_ARGS ==零
,因为会产生 ==零
将失败。
So, the problem is the comma. Because this is macro, __VA_ARGS__
is nothing. So I can't do things like __VA_ARGS__==nil
because will produce ==nil
and will fail.
问题很简单:怎么办时, __ __ VA_ARGS
什么?或只有多个参数时使用逗号。
The question is simple: What to do when __VA_ARGS__
is nothing? Or only use comma when have more arguments.
- 编辑 -
Xuzhe解决问题!是最好的code,可以以这种方式使用它:
Xuzhe solve the problem! To be best code, can use it in this way:
#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(...)
#endif
现在,没有更多的凌乱的日志!
Now, no more messy logs!!!
推荐答案
使用此code(注意 ##
部分):
Use this code (notice the ##
part):
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
这篇关于干净的NSLog - 无时间戳和程序名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!