C/C++ 工程自定義打印log信息級別及對應的顏色

共定義了四個級別的log,級別從小到大分別爲:debug,info,warning,error
定義了全局log日誌打印的級別 FRIZY_LOG_LEVEL,低於定義級別的信息將不會被打印。

#define LOG_DEBUG       0
#define LOG_INFO        1
#define LOG_WARNING     2
#define LOG_ERROR       3

#ifndef FRIZY_LOG_LEVEL
    #define FRIZY_LOG_LEVEL 2
#endif

源碼如下:

#ifndef FRIZY_LOG_PRINT_H
#define FRIZY_LOG_PRINT_H

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>

#ifdef _WIN32
#define TrimFilePath(x) strrchr(x, '\\') ? strrchr(x, '\\') + 1 : x
#else  // Linux/Unix
#define TrimFilePath(x) strrchr(x, '/') ? strrchr(x, '/') + 1 : x
#endif

//#define LogTime

#define LogColorNone_			"\033[0m"
#define LogColorRed_				"\033[0;31m"
#define LogColorLightRed_		"\033[1;31m"
#define LogColorYellow_			"\033[0;33m"
#define LogColorLightYellow_		"\033[1;33m"
#define LogColorGreen_			"\033[0;32m"
#define LogColorLightGreen_		"\033[1;32m"

#define ColorfulPrint_(color, fmt, ...) \
	do { \
        printf(color fmt LogColorNone_, ##__VA_ARGS__); \
	} while(0)

#ifndef LogTime
#define PrintLog_(color, fmt, ...) \
	do { \
        ColorfulPrint_(color, \
			"[FRIZYLog| %s(%d)]: " fmt "\n", TrimFilePath(__FILE__), __LINE__, ##__VA_ARGS__); \
	} while(0)
#else
#define PrintLog_(color, fmt, ...) \
	do { \
		time_t t; time(&t); \
		struct tm *pTm = localtime(&t); \
        ColorfulPrint_(color, \
			"[%02d:%02d:%02d| FRIZYLog: %s(%d)]: " fmt "\n", \
			pTm->tm_hour, pTm->tm_min, pTm->tm_sec, __FILE__, __LINE__, ##__VA_ARGS__); \
	} while(0)
#endif

#ifndef FRIZY_LOG_LEVEL
    #define FRIZY_LOG_LEVEL 2
#endif

#define LOG_DEBUG       0
#define LOG_INFO        1
#define LOG_WARNING     2
#define LOG_ERROR       3


#define PrintDbg_(fmt, ...) PrintLog_(LogColorNone_, fmt, ##__VA_ARGS__)
#define PrintInfo_(fmt, ...) PrintLog_(LogColorLightGreen_, fmt, ##__VA_ARGS__)
#define PrintWarn_(fmt, ...) PrintLog_(LogColorLightYellow_, fmt, ##__VA_ARGS__)
#define PrintErr_(fmt, ...) PrintLog_(LogColorLightRed_, fmt, ##__VA_ARGS__)



#define FRIZY_LOG(level, fmt, ...) do {                      \
    if (level == LOG_DEBUG) { if(LOG_DEBUG >= FRIZY_LOG_LEVEL) {PrintDbg_(fmt, ##__VA_ARGS__);}            \
    } else if (level == LOG_INFO) { if(LOG_INFO >= FRIZY_LOG_LEVEL) {PrintInfo_(fmt, ##__VA_ARGS__);}            \
    } else if (level == LOG_WARNING) { if(LOG_WARNING >= FRIZY_LOG_LEVEL) {PrintWarn_(fmt, ##__VA_ARGS__);}            \
    } else if (level == LOG_ERROR) { if(LOG_ERROR >= FRIZY_LOG_LEVEL) {PrintErr_(fmt, ##__VA_ARGS__);}            \
    } else {                                                     \
    }                                                            \
} while (0)


#ifdef NDEBUG
#define Assert(EXPRESSION) ((void)0)
#else
#define Assert(EXPRESSION) do { \
    if (EXPRESSION) { \
        ((void)0); \
    } else { \
        (_assert(#EXPRESSION, TrimFilePath(__FILE__), __LINE__)); \
    } \
} while (0)
#endif


#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif

注意:顏色打印只在linux系統中有效!!!!!

使用示例:

FRIZY_LOG(LOG_ERROR, "This is a error log demo.");
throw std::exception();
發佈了27 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章