2011-03-08 99 views
0

我繼承了包含調試宏的代碼。根據參數進行宏擴展

#define DEBUG_LEVEL_NONE 0 
#define DEBUG_LEVEL_ONE 1 
#define DEBUG_LEVEL_TWO 2 

#if DEBUG_LEVEL == DEBUG_LEVEL_NONE 
    #define DEBUG(print_level, str) 

#else /* DEBUG_LEVEL */ 
    #define DEBUG(print_level, str)         \ 
    {                \ 
     if (DEBUG_LEVEL >= print_level)        \ 
     {               \ 
     printf("%-25s %4d %2d:", __FILE__, __LINE__, print_level); \ 
     printf(str);            \ 
     fflush(NULL);            \ 
     }               \ 
    } 
#endif /* DEBUG_LEVEL */ 

一切工作正常,但我想,以避免額外的代碼和測試更低的調試級別。

我知道我不能在宏定義中放置預處理器測試。有沒有辦法讓像

#define DEBUG(print_level, str)         \ 
    {                \ 
    #if (DEBUG_LEVEL >= print_level)        \ 
    printf("%-25s %4d %2d:", __FILE__, __LINE__, print_level); \ 
    printf(str);            \ 
    fflush(NULL);            \ 
    #endif              \ 
    } 

感謝

+0

不要忘記,多行宏需要在每行結尾處使用反斜槓。 – Lindydancer

回答

0

你可以使用一個簡單的效果,「如果」,任何像樣的編譯器將恆定倍的條件,排除身體。

+0

這就是目前發生的情況。在不同的調試級別之間,我沒有看到代碼大小上的差異。 – DanS