Skip to content

sys/log: Improve the console log level indication #3470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sys/log/common/include/log_common/log_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ struct log;
(LOG_LEVEL_WARN == level ? "WARN" :\
(LOG_LEVEL_ERROR == level ? "ERROR" :\
(LOG_LEVEL_CRITICAL == level ? "CRITICAL" :\
"UNKNOWN")))))
(LOG_LEVEL_MAX == level ? "MAX" :\
"UNKNOWN"))))))

/* XXX: These module IDs are defined for backwards compatibility. Application
* code should use the syscfg settings directly. These defines will be removed
Expand Down Expand Up @@ -132,8 +133,8 @@ typedef void log_append_cb(struct log *log, uint32_t idx);

/** @typdef log_notify_rotate_cb
* @brief Callback that is executed each time we are about to rotate a log.
*
* @param log The log that is about to rotate
*
* @param log The log that is about to rotate
*/
typedef void log_notify_rotate_cb(const struct log *log);

Expand Down
79 changes: 39 additions & 40 deletions sys/log/full/src/log_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ log_console_get(void)

#if MYNEWT_VAL(LOG_CONSOLE_PRETTY_WITH_COLORS)

#define CSI "\x1b["
#define CSE "m"

#define INV_COLOR "7;"
#define ANSI_CSI "\x1b["
#define ANSI_SEP ";"
#define ANSI_CSE "m"
#define ANSI_COLOR_RESET ANSI_CSI "0" ANSI_CSE

#define COLOR_BLACK 30
#define COLOR_RED 31
Expand All @@ -72,17 +72,6 @@ log_console_get(void)
#define MOD_COLOR_MIN COLOR_GREEN
#define MOD_COLOR_MAX COLOR_CYAN

#define STRINGIFY(x) #x
#define MK_COLOR(x) CSI STRINGIFY(x) CSE
#define MK_INV_COLOR(x) CSI INV_COLOR STRINGIFY(x) CSE

#define COLOR_DBG ""
#define COLOR_INF MK_COLOR(COLOR_CYAN)
#define COLOR_WRN MK_COLOR(COLOR_YELLOW)
#define COLOR_ERR MK_COLOR(COLOR_RED)
#define COLOR_CRI MK_INV_COLOR(COLOR_RED)

#define COLOR_RESET CSI "0" CSE

static void
log_module_color(uint8_t module, char *color_on, char *color_off)
Expand All @@ -91,39 +80,43 @@ log_module_color(uint8_t module, char *color_on, char *color_off)
*color_off = 0;

if (module) {
sprintf(color_on, CSI "%s%d" CSE,
module < LOG_MODULE_PERUSER ? INV_COLOR : "",
sprintf(color_on, ANSI_CSI "%d" ANSI_SEP "%d" ANSI_CSE,
module < LOG_MODULE_PERUSER ? MYNEWT_VAL(LOG_COLOR_HILIGHT_MODULE) : 0,
MOD_COLOR_MIN + module % (MOD_COLOR_MAX - MOD_COLOR_MIN + 1));
strcpy(color_off, COLOR_RESET);
strcpy(color_off, ANSI_COLOR_RESET);
}
}

#else
#define COLOR_DBG ""
#define COLOR_INF ""
#define COLOR_WRN ""
#define COLOR_ERR ""
#define COLOR_CRI ""
#define COLOR_RESET ""
#define ANSI_CSI ""
#define ANSI_SEP ""
#define ANSI_CSE ""
#define ANSI_COLOR_RESET ""
#define log_module_color(hdr, on, off)
#endif

static const char * const log_level_color[] = {
COLOR_DBG,
COLOR_INF,
COLOR_WRN,
COLOR_ERR,
COLOR_CRI,
static const uint8_t log_level_color_code[] = {
MYNEWT_VAL(LOG_LEVEL_COLOR_CODE_DEBUG),
MYNEWT_VAL(LOG_LEVEL_COLOR_CODE_INFO),
MYNEWT_VAL(LOG_LEVEL_COLOR_CODE_WARNING),
MYNEWT_VAL(LOG_LEVEL_COLOR_CODE_ERROR),
MYNEWT_VAL(LOG_LEVEL_COLOR_CODE_CRITICAL),
/* Add new custom log levels here */
MYNEWT_VAL(LOG_LEVEL_COLOR_CODE_MAXIMUM),
};

static const char * const log_level_str[] = {
"DBG",
"INF",
"WRN",
"ERR",
"CRI",
MYNEWT_VAL(LOG_LEVEL_STRING_DEBUG),
MYNEWT_VAL(LOG_LEVEL_STRING_INFO),
MYNEWT_VAL(LOG_LEVEL_STRING_WARNING),
MYNEWT_VAL(LOG_LEVEL_STRING_ERROR),
MYNEWT_VAL(LOG_LEVEL_STRING_CRITICAL),
/* Add new custom log levels here */
MYNEWT_VAL(LOG_LEVEL_STRING_MAXIMUM),
};

static const int real_log_levels = ARRAY_SIZE(log_level_str) - 1;

void
log_console_print_hdr(const struct log_entry_hdr *hdr)
{
Expand All @@ -134,6 +127,8 @@ log_console_print_hdr(const struct log_entry_hdr *hdr)
const char *module_name = NULL;
char color[11] = "";
char color_off[6] = "";
uint8_t mapped_log_level;
uint8_t color_code;

/* Find module defined in syscfg.logcfg sections */
module_name = log_module_get_name(hdr->ue_module);
Expand All @@ -152,14 +147,18 @@ log_console_print_hdr(const struct log_entry_hdr *hdr)
} else {
image_hash_str[0] = 0;
}
if (hdr->ue_level <= LOG_LEVEL_CRITICAL) {
if (hdr->ue_level < real_log_levels || hdr->ue_level == LOG_LEVEL_MAX) {
mapped_log_level =
hdr->ue_level < real_log_levels ? hdr->ue_level : real_log_levels;
color_code = log_level_color_code[mapped_log_level];
if (MYNEWT_VAL(LOG_CONSOLE_PRETTY_WITH_COLORS)) {
strcpy(level_str_buf, log_level_color[hdr->ue_level]);
strcat(level_str_buf, log_level_str[hdr->ue_level]);
strcat(level_str_buf, COLOR_RESET);
sprintf(level_str_buf, ANSI_CSI "%d" ANSI_SEP "%d" ANSI_CSE "%s" ANSI_COLOR_RESET,
color_code >= 10 ? MYNEWT_VAL(LOG_COLOR_HILIGHT_LEVEL) : 0,
color_code % 10 + 30,
log_level_str[mapped_log_level]);
level_str = level_str_buf;
} else {
level_str = log_level_str[hdr->ue_level];
level_str = log_level_str[mapped_log_level];
}
} else {
sprintf(level_str_buf, "%-3u", hdr->ue_level);
Expand Down
85 changes: 85 additions & 0 deletions sys/log/full/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ syscfg.defs:
description: >
Use color for mod log levels.
value: 0

LOG_CONSOLE_PRETTY_COLOR_MODULES:
description: >
Use color for module names.
Expand Down Expand Up @@ -209,6 +210,90 @@ syscfg.defs:
module is initialized and most recent entry is read.
value: 0

LOG_COLOR_HILIGHT_MODULE:
description: >
Highlight attribute for system modules (terminal support may vary)
0: none, 1: bold, 2: faint, 3: italic, 4: underline, 5: flash, 6: blink, 7: reverse, 8: conceal, 9: strike
value: 7

LOG_COLOR_HILIGHT_LEVEL:
description: >
Highlight attribute for highlighted levels (terminal support may vary)
0: none, 1: bold, 2: faint, 3: italic, 4: underline, 5: flash, 6: blink, 7: reverse, 8: conceal, 9: strike
value: 7

LOG_LEVEL_STRING_DEBUG:
description: >
String for the debug log level
value: '"DBG"'

LOG_LEVEL_COLOR_CODE_DEBUG:
description: >
Color code for the debug log level
0: black, 1: red, 2: green, 3: yellow, 4: blue, 5: magenta, 6: cyan, 7: white, 8: no coloring
n+10: color with highlight, per LOG_COLOR_HILIGHT_LEVEL
value: 8

LOG_LEVEL_STRING_INFO:
description: >
String for the informational log level
value: '"INF"'

LOG_LEVEL_COLOR_CODE_INFO:
description: >
Color code for the informational log level
0: black, 1: red, 2: green, 3: yellow, 4: blue, 5: magenta, 6: cyan, 7: white, 8: no coloring
n+10: color with highlight, per LOG_COLOR_HILIGHT_LEVEL
value: 6

LOG_LEVEL_STRING_WARNING:
description: >
String for the warning log level
value: '"WRN"'

LOG_LEVEL_COLOR_CODE_WARNING:
description: >
Color code for the warning log level
0: black, 1: red, 2: green, 3: yellow, 4: blue, 5: magenta, 6: cyan, 7: white, 8: no coloring
n+10: color with highlight, per LOG_COLOR_HILIGHT_LEVEL
value: 3

LOG_LEVEL_STRING_ERROR:
description: >
String for the error log level
value: '"ERR"'

LOG_LEVEL_COLOR_CODE_ERROR:
description: >
Color code for the error log level
0: black, 1: red, 2: green, 3: yellow, 4: blue, 5: magenta, 6: cyan, 7: white, 8: no coloring
n+10: color with highlight, per LOG_COLOR_HILIGHT_LEVEL
value: 1

LOG_LEVEL_STRING_CRITICAL:
description: >
String for the critical log level
value: '"CRI"'

LOG_LEVEL_COLOR_CODE_CRITICAL:
description: >
Color code for the critical log level
0: black, 1: red, 2: green, 3: yellow, 4: blue, 5: magenta, 6: cyan, 7: white, 8: no coloring
n+10: color with highlight, per LOG_COLOR_HILIGHT_LEVEL
value: 11

LOG_LEVEL_STRING_MAXIMUM:
description: >
String for the maximum log level
value: '"MAX"'

LOG_LEVEL_COLOR_CODE_MAXIMUM:
description: >
Color code for the maximum log level
0: black, 1: red, 2: green, 3: yellow, 4: blue, 5: magenta, 6: cyan, 7: white, 8: no coloring
n+10: color with highlight, per LOG_COLOR_HILIGHT_LEVEL
value: 4

syscfg.vals.CONSOLE_TICKS:
LOG_CONSOLE_PRETTY_WITH_TIMESTAMP: 0

Expand Down
Loading