]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
dmesg: Add a --color option to colorize error and panic messages
authorOndrej Oprala <ooprala@redhat.com>
Tue, 9 Oct 2012 10:32:55 +0000 (12:32 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 16 Oct 2012 14:42:32 +0000 (16:42 +0200)
[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>
sys-utils/dmesg.1
sys-utils/dmesg.c

index 6a8374afbd5c7cefb9e430a98519ef39e1188e1d..860e556e1f996ba607f0300aa52f989ee3cb7824 100644 (file)
@@ -60,6 +60,8 @@ output.
 Print a help text and exit.
 .IP "\fB\-k\fR, \fB\-\-kernel\fR"
 Print kernel messages.
+.IP "\fB\-L\fR, \fB\-\-color\fR"
+Colorize important messages.
 .IP  "\fB\-l\fR, \fB\-\-level \fIlist\fR"
 Restrict output to defined (comma separated)
 .I list
index 4c85f9fceaf28194c82d3817a4823ae785afaea8..f95db408dc263e8d0e6a925081a512c3b52fefe9 100644 (file)
@@ -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,