#include <fcntl.h>
#include "c.h"
+#include "colors.h"
#include "nls.h"
#include "strutils.h"
#include "xalloc.h"
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 {
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)
{
" -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"
struct dmesg_record *rec)
{
char buf[256];
+ int has_color = 0;
if (!accept_record(ctl, rec))
return;
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');
}
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, ",");
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' },
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);
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,