2967 views|1 replies

2015

Posts

0

Resources
The OP
 

Using debug macro definition to turn on/off debug output in C/C++ [Copy link]

debug macro as debug switch When writing a program, in order to debug, you often need to add some output statements, and then comment them out after debugging is completed. If you need to debug next time, you have to uncomment them, which is very time-consuming and laborious. In order to solve this trouble, you can define a debug macro as a debug output switch. As shown in the following code: #include


int main(void)
{
    int i, sum;


    for (i = 1, sum = 0; i <= 5; i++)
    {
        sum += i;
#ifdef DEBUG
        printf("sum += %d is %d\n", i, sum);
#endif
    }
    printf("total sum is %d\n", sum);
}


上面代码中,只有定义DEBUG宏时,才会输出相加过程,我们可以在gcc编译时用-D选项定义DEBUG宏来打开这个调试开关,输出调试信息


gcc -D DEBUG test.c
1
DBGprint宏作为调试输出
只是像上面那样利用debug宏的话还是很麻烦,因为每次都要写#ifdef DEBUG,为此,我们可以定义个DBGprint宏作为调试输出,当需要调试输出时(有DEBUG宏定义)就将其定义为printf函数,否则就定义为空。如下例所示:


#include
#ifdef DEBUG
#define DBGprint(...) printf(__VA_ARGS__)
#else
#define DBGprint(...)
#endif


int main(void)
{
    int i, sum;


    for (i = 1, sum = 0; i <= 5; i++)
    {
        sum += i;
        DBGprint("sum += %d is %d\n", i, sum);
    }
    printf("total sum is %d\n", sum);
}


这样每次需要添加调试输出时只需要写DBGprint就可以了。


一些调试输出的宏
#define ERROR(...) /   
do{ /   
    fprintf(stderr, "[ERROR  ]%s %s(Line %d): ",__FILE__,__FUNCTION__,__LINE__); /   
    fprintf(stderr, __VA_ARGS__); /   
}while(0)   


#define WARNING(...) /   
do{ /   
    fprintf(stdout, "[WARNING]%s %s(Line %d): ",__FILE__,__FUNCTION__,__LINE__); /   
    fprintf(stdout, __VA_ARGS__); /   
}while(0)   


#define INFO(...) /   
do{ /   
    fprintf(stdout, "[INFO  ]%s %s(Line %d): ",__FILE__,__FUNCTION__,__LINE__); /   
    fprintf(stdout, __VA_ARGS__); /   
}while(0)   




#define SHOW_TIME(...) /   
do{/   
    extern unsigned long long gLatestTime;/   
    timeval tp;/   
    gettimeofday(&tp, NULL);/   
    unsigned long long now = tp.tv_sec*1000000+tp.tv_usec; /   
    if(gLatestTime != 0) /   
    { /   
        fprintf(stdout, ">>>>>>>>>Used Time: %s[%d], %s: %ld.%ld, %llu ms ", __FILE__, __LINE__, __func__, tp.tv_sec, tp.tv_usec, (now-gLatestTime)/1000);/   
        fprintf(stdout, __VA_ARGS__); /   
        fprintf(stdout, "/n"); /   
    } /   
    gLatestTime = now;/   
}while(0)   




#ifdef DEBUG   
#define DBG(...) /   
do{ /   
    fprintf(stdout, "[DEBUG  ]%s %s(Line %d): ",__FILE__,__FUNCTION__,__LINE__); /   
    fprintf(stdout, __VA_ARGS__); /   
}while(0)   
#else   
#define DBG(...)   
#endif   

This post is from Microcontroller MCU

Latest reply

Good information of the landlord  Details Published on 2019-3-18 08:50
 

406

Posts

1

Resources
2
 
Good information of the landlord
This post is from Microcontroller MCU
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list