代码如下:
#ifndef __MY_DEBUG__
#define __MY_DEBUG__
#include <stdio.h>
#define NONE "\e[0m"
#define BLACK "\e[0;30m"
#define L_BLACK "\e[1;30m"
#define RED "\e[0;31m"
#define L_RED "\e[1;31m"
#define GREEN "\e[0;32m"
#define L_GREEN "\e[1;32m"
#define BROWN "\e[0;33m"
#define YELLOW "\e[1;33m"
#define BLUE "\e[0;34m"
#define L_BLUE "\e[1;34m"
#define PURPLE "\e[0;35m"
#define L_PURPLE "\e[1;35m"
#define CYAN "\e[0;36m"
#define L_CYAN "\e[1;36m"
#define GRAY "\e[0;37m"
#define WHITE "\e[1;37m"
#define DEBUG_MESSAGE
#ifdef DEBUG_MESSAGE
#define deb_info(format, ...) \
do { \
if (1) { \
printf("[%s %s] ", __DATE__, __TIME__); \
printf(GREEN "[info] " NONE); \
printf(format, ##__VA_ARGS__); \
printf("\n"); \
} \
} while (0)
#define deb_warn(format, ...) \
do { \
if (1) { \
printf("[%s %s] ", __DATE__, __TIME__); \
printf(YELLOW "[warm] " NONE); \
printf(format, ##__VA_ARGS__); \
printf("\n"); \
} \
} while (0)
#define deb_erro(format, ...) \
do { \
if (1) { \
printf("[%s %s] ", __DATE__, __TIME__); \
printf(RED "[error] " NONE); \
printf(format, ##__VA_ARGS__); \
printf("\n"); \
} \
} while (0)
#else
#define deb_info(format, ...) \
do { \
if (0) { \
printf("[%s %s] ", __DATE__, __TIME__); \
printf(GREEN "[info] " NONE); \
printf(format, ##__VA_ARGS__); \
printf("\n"); \
} \
} while (0)
#define deb_warn(format, ...) \
do { \
if (0) { \
printf("[%s %s] ", __DATE__, __TIME__); \
printf(YELLOW "[warm] " NONE); \
printf(format, ##__VA_ARGS__); \
printf("\n"); \
} \
} while (0)
#define deb_erro(format, ...) \
do { \
if (0) { \
printf("[%s %s] ", __DATE__, __TIME__); \
printf(RED "[error] " NONE); \
printf(format, ##__VA_ARGS__); \
printf("\n"); \
} \
} while (0)
#endif
#endif
main函数:
int main(int argc, char **argv) {
deb_info("this is info");
deb_warn("this is warn");
deb_erro("this is erro");
return 0;
}
运行结果:
如果后期不需要调试信息的时候,可以将头文件中的#define DEBUG_MESSAGE给注释掉,并且在没有大括号的if语句和while语句中也不会有影响
测试代码
int func1(){
return 0;
}
int main(){
int ret = func1();
if(ret == 0)
deb_info("success");
else
deb_erro("failed");
deb_info("this is a test");
return 0;
}
没注释:
注释掉后: