我有一个IS_TESTING布尔标志,指示当前版本是否正在测试
如果IS_TESTING为NO,我想隐藏NSLog。它还应该支持单个字符串参数。
这是来自cocos2d的CCLog:
#define __CCLOGWITHFUNCTION(s, ...) \
NSLog(@"%s : %@",__FUNCTION__,[NSString stringWithFormat:(s), ##__VA_ARGS__])
#define __CCLOG(s, ...) \
NSLog(@"%@",[NSString stringWithFormat:(s), ##__VA_ARGS__])
#if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
#define CCLOG(...) do {} while (0)
#define CCLOGWARN(...) do {} while (0)
#define CCLOGINFO(...) do {} while (0)
#elif COCOS2D_DEBUG == 1
#define CCLOG(...) __CCLOG(__VA_ARGS__)
#define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)
#define CCLOGINFO(...) do {} while (0)
#elif COCOS2D_DEBUG > 1
#define CCLOG(...) __CCLOG(__VA_ARGS__)
#define CCLOGWARN(...) __CCLOGWITHFUNCTION(__VA_ARGS__)
#define CCLOGINFO(...) __CCLOG(__VA_ARGS__)
#endif // COCOS2D_DEBUG
这就是我写的:
#define __CCLOG(s, ...) \
NSLog(@"%@",[NSString stringWithFormat:(s), ##__VA_ARGS__])
#if IS_TESTING == 0
#define CCLOG(...) do {} while (0)
#elif IS_TESTING == 1
#define CCLOG(...) __CCLOG(__VA_ARGS__)
#endif
使用:
CCLOG(@"single string parameter");
CCLOG(@"string format %@", @"parameter");
它运行没有崩溃,但没有打印出来。
编辑:
#define IS_TESTING YES
我试过了
#if IS_TESTING == YES/NO
,但仍然与0/1相同,没有任何内容打印出来
答案 0 :(得分:0)
您的代码完美无缺。但是,在开始IS_TESTING
定义之前,必须先定义
所以,在 .pch 文件中,我写道:
__CCLOG
后来:
#define IS_TESTING 1
#define __CCLOG(s, ...) \
NSLog(@"%@",[NSString stringWithFormat:(s), ##__VA_ARGS__])
#if IS_TESTING == 0
#define CCLOG(...) do {} while (0)
#elif IS_TESTING == 1
#define CCLOG(...) __CCLOG(__VA_ARGS__)
#endif
由于我已将CCLOG(@"%@", @"YES");
定义为1,因此我们会记录。如果我将其更改为0,我们就不会。
作为奖励,我会告诉你我的所作所为。它简单得多。这是:
IS_TESTING
这使#define MyLog if(0); else NSLog
等同于MyLog
。要关闭日志记录,请将NSLog
更改为0
。