DEBUG macros in C++

后端 未结 9 1942
谎友^
谎友^ 2020-12-07 07:54

I just encountered a DEBUG macro in C that I really like

#ifdef DEBUG_BUILD
#  define DEBUG(x) fprintf(stderr, x)
#else
#  define DEBUG(x) do {} while (0)
#e         


        
9条回答
  •  天涯浪人
    2020-12-07 08:39

    This is the log macro I am using currently:

    #ifndef DEBUG 
    #define DEBUG 1 // set debug mode
    #endif
    
    #if DEBUG
    #define log(...) {\
        char str[100];\
        sprintf(str, __VA_ARGS__);\
        std::cout << "[" << __FILE__ << "][" << __FUNCTION__ << "][Line " << __LINE__ << "] " << str << std::endl;\
        }
    #else
    #define log(...)
    #endif
    

    Usage:

    log(">>> test...");
    

    Output:

    xxxx/proj.ios_mac/Classes/IntroScene.cpp][gotoNextScene][Line 58] >>> test...
    

提交回复
热议问题