通过宏定义控制实现自己的Debug_Log输出

普通宏定义控制

通过宏定义控制打印

#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

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值