自己实现一个调试信息库

代码如下:

#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;
}

没注释:
在这里插入图片描述
注释掉后:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值