【开源项目】console-colors.c输出带颜色的日志
简介
console-colors.c是用于打印输出带有颜色的日志。在排除问题时,通过打印彩色的日志有助于我们更有效的定位问题。console-colors.c项目很简单,只有一个.c文件和一个.h文件。
项目地址:
https://github.com/Constellation/console-colors.c
使用
console-colors.c的使用十分简单,只有一个接口。
int cc_fprintf(cc_color_t color, FILE* stream, const char* format, ...);
参数:
color {console_color_t}:控制台颜色。我们可以将(FG | BG)作为颜色传递。
stream {FILE*}:stdout或stderr。其他将传递给fprintf没有颜色。
format {const char*}:格式字符串fprintf将采用
测试程序
我们打印HELLO WORLD
语句进行测试,HELLO
字体设置为红色,背景色设置为蓝色;WORLD
字体设置为白色,背景色设置为黑色。
int main(int argc, char* argv[])
{
cc_fprintf(CC_FG_RED | CC_BG_BLUE, stdout, "HELLO");
cc_fprintf(CC_FG_WHITE | CC_BG_BLACK, stdout, " WORLD\n");
return 0;
}
运行结果如下图所示:
可以看到打印的log中HELLO
字体为红色,背景色为蓝色,WORLD
字体为白色,背景色为黑色。效果与我们设置的一致。
本文链接:https://blog.csdn.net/u012028275/article/details/121182594