通过宏定义控制打印调试 C
普通宏定义控制
通过宏定义控制打印
#undef PDEBUG /* undef it, just in case */
#ifdef SCULL_DEBUG
# ifdef __KERNEL__
/* This one if debugging is on, and kernel space */
# define PDEBUG(fmt, args...) printk( KERN_DEBUG "scull: " fmt, ## args)
# else
/* This one for user space */
# define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
# endif
#else
# define PDEBUG(fmt, args...) /* not debugging: nothing */
#endif
#undef PDEBUGG
#define PDEBUGG(fmt, args...) /* nothing: it's a placeholder */
Makefile控制
将控制步骤简化到 Makefile 中,添加下面的行到你的 makfile 里:
# Comment/uncomment the following line to disable/enable debugging
DEBUG = y
# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines
else
DEBFLAGS = -O2
endif
CFLAGS += $(DEBFLAGS)
示例(TCP工程中的使用)
工程中实际使用
//Debug_Log(...) C99才支持
//用法Debug_Log同printf,注意不可使用在不加{}的条件体和循环体
#define TEST_DEBUG_LOG
#ifdef TEST_DEBUG_LOG
#define Debug_Log printf("Log at %s:%s:%d--",__FILE__, __fucn__, __LINE__);printf
#else
#define Debug_Log(...)
#endif
|-->linux 调试示例(TCP工程){
#ifndef DICTOL_DEBUG_H
#define DICTOL_DEBUG_H
//仅客户端使用
#define DICTOL_CLIENT_DEBUG_ERROR
#define DICTOL_CLIENT_DEBUG_WARNING
#define DICTOL_CLIENT_DEBUG_INFO
#ifdef DICTOL_CLIENT_DEBUG_ERROR
#define PrintClientError printf("<Client Error>at %s:%d--",__FILE__,__LINE__);printf
#else
#define PrintClientError(...)
#endif
//PrintClientError(...) C99才支持
//用法PrintClientError同printf,注意不可使用在不加{}的条件体和循环体,以下雷同
#ifdef DICTOL_CLIENT_DEBUG_WARNING
#define PrintClientWarning printf("<Client Warning>at %s:%d--",__FILE__,__LINE__);printf
#else
#define PrintClientWarning(...)
#endif
#ifdef DICTOL_CLIENT_DEBUG_INFO
#define PrintClientInfo printf("<Client Info>at %s:%d--",__FILE__,__LINE__);printf
#else
#define PrintClientInfo(...)
#endif
//仅服务器使用
#define DICTOL_SERVER_DEBUG_ERROR
#define DICTOL_SERVER_DEBUG_WARNING
#define DICTOL_SERVER_DEBUG_INFO
#ifdef DICTOL_SERVER_DEBUG_ERROR
#define PrintServerError printf("<Server Error>at %s:%d--",__FILE__,__LINE__);printf
#else
#define PrintServerError(...)
#endif
#ifdef DICTOL_SERVER_DEBUG_WARNING
#define PrintServerWarning printf("<Server Warning>at %s:%d--",__FILE__,__LINE__);printf
#else
#define PrintServerWarning(...)
#endif
#ifdef DICTOL_SERVER_DEBUG_INFO
#define PrintServerInfo printf("<Server Info>at %s:%d--",__FILE__,__LINE__);printf
#else
#define PrintServerInfo(...)
#endif
//仅公共库代码使用
#define DICTOL_PUBLIC_DEBUG_ERROR
#define DICTOL_PUBLIC_DEBUG_WARNING
#define DICTOL_PUBLIC_DEBUG_INFO
#ifdef DICTOL_PUBLIC_DEBUG_ERROR
#define PrintPublicError printf("<Public Error>at %s:%d--",__FILE__,__LINE__);printf
#else
#define PrintPublicError(...)
#endif
#ifdef DICTOL_PUBLIC_DEBUG_WARNING
#define PrintPublicWarning printf("<Public Warning>at %s:%d--",__FILE__,__LINE__);printf
#else
#define PrintPublicWarning(...)
#endif
#ifdef DICTOL_PUBLIC_DEBUG_INFO
#define PrintPublicInfo printf("<Public Info>at %s:%d--",__FILE__,__LINE__);printf
#else
#define PrintPublicInfo(...)
#endif
#endif