C 调试宏 【转】

#ifndef MYDEBUG
#define MYDEBUG
#endif

#define MYDEBUG_PREFIX "--Hank Gong--"

//Debug information verbosity: lower values indicate higher urgency
#define MYDEBUG_OFF 0
#define MYDEBUG_ERR 1
#define MYDEBUG_WRN 2
#define MYDEBUG_TRC 3
#define MYDEBUG_INF 4

extern int MYDEBUG_LEVEL;

#define mydebug_off(msg...) \
do{ \
    if (MYDEBUG_LEVEL>=MYDEBUG_OFF) { \
        printf(MYDEBUG_PREFIX msg); \
    } \
 }while(0)
            
#define mydebug_err(msg...) \
do{ \
    if (MYDEBUG_LEVEL>=MYDEBUG_ERR) { \
        printf(MYDEBUG_PREFIX msg); \
    } \
 }while(0)

#define mydebug_wrn(msg...) \
do{ \
    if (MYDEBUG_LEVEL>=MYDEBUG_WRN) { \
        printf(MYDEBUG_PREFIX msg); \
    } \
 }while(0)

#define mydebug_trc(msg...) \
do{ \
    if (MYDEBUG_LEVEL>=MYDEBUG_TRC) { \
        printf(MYDEBUG_PREFIX msg); \
    } \
 }while(0)

#define mydebug_inf(msg...) \
do{ \
    if (MYDEBUG_LEVEL>=MYDEBUG_INF) { \
        printf(MYDEBUG_PREFIX msg); \
    } \
 }while(0)

#define mydebug(level, msg...) mydebug_##level(msg)

#undef assert
#define assert(x) { \
    if (!(x)) { \
        printf(__FILE__ ":%d assert " #x " failed\n", __LINE__);    \
    }\
}


//Usage:
//just include this file and define MYDEBUG_LEVEL like
//
//int MYDEBUG_LEVEL=MYDEBUG_ERR
//
//in the program use the command by
//mydebug(off|err|wrn|trc|inf, "test %d with %s", 100, "this");

原文:http://www.iteye.com/topic/853465

posted @ 2012-12-05 15:56  Leo Forest  阅读(209)  评论(0编辑  收藏  举报