debug用的宏收集

1. 打印返回值

#define SAMPLE_DBG(s32Ret)\
do{\
    printf("s32Ret=%#x,fuc:%s,line:%d\n", s32Ret, __FUNCTION__, __LINE__);\
}while(0)

int i = 123;

SAMPLE_DBG(i);

//s32Ret=0x7b,fuc:test,line:20

 

2. 封装printf

#define MY_PRINTF(fmt, args...) fprintf(stderr,"\tMY DEBUG(%s:%d):\t" fmt, __func__, __LINE__, ##args)

//        MY DEBUG(test:12):      i = 123, s= sd

root@ubuntu:/work/pc_apps/demo# gcc debug.c -o debug
root@ubuntu:/work/pc_apps/demo# ./debug             
             NETLIB DEBUG(test:11):             i = 123, s= sd
             ZMDNETLIB ERROR (test:12):             i = 123, s= sd
root@ubuntu:/work/pc_apps/demo# cat debug.c         
#include <stdio.h>
#define NETLIB_PRINTF(fmt, args...) fprintf(stderr, "\033[1;32m             NETLIB DEBUG(%s:%d):             \033[0m" fmt, __func__, __LINE__, ## args)
#define ERR(fmt, args...) fprintf(stderr, "\033[1;31m             ZMDNETLIB ERROR (%s:%d):             \033[0m" fmt, __func__, __LINE__, ## args)
void test(void)
{
        int i = 123;
        char *s = "sd";
        NETLIB_PRINTF("i = %d, s= %s\n", i, s);
        ERR("i = %d, s= %s\n", i, s);
}
void main(void)
{
        test();
}

 

你可能感兴趣的:(debug用的宏收集)