调试的时候需要打印一些调试信息,刚开始的做法如下
1 #ifdef _DEBUG
2 // 打印调试信息
3 #endif
2 // 打印调试信息
3 #endif
这样的代码写得多了,就觉得繁琐,于是我进行了改进如下:
1 void DebugPrint(const char * format, ... );
2 #ifdef _DEBUG
3 #define DEBUG_PRINT DebugPrint
4 #else
5 #define DEBUG_PRINT
6 #endif
2 #ifdef _DEBUG
3 #define DEBUG_PRINT DebugPrint
4 #else
5 #define DEBUG_PRINT
6 #endif
这样就可以在调试模式下使用DEBUG_PRINT打印各种类型的数据。
自鸣得意了一段时间,忽然想到了一个问题,在Release模式下,语句:
1 DEBUG_PRINT("Hello, %s", "World");
虽然这样子不会影响到程序的运行,但是万一产生某些副作用呢???于是到网上搜了一下,找到了下面的解决方案:
1 void DebugPrint(const char * format, ... );
2 #ifdef _DEBUG
3 #define DEBUG_PRINT DebugPrint
4 #else
5 #define DEBUG_PRINT /\
6 / DebugPrint
7 #endif
2 #ifdef _DEBUG
3 #define DEBUG_PRINT DebugPrint
4 #else
5 #define DEBUG_PRINT /\
6 / DebugPrint
7 #endif