c语言编程有用的调试宏

1615阅读 0评论2009-08-07 piginthetree
分类:C/C++


在c文件函数的上方加上这个宏定义之后,可以方便的开启和关闭调试功能,内核代码中经常看到类似技巧。

#if    1
#define    DEBUGP    printf
#else
#define    DEBUGP(fmt, ...)
#endif

分级调试

#ifndef DBG
#define DBG
#endif

//

// Debug information verbosity: lower values indicate higher urgency

//

#define RT_DEBUG_OFF 0
#define RT_DEBUG_ERROR 1
#define RT_DEBUG_WARN 2
#define RT_DEBUG_TRACE 3
#define RT_DEBUG_INFO 4
#define RT_DEBUG_LOUD 5


#ifdef DBG
extern ULONG        RTDebugLevel;

#define DBGPRINT_RAW(Level, Fmt) \
do{ \
    if (Level <= RTDebugLevel) \
    { \
        printk Fmt; \
    } \
}while(0)

#define DBGPRINT(Level, Fmt) DBGPRINT_RAW(Level, Fmt)


#define DBGPRINT_ERR(Fmt) \
{ \
    printk("ERROR!!! "); \
    printk Fmt; \
}

#define DBGPRINT_S(Status, Fmt)        \
{                                    \
    printk Fmt;                    \
}
#else
#define DBGPRINT(Level, Fmt)
#define DBGPRINT_RAW(Level, Fmt)
#define DBGPRINT_S(Status, Fmt)
#define DBGPRINT_ERR(Fmt)

#endif

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


//put this in .c file

//ULONG    RTDebugLevel = RT_DEBUG_ERROR;


//example

/*
ULONG    RTDebugLevel = RT_DEBUG_ERROR;
DBGPRINT(RT_DEBUG_ERROR, ("hello,debug=%s", "aaa"));
*/


上一篇:linux中system函数的返回值
下一篇:gcc 编译错误记录