今天在群里有个人问到
请问下,这个彩色日志是怎样设置打印出来的,我的这些呼叫日志怎么都是白色的呢
于是追踪了下。在 Linux 终端上显示颜色的代码,格式为
%c[%d;%d;%dm%s%c[%d;%dm
如,下面两个实例
echo -e "\033[44;37;5m ME \033[0m"
echo -e "\e[32;1m[OK]\e[0m"
字色 背景 颜色
---------------------------------------
30 40 黑色
31 41 红色
32 42 绿色
33 43 黄色
34 44 蓝色
35 45 紫红色
36 46 青蓝色
37 47 白色
0 终端默认设置(黑底白字)
1 高亮显示
4 使用下划线
5 闪烁
7 反白显示
8 不可见
\033[44;37;5m ME \033[0m
中的 \033
为 ESC
的八进制表示 \033[
表示颜色设置开始,\033[0m
关闭颜色设置,同时 \033[0m
也是默认模式也就是传统终端的黑底白字。
至于,开头提到的终端不显示颜色,网上也有人提到,原因有以下几点
-
The command-line options to connect to the server includes the “no-color” flag.
-
No “term” environment variable is defined.
-
If the terminfo database exists, and either does not include a max_colors definition, or the value = 0.
-
The “term” environment variable does not include one of the following:
- “xterm”
- “xterm-color”
- “xterm-256color”
- “Eterm”
- “vt100”
- “crt”
大意就是终端默认发送了 no-color
标识,没有 term
环境变量。具体可以看看 Asterisk
中相关的代码
int ast_term_init(void)
{
char *term = getenv("TERM");
char termfile[256] = "";
char buffer[512] = "";
int termfd = -1, parseokay = 0, i;
if (ast_opt_no_color) { // asterisk 中没有设置颜色
return 0;
}
if (!ast_opt_console) { // 不是命令行模式
/* If any remote console is not compatible, we'll strip the color codes at that point */
vt100compat = 1;
goto end;
}
if (!term) { // 没有 term 环境变量
return 0;
}
for (i = 0;; i++) {
if (termpath[i] == NULL) {
break;
}
snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term);
termfd = open(termfile, O_RDONLY);
if (termfd > -1) {
break;
}
}
if (termfd > -1) {
int actsize = read(termfd, buffer, sizeof(buffer) - 1);
short sz_names = convshort(buffer + 2);
short sz_bools = convshort(buffer + 4);
short n_nums = convshort(buffer + 6);
/* if ((sz_names + sz_bools) & 1)
sz_bools++; */
if (sz_names + sz_bools + n_nums < actsize) {
/* Offset 13 is defined in /usr/include/term.h, though we do not
* include it here, as it conflicts with include/asterisk/term.h */
short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2);
if (max_colors > 0) {
vt100compat = 1;
}
parseokay = 1;
}
close(termfd);
}
if (!parseokay) {
/* These comparisons should not be substrings nor case-insensitive, as
* terminal types are very particular about how they treat suffixes and
* capitalization. For example, terminal type 'linux-m' does NOT
* support color, while 'linux' does. Not even all vt100* terminals
* support color, either (e.g. 'vt100+fnkeys'). */
if (!strcmp(term, "linux")) {
vt100compat = 1;
} else if (!strcmp(term, "xterm")) {
vt100compat = 1;
} else if (!strcmp(term, "xterm-color")) {
vt100compat = 1;
} else if (!strcmp(term, "xterm-256color")) {
vt100compat = 1;
} else if (!strncmp(term, "Eterm", 5)) {
/* Both entries which start with Eterm support color */
vt100compat = 1;
} else if (!strcmp(term, "vt100")) {
vt100compat = 1;
} else if (!strncmp(term, "crt", 3)) {
/* Both crt terminals support color */
vt100compat = 1;
}
}
end:
if (vt100compat) {
/* Make commands show up in nice colors */
if (ast_opt_light_background) {
snprintf(prepdata, sizeof(prepdata), "%c[%dm", ESC, COLOR_BROWN);
snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
} else if (ast_opt_force_black_background) {
snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
} else {
snprintf(prepdata, sizeof(prepdata), "%c[%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN);
snprintf(enddata, sizeof(enddata), "%c[%d;%dm", ESC, ATTR_RESET, COLOR_WHITE);
snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
}
}
return 0;
}