Skip to content

Commit 39046ba

Browse files
committed
dmesg: rework color feature, compat. with more
when used with less or more, the colors were not in the same buffer, resulting blocs of colors. Also, reset color numbers to the 16 ANSI ones. Reduce the diff with busybox to be able to send the patch. This is the first full busybox aosp feature with the new build system Signed-off-by: Tanguy Pruvot <[email protected]> Change-Id: Ia0239a0bea02da01cae15736150dfee4fe66dcdb
1 parent a20778a commit 39046ba

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

busybox-full.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ CONFIG_BLKID=y
538538
CONFIG_FEATURE_BLKID_TYPE=y
539539
CONFIG_DMESG=y
540540
# CONFIG_FEATURE_DMESG_PRETTY is not set
541+
CONFIG_FEATURE_DMESG_COLOR=y
541542
# CONFIG_FBSET is not set
542543
# CONFIG_FEATURE_FBSET_FANCY is not set
543544
# CONFIG_FEATURE_FBSET_READMODE is not set

busybox-minimal.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ CONFIG_BLKID=y
538538
CONFIG_FEATURE_BLKID_TYPE=y
539539
CONFIG_DMESG=y
540540
# CONFIG_FEATURE_DMESG_PRETTY is not set
541+
CONFIG_FEATURE_DMESG_COLOR=y
541542
# CONFIG_FBSET is not set
542543
# CONFIG_FEATURE_FBSET_FANCY is not set
543544
# CONFIG_FEATURE_FBSET_READMODE is not set

util-linux/Config.src

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ config FEATURE_DMESG_PRETTY
8181
<6>BIOS-provided physical RAM map:
8282
<6> BIOS-e820: 0000000000000000 - 000000000009f000 (usable)
8383

84+
config FEATURE_DMESG_COLOR
85+
bool "Colored dmesg output"
86+
default y
87+
depends on DMESG
88+
help
89+
Allow to show errors and warnings in different colors
90+
dmesg -C
91+
8492
config FBSET
8593
bool "fbset"
8694
default y

util-linux/dmesg.c

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,52 @@
1010
*/
1111

1212
//usage:#define dmesg_trivial_usage
13-
//usage: "[-c] [-n LEVEL] [-r] [-s SIZE] [-C]"
13+
//usage: "[-c] [-n LEVEL] [-s SIZE]"
14+
//usage: IF_FEATURE_DMESG_PRETTY(" [-r]")
15+
//usage: IF_FEATURE_DMESG_COLOR(" [-C]")
1416
//usage:#define dmesg_full_usage "\n\n"
1517
//usage: "Print or control the kernel ring buffer\n"
1618
//usage: "\n -c Clear ring buffer after printing"
1719
//usage: "\n -n LEVEL Set console logging level"
18-
//usage: "\n -r Show level prefix"
1920
//usage: "\n -s SIZE Buffer size"
20-
//usage: "\n -C Colored output"
21+
//usage: IF_FEATURE_DMESG_PRETTY(
22+
//usage: "\n -r Show level prefix")
23+
//usage: IF_FEATURE_DMESG_COLOR(
24+
//usage: "\n -C Colored output")
2125

2226
#include <sys/klog.h>
2327
#include "libbb.h"
2428

29+
#if ENABLE_FEATURE_DMESG_COLOR
2530
#define COLOR_DEFAULT 0
26-
#define COLOR_WHITE 231
27-
#define COLOR_YELLOW 226
28-
#define COLOR_ORANGE 166
29-
#define COLOR_RED 196
31+
#define COLOR_WHITE 97
32+
#define COLOR_YELLOW 93
33+
#define COLOR_ORANGE 33
34+
#define COLOR_RED 91
35+
36+
static void set_color(int color)
37+
{
38+
printf("%c[%dm", 0x1B, color);
39+
}
40+
41+
#else
42+
#define set_color(c) {}
43+
#endif
3044

3145
int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
3246
int dmesg_main(int argc UNUSED_PARAM, char **argv)
3347
{
3448
int len, level;
3549
char *buf;
3650
unsigned opts;
51+
int color = 0;
3752
enum {
3853
OPT_c = 1 << 0,
3954
OPT_s = 1 << 1,
4055
OPT_n = 1 << 2,
4156
OPT_r = 1 << 3,
42-
OPT_C = 1 << 4
57+
OPT_C = 1 << 4,
58+
OPT_end
4359
};
4460

4561
opt_complementary = "s+:n+"; /* numeric */
@@ -67,14 +83,15 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv)
6783

6884
if ((ENABLE_FEATURE_DMESG_PRETTY || (opts & OPT_C)) && !(opts & OPT_r)) {
6985
int last = '\n';
70-
int in = 0, l, color;
71-
char pfx[16], *lvl;
86+
int in = 0;
7287

7388
/* Skip <[0-9]+> at the start of lines */
7489
while (1) {
7590
if (last == '\n' && buf[in] == '<') {
91+
92+
#if ENABLE_FEATURE_DMESG_COLOR
7693
if (opts & OPT_C) {
77-
lvl = buf + in + 1;
94+
char *lvl = buf + in + 1;
7895
sscanf(lvl, "%d", &level);
7996

8097
switch (level) {
@@ -84,18 +101,14 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv)
84101
case 4: color = COLOR_ORANGE; break;
85102
case 5: color = COLOR_YELLOW; break;
86103
case 7: color = COLOR_WHITE; break;
87-
case 6: // common dmesg info
88-
default: color = COLOR_DEFAULT;
104+
case 6: /* common dmesg info */
105+
default:
106+
color = COLOR_DEFAULT;
89107
}
90108

91-
if (color != COLOR_DEFAULT)
92-
l = sprintf(pfx, "%c[%d;%d;%dm",
93-
0x1B, 38, 5, color);
94-
else
95-
l = sprintf(pfx, "%c[%dm", 0x1B, 0);
96-
97-
full_write(STDOUT_FILENO, pfx, l);
109+
set_color(color);
98110
}
111+
#endif
99112
while (buf[in++] != '>' && in < len)
100113
;
101114
} else {
@@ -105,17 +118,9 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv)
105118
if (in >= len)
106119
break;
107120
}
108-
109-
if (opts & OPT_C) {
110-
/* Reset default terminal color */
111-
l = sprintf(pfx, "%c[%dm", 0x1B, 0);
112-
full_write(STDOUT_FILENO, pfx, l);
113-
}
114-
115121
/* Make sure we end with a newline */
116122
if (last != '\n')
117123
bb_putchar('\n');
118-
119124
} else {
120125
full_write(STDOUT_FILENO, buf, len);
121126
if (buf[len-1] != '\n')
@@ -124,5 +129,9 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv)
124129

125130
if (ENABLE_FEATURE_CLEAN_UP) free(buf);
126131

132+
/* Reset default terminal color */
133+
if (color)
134+
set_color(0);
135+
127136
return EXIT_SUCCESS;
128137
}

0 commit comments

Comments
 (0)