Re: [問題] print裡 比__VAR_ARGS__ 更好的宏?
支援不定變數的Macro
可以參考以下網站
http://gslin.blogspot.com/2006/11/c-macro.html
做法可以更輕鬆
class MyLog
{
public :
static void WriteLog(const char * format , ...)
{
// do something
}
};
#ifdef DEBUG
#define debug_printf MyLog::WriteLog
#else
#define debug_printf
#endif
※ 引述《DrStein (啤酒肚)》之銘言:
: 就想設個宏 讓他在 debug 會打印出來 但release 不會
: 這該是老生常談的問題:
: 不管windows 還是 linux 都該是這樣 :
: /*vc*/
: #define PRINT(FMT, ...) fprintf(stderr, FMT, __VA_ARGS__)
: /*gcc*/
: #define PRINT(FMT, ARGS...) fprintf(stderr, FMT,ARGS)
: /*g++*/
: #define PRINT(FMT, ...) fprintf (stderr, FMT, ## ARGS)
: 然後再定個:
: #define DEBUG_PRINT(ARG) (PRINT("DEBUG: "), PRINT(ARGS))
: 這樣看起來不錯 但討驗的是,在DEBUG_PRINT未定意時
: DEBUG_PRINT 這被替換為 空,後面的;又沒被忽略掉:
: ex :
: if(TRUE === someLogic )
: {
: DEBUG_PRINT("someLogic is TRUE\n");
: /*do some time */
: :
: }/*if TRUE === someLogic*/
: DEBUG_PRINT沒被定意時,被換為:
: if(TRUE === someLogic )
: {
: ;
: /*do some time */
: :
: }/*if TRUE === someLogic*/
: 編譯器就會靠杯了。
: 令一個方法是用
: void va_start( va_list arg_ptr, prev_param );
: type va_arg( va_list arg_ptr, type );
: void va_end( va_list arg_ptr );
: 這究不詳束 詳見
: http://hi.baidu.com/funrole/blog/item/6aaefaec3131e4c32e2e21ee.html
: 實作就是
: #include <stdarg.h>
: void debug_print(const char *fmt, ...)
: {
: #ifdef _DEBUG
: int nBuf;
: char szBuffer[1024];
: va_list args;
: va_start(args, fmt);
: nBuf = vsprintf(szBuffer, fmt, args) ;
: assert(nBuf >= 0);
: frintf(stderr,"QDOGC ERROR:%s\n",szBuffer);
: va_end(args);
: #endif /*_DEBUG*/
: }/*debug_print*/
: 這樣更不好 程式進到個空函數降低效能,雖然編譯器會加以優化,
: 但多少還是毛毛的
: 那請問板上高手有沒更好的做法呢 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.169.133.217
→
05/28 22:47, , 1F
05/28 22:47, 1F
推
05/29 01:16, , 2F
05/29 01:16, 2F
推
05/29 20:54, , 3F
05/29 20:54, 3F
→
05/29 20:54, , 4F
05/29 20:54, 4F
→
05/29 20:56, , 5F
05/29 20:56, 5F
→
05/29 20:56, , 6F
05/29 20:56, 6F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):