C/C++:自訂 Debug 訊息

Jacky | Aug 30, 2022 min read

此內容由 Notion 編輯並經由 Notion API 自動轉成 Hugo 頁面
The content write by Notion, and use Notion API convert to Hugo page.

#include <stdarg.h>
#include <stdio.h>

#define COLOR_X "\033[0m"
#define COLOR_I "\033[92m"
#define COLOR_D "\033[96m"
#define COLOR_W "\033[93m"
#define COLOR_E "\033[91m"


#define _INFO
#define _DEBUG
#define _WARN
#define _ERROR


#ifdef _INFO
#define print_info(fmt, ...)  \
    do { \
        printf("%s[I] <%s:%d>%s ", COLOR_I, __FUNCTION__, __LINE__, COLOR_X); \
        printf(fmt, ##__VA_ARGS__); \
        printf("\n"); \
    } while(0)
#else
#define print_info(fmt, ...)
#endif


#ifdef _DEBUG
#define print_debug(fmt, ...)  \
    do { \
        printf("%s[D] <%s:%d>%s ", COLOR_D, __FUNCTION__, __LINE__, COLOR_X); \
        printf(fmt, ##__VA_ARGS__); \
        printf("\n"); \
    } while(0)
#else
#define print_debug(fmt, ...)
#endif


#ifdef _WARN
#define print_warn(fmt, ...)  \
    do { \
        printf("%s[W] <%s:%d>%s ", COLOR_W, __FUNCTION__, __LINE__, COLOR_X); \
        printf(fmt, ##__VA_ARGS__); \
        printf("\n"); \
    } while(0)
#else
#define print_warn(fmt, ...)
#endif


#ifdef _ERROR
#define print_error(fmt, ...)  \
    do { \
        printf("%s[E] <%s:%d>%s ", COLOR_E, __FUNCTION__, __LINE__, COLOR_X); \
        printf(fmt, ##__VA_ARGS__); \
        printf("\n"); \
    } while(0)
#else
#define print_error(fmt, ...)
#endif

Refer

comments powered by Disqus