linux 调试 之printf

1. 可变参数宏

1.1 C99 标准中可变参数宏

#define debug(format, ...) printf(format, ##__VA_ARGS__)

1.2 gcc 复杂宏

 #define debug(format, args...) printf(format, ##args)

#的作用 : 连接两个宏,如果可变参数被忽略或为空,"##"操作将使预处理器(preprocessor)去除掉它前面的那个逗号.

2. 打印带颜色的字符串

2.1 格式

printf("\033[字背景颜色;字体颜色m字符串\033[0m" );

2.2 颜色码

背景色                           前景色
40: 黑                           30: 黑
41: 红                           31: 红
42: 绿                           32: 绿
43: 黄                           33: 黄
44: 蓝                           34: 蓝
45: 紫                           35: 紫
46: 深绿                         36: 深绿
47: 白色                         37: 白色

3. 举例----打印调试宏

#ifndef __USE_DEBUG                                                                                                                   
#define __USE_DEBUG

#ifdef  USE_DEBUG
#define DEBUG_LINE() printf("[%s:%s] line=%d\r\n",__FILE__, __func__, __LINE__)
#define DEBUG_ERR(fmt, args...) printf("\033[47;31m[%s:%d]\033[0m   "fmt" \r\n", __func__, __LINE__,  ##args)
#define DEBUG_INFO(fmt, args...) printf("\033[33m[%s:%d]\033[0m  "fmt"  \r\n", __func__, __LINE__, ##args)
#else
#define DEBUG_LINE()
#define DEBUG_ERR(fmt, ...)
#define DEBUG_INFO(fmt, ...)
#endif

#endif




int main(int args, const char ** argv)
{
    int fd;
    
    fd = open("/dev/nothing, O_RDWR");
    if(fd < 0){
        DEBUG_ERR(“open failed, ret = %d, %m\n”, fd);
    }
}

运行:

linux@fyang:~/work/debug$ gcc debug.c -DUSE_DEBUG
linux@fyang:~/work/debug$ ./a.out
[main:42] open faild, ret = -1, No such file or directory
linux@fyang:~/work/debug$

注: %m 会打印errno 对应的字符串 功能类似 perror

转载于:https://www.cnblogs.com/fengyang66/p/6523329.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值