封装打印,区分颜色,显示文件名,函数名和行号
01_test_printf.c
#include <stdio.h> #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" #define ERROR(x, args...) printf(ANSI_COLOR_RED "ERROR, %s,%s:%d "x ANSI_COLOR_RESET" ", __FILE__, __func__, __LINE__, ##args) #define WARN(x, args...) printf(ANSI_COLOR_YELLOW "WARN , %s,%s:%d "x ANSI_COLOR_RESET" ", __FILE__, __func__, __LINE__, ##args) #define INFO(x, args...) printf(ANSI_COLOR_CYAN "INFO , %s,%s:%d "x ANSI_COLOR_RESET" ", __FILE__, __func__, __LINE__, ##args) #define DEBUG(x, args...) printf(ANSI_COLOR_GREEN "DEBUG, %s,%s:%d "x ANSI_COLOR_RESET" ", __FILE__, __func__, __LINE__, ##args) int main() { printf("hello "); INFO("hello world"); ERROR("hello world"); DEBUG("hello world"); WARN("hello world"); return 0; }
运行结果