author | Tanguy Pruvot <tanguy.pruvot@gmail.com> | 2014-08-10 12:34:51 (GMT) |
---|---|---|
committer | Tanguy Pruvot <tanguy.pruvot@gmail.com> | 2014-08-10 15:34:49 (GMT) |
commit | 39046bad23a7b0379b401561b9b613a6623bd2fa (patch) | |
tree | f3c0ec7f8e91c27170ccfadce5c7673370b11dbd | |
parent | a20778a7a0e0b00dfc28b2a955ba166d027e45e6 (diff) | |
download | busybox-39046bad23a7b0379b401561b9b613a6623bd2fa.zip busybox-39046bad23a7b0379b401561b9b613a6623bd2fa.tar.gz busybox-39046bad23a7b0379b401561b9b613a6623bd2fa.tar.bz2 |
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 <tanguy.pruvot@gmail.com>
Change-Id: Ia0239a0bea02da01cae15736150dfee4fe66dcdb
-rw-r--r-- | busybox-full.config | 1 | ||||
-rw-r--r-- | busybox-minimal.config | 1 | ||||
-rw-r--r-- | util-linux/Config.src | 8 | ||||
-rw-r--r-- | util-linux/dmesg.c | 65 |
4 files changed, 47 insertions, 28 deletions
diff --git a/busybox-full.config b/busybox-full.config index 27221c6..95664c4 100644 --- a/busybox-full.config +++ b/busybox-full.config @@ -538,6 +538,7 @@ CONFIG_BLKID=y CONFIG_FEATURE_BLKID_TYPE=y CONFIG_DMESG=y # CONFIG_FEATURE_DMESG_PRETTY is not set +CONFIG_FEATURE_DMESG_COLOR=y # CONFIG_FBSET is not set # CONFIG_FEATURE_FBSET_FANCY is not set # CONFIG_FEATURE_FBSET_READMODE is not set diff --git a/busybox-minimal.config b/busybox-minimal.config index fe852c2..ad8574f 100644 --- a/busybox-minimal.config +++ b/busybox-minimal.config @@ -538,6 +538,7 @@ CONFIG_BLKID=y CONFIG_FEATURE_BLKID_TYPE=y CONFIG_DMESG=y # CONFIG_FEATURE_DMESG_PRETTY is not set +CONFIG_FEATURE_DMESG_COLOR=y # CONFIG_FBSET is not set # CONFIG_FEATURE_FBSET_FANCY is not set # CONFIG_FEATURE_FBSET_READMODE is not set diff --git a/util-linux/Config.src b/util-linux/Config.src index 8c5343b..9af923e 100644 --- a/util-linux/Config.src +++ b/util-linux/Config.src @@ -81,6 +81,14 @@ config FEATURE_DMESG_PRETTY <6>BIOS-provided physical RAM map: <6> BIOS-e820: 0000000000000000 - 000000000009f000 (usable) +config FEATURE_DMESG_COLOR + bool "Colored dmesg output" + default y + depends on DMESG + help + Allow to show errors and warnings in different colors + dmesg -C + config FBSET bool "fbset" default y diff --git a/util-linux/dmesg.c b/util-linux/dmesg.c index 84173ce..ba52036 100644 --- a/util-linux/dmesg.c +++ b/util-linux/dmesg.c @@ -10,23 +10,37 @@ */ //usage:#define dmesg_trivial_usage -//usage: "[-c] [-n LEVEL] [-r] [-s SIZE] [-C]" +//usage: "[-c] [-n LEVEL] [-s SIZE]" +//usage: IF_FEATURE_DMESG_PRETTY(" [-r]") +//usage: IF_FEATURE_DMESG_COLOR(" [-C]") //usage:#define dmesg_full_usage "\n\n" //usage: "Print or control the kernel ring buffer\n" //usage: "\n -c Clear ring buffer after printing" //usage: "\n -n LEVEL Set console logging level" -//usage: "\n -r Show level prefix" //usage: "\n -s SIZE Buffer size" -//usage: "\n -C Colored output" +//usage: IF_FEATURE_DMESG_PRETTY( +//usage: "\n -r Show level prefix") +//usage: IF_FEATURE_DMESG_COLOR( +//usage: "\n -C Colored output") #include <sys/klog.h> #include "libbb.h" +#if ENABLE_FEATURE_DMESG_COLOR #define COLOR_DEFAULT 0 -#define COLOR_WHITE 231 -#define COLOR_YELLOW 226 -#define COLOR_ORANGE 166 -#define COLOR_RED 196 +#define COLOR_WHITE 97 +#define COLOR_YELLOW 93 +#define COLOR_ORANGE 33 +#define COLOR_RED 91 + +static void set_color(int color) +{ + printf("%c[%dm", 0x1B, color); +} + +#else +#define set_color(c) {} +#endif int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int dmesg_main(int argc UNUSED_PARAM, char **argv) @@ -34,12 +48,14 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv) int len, level; char *buf; unsigned opts; + int color = 0; enum { OPT_c = 1 << 0, OPT_s = 1 << 1, OPT_n = 1 << 2, OPT_r = 1 << 3, - OPT_C = 1 << 4 + OPT_C = 1 << 4, + OPT_end }; opt_complementary = "s+:n+"; /* numeric */ @@ -67,14 +83,15 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv) if ((ENABLE_FEATURE_DMESG_PRETTY || (opts & OPT_C)) && !(opts & OPT_r)) { int last = '\n'; - int in = 0, l, color; - char pfx[16], *lvl; + int in = 0; /* Skip <[0-9]+> at the start of lines */ while (1) { if (last == '\n' && buf[in] == '<') { + +#if ENABLE_FEATURE_DMESG_COLOR if (opts & OPT_C) { - lvl = buf + in + 1; + char *lvl = buf + in + 1; sscanf(lvl, "%d", &level); switch (level) { @@ -84,18 +101,14 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv) case 4: color = COLOR_ORANGE; break; case 5: color = COLOR_YELLOW; break; case 7: color = COLOR_WHITE; break; - case 6: // common dmesg info - default: color = COLOR_DEFAULT; + case 6: /* common dmesg info */ + default: + color = COLOR_DEFAULT; } - if (color != COLOR_DEFAULT) - l = sprintf(pfx, "%c[%d;%d;%dm", - 0x1B, 38, 5, color); - else - l = sprintf(pfx, "%c[%dm", 0x1B, 0); - - full_write(STDOUT_FILENO, pfx, l); + set_color(color); } +#endif while (buf[in++] != '>' && in < len) ; } else { @@ -105,17 +118,9 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv) if (in >= len) break; } - - if (opts & OPT_C) { - /* Reset default terminal color */ - l = sprintf(pfx, "%c[%dm", 0x1B, 0); - full_write(STDOUT_FILENO, pfx, l); - } - /* Make sure we end with a newline */ if (last != '\n') bb_putchar('\n'); - } else { full_write(STDOUT_FILENO, buf, len); if (buf[len-1] != '\n') @@ -124,5 +129,9 @@ int dmesg_main(int argc UNUSED_PARAM, char **argv) if (ENABLE_FEATURE_CLEAN_UP) free(buf); + /* Reset default terminal color */ + if (color) + set_color(0); + return EXIT_SUCCESS; } |