printf打印的字符串显示不同颜色
在ANSI终端下可以显示彩色代码:
如 printgf(“33[01;40;32m HELLO 33[01;40;37m”); 能显示绿色的HELLO字样。
printf("\033[0;32mPASSED\033[0;0m\n");
printf("\033[0;31mFAILED\033[0;0m\n");
这里33[是转义子列,表示后面接的是颜色代码。
01是高亮度,不写是低亮度。
40是背景色,40:黑,41:红:42:绿,43:黄,44:青,45:蓝,47:白。
32m是前景色:30:黑,31:红,32:绿,33:黄,34:蓝,35:紫,36:青,37:白。
实例:printf("\033[1;33m Hello World. \033[0m \n");
\033 [1; 33m xxxx
| | | |
开始 背景色 字体色 字符串
一,printf打印出不同颜色
printf("\033[字背景颜色;字体颜色m字符串\033[0m" );
颜色代码:
QUOTE:
字背景颜色范围: 40--49 字颜色: 30--39
40: 黑 30: 黑
41: 红 31: 红
42: 绿 32: 绿
43: 黄 33: 黄
44: 蓝 34: 蓝
45: 紫 35: 紫
46: 深绿 36: 深绿
47: 白色 37: 白色
ANSI控制码:
QUOTE:
\033[0m 关闭所有属性
\033[1m 设置高亮度
\03[4m 下划线
\033[5m 闪烁
\033[7m 反显
\033[8m 消隐
\033[30m -- \033[37m 设置前景色
\033[40m -- \033[47m 设置背景色
\033[nA 光标上移n行
\03[nB 光标下移n行
\033[nC 光标右移n行
\033[nD 光标左移n行
\033[y;xH设置光标位置
\033[2J 清屏
\033[K 清除从光标到行尾的内容
\033[s 保存光标位置
\033[u 恢复光标位置
\033[?25l 隐藏光标
\33[?25h 显示光标
// ---------------------------------------------------------------------------------- # include <stdio.h> # include <string.h> # include <time.h> # include <stdlib.h> // ---------------------------------------------------------------------------------- // 01是高亮度,不写是低亮度。 // 40是背景色,40:黑,41:红:42:绿,43:黄,44:青,45:蓝,47:白。 // 32m是前景色:30:黑,31:红,32:绿,33:黄,34:蓝,35:紫,36:青,37:白。 // ---------------------------------------------------------------------------------- // printf # define system_printf "printf " // printf // head # define properties_head "\"\\033[" // 数据头 // 添加分号 # define Control_code_add_semicolon ";" // 拼接 // 添加尾 # define Control_code_con_end "m" // 拼接 // 尾 # define Control_code_terminal_end "\n\"" // 尾部 # define Control_code_terminal_file "\n\">>output.txt" // 尾部 // Control code # define Control_code_Close_all_properties 0 // 清空all属性 # define Control_code_brightness_High 1 // 设置高亮度 # define Control_code_Italic 3 // 设置斜体 # define Control_code_Underscore 4 // 设置下划线 # define Control_code_Flashing 5 // 设置闪烁 # define Control_code_Reverse 7 // 设置返现 # define Control_code_Blanking 8 // 设置消隐 # define Control_code_Strikethrough_text 9 // 设置文本删除线 # define Control_code_Double_underscore 21 // 设置双下划线 // 字体颜色设置 # define Control_code_Font_color_black 30 // 设置字体颜色黑色 # define Control_code_Font_color_red 31 // 设置字体颜色红色 # define Control_code_Font_color_green 32 // 设置字体颜色绿色 # define Control_code_Font_color_yellow 33 // 设置字体颜色黄色 # define Control_code_Font_color_blue 34 // 设置字体颜色蓝色 # define Control_code_Font_color_purple 35 // 设置字体颜色紫色 # define Control_code_Font_color_Dark_green 36 // 设置字体颜色深绿 # define Control_code_Font_color_gray 37 // 设置字体颜色灰白 // 背景颜色设置 # define Control_code_background_color_black 40 // 设置背景颜色黑色 # define Control_code_background_color_red 41 // 设置背景颜色红色 # define Control_code_background_color_green 42 // 设置背景颜色绿色 # define Control_code_background_color_yellow 43 // 设置背景颜色黄色 # define Control_code_background_color_blue 44 // 设置背景颜色蓝色 # define Control_code_background_color_purple 45 // 设置背景颜色紫色 # define Control_code_background_color_Dark_green 46 // 设置背景颜色深绿 # define Control_code_background_color_gray 47 // 设置背景颜色灰白 // ---------------------------------------------------------------------------------- # define par_type long // ---------------------------------------------------------------------------------- // 流程 # define lcd_code_log_options_Hardware_data 0x0001 // lcd硬件数据 # define lcd_code_log_options_Initialization_data 0x0002 // 数据初始化 # define lcd_code_log_options_Filter_data 0x0004 // 数据过滤 # define lcd_code_log_options_AT_Parsing 0x0008 // 指令解析 # define lcd_code_log_options_Type_conversion 0x0010 // 数据类型转换 # define lcd_code_log_options_Deconstruct 0x0020 // 解字模 # define lcd_code_log_options_Update_interface 0x0040 // 刷界面 Interface # define lcd_code_log_options_Output_interface 0x0080 // 输出 Interface // 日报位置状态 # define lcd_code_log_options_tatus_conventional 0X0100 // 日报状态正常 # define lcd_code_log_options_tatus_Warning 0x0200 // 日报状态异常 // 输出位置状态 # define lcd_code_log_options_terminal 0X1000 // 输出到终端 # define lcd_code_log_options_file 0x2000 // 输出到文件 // ---------------------------------------------------------------------------------- struct Debug_Message_data{ unsigned long logo_out_Sign; unsigned int length; unsigned char logo_out_sbus[4096]; }; // ---------------------------------------------------------------------------------- // 清除调试数据 void Clean_Debug_Message_Module_data(par_type Debug_data_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; Debug_Message->logo_out_Sign = 0; Debug_Message->length = 0; memset((void*)Debug_Message->logo_out_sbus,0,4096); } // printf void log_Debug_Message_Module_add_printf(par_type Debug_data_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; sprintf((void *)&(Debug_Message->logo_out_sbus[Debug_Message->length]),"%s",system_printf); Debug_Message->length = strlen(Debug_Message->logo_out_sbus); } // 添加数据头 void log_Debug_Message_Module_add_head(par_type Debug_data_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; sprintf((void *)&(Debug_Message->logo_out_sbus[Debug_Message->length]),"%s",properties_head); Debug_Message->length = strlen(Debug_Message->logo_out_sbus); } // 添加参数 void log_Debug_Message_Module_add_data(par_type Debug_data_addr,char parameter){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; sprintf((void *)&Debug_Message->logo_out_sbus[Debug_Message->length],"%d",parameter); Debug_Message->length = strlen(Debug_Message->logo_out_sbus); } // 添加分号 void log_Debug_Message_Module_code_semicolon(par_type Debug_data_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; sprintf((void *)&Debug_Message->logo_out_sbus[Debug_Message->length],"%s",Control_code_add_semicolon); Debug_Message->length = strlen(Debug_Message->logo_out_sbus); } // 添加尾部 void log_Debug_Message_Module_code_end(par_type Debug_data_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; sprintf((void *)&Debug_Message->logo_out_sbus[Debug_Message->length],"%s",Control_code_con_end); Debug_Message->length = strlen(Debug_Message->logo_out_sbus); } // 添加时间 void log_Debug_Message_Module_add_Time(par_type Debug_data_addr,par_type label_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; char * label = (char *)label_addr; time_t t_time; struct tm *tm_time; // 时间转换 t_time = time(NULL); tm_time = localtime(&t_time); sprintf((void *)&Debug_Message->logo_out_sbus[Debug_Message->length],"%4d:%02d:%02d-%02d:%02d:%02d -- %s:",1900+tm_time->tm_year,tm_time->tm_mon,tm_time->tm_mday,tm_time->tm_hour,tm_time->tm_min,tm_time->tm_sec,label); Debug_Message->length = strlen(Debug_Message->logo_out_sbus); } // 添加字符串 void log_Debug_Message_Module_add_string(par_type Debug_data_addr,unsigned char * data_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; sprintf((void *)&Debug_Message->logo_out_sbus[Debug_Message->length],"%s",data_addr); Debug_Message->length = strlen(Debug_Message->logo_out_sbus); } // 添加尾部 output终端 void log_Debug_Message_Module_terminal_end(par_type Debug_data_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; sprintf((void *)&Debug_Message->logo_out_sbus[Debug_Message->length],"%s",Control_code_terminal_end); Debug_Message->length = strlen(Debug_Message->logo_out_sbus); } // 添加尾部 output 文件output.txt void log_Debug_Message_Module_file_end(par_type Debug_data_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; sprintf((void *)&Debug_Message->logo_out_sbus[Debug_Message->length],"%s",Control_code_terminal_file); Debug_Message->length = strlen(Debug_Message->logo_out_sbus); } // ---------------------------------------------------------------------------------- void printfss(par_type Debug_data_addr){ int count = 0; struct Debug_Message_data* Debug_Message = (struct Debug_Message_data *)Debug_data_addr; for(count = 0; count < 4096;count++){ printf("%c",Debug_Message->logo_out_sbus[count]); } } // ---------------------------------------------------------------------------------- // 正常日报 void log_Debug_Message_conventional_output(par_type Debug_Message_addr,par_type label_addr,par_type sbuf_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data* )Debug_Message_addr; char * sbuf = (char *)sbuf_addr; unsigned int Output_Sign = (Debug_Message->logo_out_Sign)&0xf000; Clean_Debug_Message_Module_data(Debug_Message_addr); // 清理数据 log_Debug_Message_Module_add_printf(Debug_Message_addr); // 添加指令printf log_Debug_Message_Module_add_head(Debug_Message_addr); // 添加数据头 log_Debug_Message_Module_add_data(Debug_Message_addr,Control_code_Font_color_black); // 添加参数 log_Debug_Message_Module_code_end(Debug_Message_addr); // 添加尾部 log_Debug_Message_Module_add_Time(Debug_Message_addr,label_addr); // 添加时间 log_Debug_Message_Module_add_string(Debug_Message_addr,sbuf); // 添加字符串 log_Debug_Message_Module_terminal_end(Debug_Message_addr); // 添加尾部 switch(Output_Sign){ // 文件存储位置 case lcd_code_log_options_terminal:{log_Debug_Message_Module_terminal_end(Debug_Message_addr);}break; case lcd_code_log_options_file:{log_Debug_Message_Module_file_end(Debug_Message_addr);}break; default:{;}break; } system((const char *)(Debug_Message->logo_out_sbus)); // 输出日志 } // 警告日报 void log_Debug_Message_Warning_output(par_type Debug_Message_addr,par_type label_addr,par_type sbuf_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data* )Debug_Message_addr; char * sbuf = (char *)sbuf_addr; unsigned int Output_Sign = (Debug_Message->logo_out_Sign)&0xf000; Clean_Debug_Message_Module_data(Debug_Message_addr); // 清理数据 log_Debug_Message_Module_add_printf(Debug_Message_addr); // 添加指令printf log_Debug_Message_Module_add_head(Debug_Message_addr); // 添加数据头 log_Debug_Message_Module_add_data(Debug_Message_addr,Control_code_brightness_High); // 添加参数 log_Debug_Message_Module_code_semicolon(Debug_Message_addr); // 添加分号 log_Debug_Message_Module_add_data(Debug_Message_addr,Control_code_Font_color_red); // 添加参数 log_Debug_Message_Module_code_end(Debug_Message_addr); // 添加尾部 log_Debug_Message_Module_add_Time(Debug_Message_addr,label_addr); // 添加时间 log_Debug_Message_Module_add_string(Debug_Message_addr,sbuf); // 添加字符串 switch(Output_Sign){ // 文件存储位置 case lcd_code_log_options_terminal:{log_Debug_Message_Module_terminal_end(Debug_Message_addr);}break; case lcd_code_log_options_file:{log_Debug_Message_Module_file_end(Debug_Message_addr);}break; default:{;}break; } system((const char *)(Debug_Message->logo_out_sbus)); // 输出日志 } // ---------------------------------------------------------------------------------- // lcd硬件数据 # define log_conventional_lcd_hardware_data(Debug_Message_addr,sbuf_addr) log_Debug_Message_conventional_output(Debug_Message_addr,(par_type)"硬件数据",sbuf_addr) // 正常日报 # define log_Warning_lcd_hardware_data(Debug_Message_addr,sbuf_addr) log_Debug_Message_Warning_output(Debug_Message_addr,(par_type)"硬件数据",sbuf_addr) // 警告日报 // 数据初始化 # define log_conventional_Data_initialization(Debug_Message_addr,sbuf_addr) log_Debug_Message_conventional_output(Debug_Message_addr,(par_type)"数据初始",sbuf_addr) // 正常日报 # define log_Warning_Data_initialization(Debug_Message_addr,sbuf_addr) log_Debug_Message_Warning_output(Debug_Message_addr,(par_type)"数据初始",sbuf_addr) // 警告日报 // 数据过滤 # define log_conventional_lcd_Data_filtering(Debug_Message_addr,sbuf_addr) log_Debug_Message_conventional_output(Debug_Message_addr,(par_type)"数据过滤",sbuf_addr) // 正常日报 # define log_Warning_lcd_Data_filtering(Debug_Message_addr,sbuf_addr) log_Debug_Message_Warning_output(Debug_Message_addr,(par_type)"数据过滤",sbuf_addr) // 警告日报 // 指令解析 # define log_conventional_lcd_Instruction_analysis(Debug_Message_addr,sbuf_addr) log_Debug_Message_conventional_output(Debug_Message_addr,(par_type)"指令解析",sbuf_addr) // 正常日报 # define log_Warning_lcd_Instruction_analysis(Debug_Message_addr,sbuf_addr) log_Debug_Message_Warning_output(Debug_Message_addr,(par_type)"指令解析",sbuf_addr) // 警告日报 // 数据类型转换 # define log_conventional_lcd_Data_type_conversion(Debug_Message_addr,sbuf_addr) log_Debug_Message_conventional_output(Debug_Message_addr,(par_type)"编码转换",sbuf_addr) // 正常日报 # define log_Warning_lcd_Data_type_conversion(Debug_Message_addr,sbuf_addr) log_Debug_Message_Warning_output(Debug_Message_addr,(par_type)"编码转换",sbuf_addr) // 警告日报 // 解字模 # define log_conventional_lcd_Deconstruct(Debug_Message_addr,sbuf_addr) log_Debug_Message_conventional_output(Debug_Message_addr,(par_type)"解析字模",sbuf_addr) // 正常日报 # define log_Warning_lcd_Deconstruct(Debug_Message_addr,sbuf_addr) log_Debug_Message_Warning_output(Debug_Message_addr,(par_type)"解析字模",sbuf_addr) // 警告日报 // 刷界面 Update Interface # define log_conventional_lcd_Update_Interface(Debug_Message_addr,sbuf_addr) log_Debug_Message_conventional_output(Debug_Message_addr,(par_type)"刷新界面",sbuf_addr) // 正常日报 # define log_Warning_lcd_Update_Interface(Debug_Message_addr,sbuf_addr) log_Debug_Message_Warning_output(Debug_Message_addr,(par_type)"刷新界面",sbuf_addr) // 警告日报 // 输出界面 Interface # define log_conventional_lcd_output_Interface(Debug_Message_addr,sbuf_addr) log_Debug_Message_conventional_output(Debug_Message_addr,(par_type)"输出界面",sbuf_addr) // 正常日报 # define log_Warning_lcd_output_Interface(Debug_Message_addr,sbuf_addr) log_Debug_Message_Warning_output(Debug_Message_addr,(par_type)"输出界面",sbuf_addr) // 警告日报 // ---------------------------------------------------------------------------------- // 正常 void log_Debug_Message_Module_conventional_Process_Sign(par_type Debug_Message_addr,par_type sbuf_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data* )Debug_Message_addr; unsigned int Process_Sign = (Debug_Message->logo_out_Sign)&0x00ff; // 流程 switch(Process_Sign){ case lcd_code_log_options_Hardware_data:{log_conventional_lcd_hardware_data(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Initialization_data:{log_conventional_Data_initialization(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Filter_data:{log_conventional_lcd_Data_filtering(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_AT_Parsing:{log_conventional_lcd_Instruction_analysis(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Type_conversion:{log_conventional_lcd_Data_type_conversion(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Deconstruct:{log_conventional_lcd_Deconstruct(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Update_interface:{log_conventional_lcd_Update_Interface(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Output_interface:{log_conventional_lcd_output_Interface(Debug_Message_addr,sbuf_addr);}break; default :{log_Debug_Message_conventional_output(Debug_Message_addr,(par_type)"logo Warning",(par_type)":logo_out_Sign Parameter error");}break; } } // 警告日报 void log_Debug_Message_Module_Warning_Process_Sign(par_type Debug_Message_addr,par_type sbuf_addr){ struct Debug_Message_data* Debug_Message = (struct Debug_Message_data* )Debug_Message_addr; unsigned int Process_Sign = (Debug_Message->logo_out_Sign)&0x00ff; // 流程 switch(Process_Sign){ case lcd_code_log_options_Hardware_data:{log_Warning_lcd_hardware_data(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Initialization_data:{log_Warning_Data_initialization(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Filter_data:{log_Warning_lcd_Data_filtering(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_AT_Parsing:{log_Warning_lcd_Instruction_analysis(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Type_conversion:{log_Warning_lcd_Data_type_conversion(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Deconstruct:{log_Warning_lcd_Deconstruct(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Update_interface:{log_Warning_lcd_Update_Interface(Debug_Message_addr,sbuf_addr);}break; case lcd_code_log_options_Output_interface:{log_Warning_lcd_output_Interface(Debug_Message_addr,sbuf_addr);}break; default :{log_Debug_Message_Warning_output(Debug_Message_addr,(par_type)"logo Warning",(par_type)"logo_out_Sign Parameter error");}break; } } // ---------------------------------------------------------------------------------- void log_Debug_Message_output(par_type sbuf_addr,unsigned int mode){ struct Debug_Message_data Debug_Message; unsigned int status_Sign = mode&0x0f00; Debug_Message.logo_out_Sign = mode; // 日志 switch(status_Sign){ case lcd_code_log_options_tatus_conventional:{ log_Debug_Message_Module_conventional_Process_Sign((par_type)(&Debug_Message),sbuf_addr); // 正常 }break; case lcd_code_log_options_tatus_Warning:{ log_Debug_Message_Module_Warning_Process_Sign((par_type)(&Debug_Message),sbuf_addr); // 异常 }break; default :{log_Debug_Message_Warning_output((par_type)&Debug_Message,(par_type)"logo Warning",(par_type)"status_Sign Parameter error");}break; } } // ---------------------------------------------------------------------------------- void main(void){ log_Debug_Message_output((par_type)"jikexianfeng@outlook.com",(lcd_code_log_options_Hardware_data|lcd_code_log_options_tatus_Warning|lcd_code_log_options_terminal)); log_Debug_Message_output((par_type)"jikexianfeng@outlook.com",(lcd_code_log_options_Initialization_data|lcd_code_log_options_tatus_Warning|lcd_code_log_options_terminal)); log_Debug_Message_output((par_type)"jikexianfeng@outlook.com",(lcd_code_log_options_Filter_data|lcd_code_log_options_tatus_Warning|lcd_code_log_options_terminal)); log_Debug_Message_output((par_type)"jikexianfeng@outlook.com",(lcd_code_log_options_AT_Parsing|lcd_code_log_options_tatus_Warning|lcd_code_log_options_terminal)); log_Debug_Message_output((par_type)"jikexianfeng@outlook.com",(lcd_code_log_options_Type_conversion|lcd_code_log_options_tatus_Warning|lcd_code_log_options_terminal)); log_Debug_Message_output((par_type)"jikexianfeng@outlook.com",(lcd_code_log_options_Deconstruct|lcd_code_log_options_tatus_Warning|lcd_code_log_options_terminal)); log_Debug_Message_output((par_type)"jikexianfeng@outlook.com",(lcd_code_log_options_Update_interface|lcd_code_log_options_tatus_Warning|lcd_code_log_options_terminal)); log_Debug_Message_output((par_type)"jikexianfeng@outlook.com",(lcd_code_log_options_Output_interface|lcd_code_log_options_tatus_Warning|lcd_code_log_options_terminal)); }
// ---------------------------------------------------------------------------------------------------------------------------------
测试代码:https://www.cnblogs.com/jikexianfeng/p/13830385.html