Skip to content

Commit

Permalink
console: added colored print out of log levels
Browse files Browse the repository at this point in the history
When the system is booting the warnings and errors are not be quickly discovered. With this improvement the errors are colored red, the warnings yellow and the notices blue.

Signed-off-by: Eugen Wiens <[email protected]>
Signed-off-by: Sascha Hauer <[email protected]>
  • Loading branch information
EugenWiens authored and saschahauer committed Feb 22, 2018
1 parent 7e879bd commit a019f72
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions common/console_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@

#ifndef CONFIG_CONSOLE_NONE

static const char *colored_log_level[] = {
[MSG_EMERG] = "\033[31mEMERG:\033[0m ", /* red */
[MSG_ALERT] = "\033[31mALERT:\033[0m ", /* red */
[MSG_CRIT] = "\033[31mCRITICAL:\033[0m ", /* red */
[MSG_ERR] = "\033[31mERROR:\033[0m ", /* red */
[MSG_WARNING] = "\033[33mWARNING:\033[0m ", /* yellow */
[MSG_NOTICE] = "\033[34mNOTICE:\033[0m ", /* blue */
};

int barebox_loglevel = CONFIG_DEFAULT_LOGLEVEL;

LIST_HEAD(barebox_logbuf);
Expand Down Expand Up @@ -102,6 +111,18 @@ static void pr_puts(int level, const char *str)
puts(str);
}

static void print_colored_log_level(const int level)
{
if (!console_allow_color())
return;
if (level >= ARRAY_SIZE(colored_log_level))
return;
if (!colored_log_level[level])
return;

pr_puts(level, colored_log_level[level]);
}

int pr_print(int level, const char *fmt, ...)
{
va_list args;
Expand All @@ -111,6 +132,8 @@ int pr_print(int level, const char *fmt, ...)
if (!IS_ENABLED(CONFIG_LOGBUF) && level > barebox_loglevel)
return 0;

print_colored_log_level(level);

va_start(args, fmt);
i = vsprintf(printbuffer, fmt, args);
va_end(args);
Expand All @@ -129,6 +152,8 @@ int dev_printf(int level, const struct device_d *dev, const char *format, ...)
if (!IS_ENABLED(CONFIG_LOGBUF) && level > barebox_loglevel)
return 0;

print_colored_log_level(level);

if (dev->driver && dev->driver->name)
ret += sprintf(printbuffer, "%s ", dev->driver->name);

Expand Down

0 comments on commit a019f72

Please sign in to comment.