diff options
author | Ondrej Oprala | 2012-10-09 12:32:55 +0200 |
---|---|---|
committer | Karel Zak | 2012-10-16 16:42:32 +0200 |
commit | 098ce273cab48c806e6da0ccda197e6309dbfadc (patch) | |
tree | 864eb7c47f706ce77abcc4b429f4936c54713840 /sys-utils/dmesg.c | |
parent | lib/color: add module for work with terminal colors (diff) | |
download | kernel-qcow2-util-linux-098ce273cab48c806e6da0ccda197e6309dbfadc.tar.gz kernel-qcow2-util-linux-098ce273cab48c806e6da0ccda197e6309dbfadc.tar.xz kernel-qcow2-util-linux-098ce273cab48c806e6da0ccda197e6309dbfadc.zip |
dmesg: Add a --color option to colorize error and panic messages
[kzak@redhat.com: - update man page
- colorize critical messages too]
Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Ondrej Oprala <ooprala@redhat.com>
Diffstat (limited to 'sys-utils/dmesg.c')
-rw-r--r-- | sys-utils/dmesg.c | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index 4c85f9fce..f95db408d 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -23,6 +23,7 @@ #include <fcntl.h> #include "c.h" +#include "colors.h" #include "nls.h" #include "strutils.h" #include "xalloc.h" @@ -147,7 +148,8 @@ struct dmesg_control { notime:1, /* don't print timestamp */ delta:1, /* show time deltas */ reltime:1, /* show human readable relative times */ - ctime:1; /* show human readable time */ + ctime:1, /* show human readable time */ + color:1; /* colorize messages */ }; struct dmesg_record { @@ -173,6 +175,24 @@ struct dmesg_record { static int read_kmsg(struct dmesg_control *ctl); +static int set_level_color(int log_level) +{ + switch (log_level) { + case LOG_ALERT: + color_enable(UL_COLOR_BOLD_RED); + return 0; + case LOG_CRIT: + color_enable(UL_COLOR_RED); + return 0; + case LOG_ERR: + color_enable(UL_COLOR_BOLD); + return 0; + default: + break; + } + + return 1; +} static void __attribute__((__noreturn__)) usage(FILE *out) { @@ -193,6 +213,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out) " -f, --facility <list> restrict output to defined facilities\n" " -h, --help display this help and exit\n" " -k, --kernel display kernel messages\n" + " -L, --color colorize messages\n" " -l, --level <list> restrict output to defined levels\n" " -n, --console-level <level> set level of messages printed to console\n" " -r, --raw print the raw message buffer\n" @@ -756,6 +777,7 @@ static void print_record(struct dmesg_control *ctl, struct dmesg_record *rec) { char buf[256]; + int has_color = 0; if (!accept_record(ctl, rec)) return; @@ -839,8 +861,15 @@ static void print_record(struct dmesg_control *ctl, printf("[%5d.%06d] ", (int) rec->tv.tv_sec, (int) rec->tv.tv_usec); mesg: + /* Change the output color for panic and error messages */ + if (ctl->color) + has_color = set_level_color(rec->level) == 0; + safe_fwrite(rec->mesg, rec->mesg_size, stdout); + if (has_color) + color_disable(); + if (*(rec->mesg + rec->mesg_size - 1) != '\n') putchar('\n'); } @@ -942,7 +971,8 @@ static int parse_kmsg_record(struct dmesg_control *ctl, p++; /* A) priority and facility */ - if (ctl->fltr_lev || ctl->fltr_fac || ctl->decode || ctl->raw) + if (ctl->fltr_lev || ctl->fltr_fac || ctl->decode || + ctl->raw || ctl->color) p = parse_faclev(p, &rec->facility, &rec->level); else p = skip_item(p, end, ","); @@ -1046,6 +1076,7 @@ int main(int argc, char *argv[]) static const struct option longopts[] = { { "buffer-size", required_argument, NULL, 's' }, { "clear", no_argument, NULL, 'C' }, + { "color", no_argument, NULL, 'L' }, { "console-level", required_argument, NULL, 'n' }, { "console-off", no_argument, NULL, 'D' }, { "console-on", no_argument, NULL, 'E' }, @@ -1080,7 +1111,7 @@ int main(int argc, char *argv[]) textdomain(PACKAGE); atexit(close_stdout); - while ((c = getopt_long(argc, argv, "CcDdEeF:f:hkl:n:rSs:TtuVwx", + while ((c = getopt_long(argc, argv, "CcDdEeF:f:hkLl:n:rSs:TtuVwx", longopts, NULL)) != -1) { err_exclusive_options(c, longopts, excl, excl_st); @@ -1123,6 +1154,10 @@ int main(int argc, char *argv[]) ctl.fltr_fac = 1; setbit(ctl.facilities, FAC_BASE(LOG_KERN)); break; + case 'L': + if (colors_init()) + ctl.color = 1; + break; case 'l': ctl.fltr_lev= 1; if (string_to_bitarray(optarg, |