// ANSI_COLOR_RED 定义宏打印带有颜色的日志
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
// 定义日志级别 大于或等于LOG_LEVEL_SHOW 的都打印
#define LOG_LEVEL_SHOW 0
#define LOG_LEVEL_DEBUG 0
#define LOG_LEVEL_INFO 1
#define LOG_LEVEL_WARN 2
#define LOG_LEVEL_ERROR 3
// 定义log_info,根据LOG_LEVEL_SHOW的值来决定是否打印日志
#define log_info(fmt, ...) if (LOG_LEVEL_SHOW <= LOG_LEVEL_INFO) printf(ANSI_COLOR_GREEN " [%s]"ANSI_COLOR_RESET"%s %s %s %d " fmt "\r \n", "INFO", __FILE__, __func__, __TIME__, __LINE__, ##__VA_ARGS__)
#define log_debug(fmt, ...) if (LOG_LEVEL_SHOW <= LOG_LEVEL_DEBUG) printf(ANSI_COLOR_BLUE " [%s]"ANSI_COLOR_RESET"%s %s %s %d " fmt "\r \n", "DEBUG", __FILE__, __func__, __TIME__, __LINE__, ##__VA_ARGS__)
#define log_warn(fmt, ...) if (LOG_LEVEL_SHOW <= LOG_LEVEL_WARN) printf(ANSI_COLOR_YELLOW " [%s]"ANSI_COLOR_RESET"%s %s %s %d " fmt "\r \n", "WARN", __FILE__, __func__, __TIME__, __LINE__, ##__VA_ARGS__)
#define log_error(fmt, ...) if (LOG_LEVEL_SHOW <= LOG_LEVEL_ERROR) printf(ANSI_COLOR_RED " [%s]"ANSI_COLOR_RESET"%s %s %s %d " fmt "\r \n", "ERROR", __FILE__, __func__, __TIME__, __LINE__, ##__VA_ARGS__)
02-26
1258
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)