Skip to content

Commit 7de6d49

Browse files
committed
sys/log: Add string & color to max log level
The maximum MODLOG level is often being used to ensure unconditional logging, therefore it deserves its own verbal string and color.
1 parent 4e013e8 commit 7de6d49

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

sys/log/common/include/log_common/log_common.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ struct log;
4949
(LOG_LEVEL_WARN == level ? "WARN" :\
5050
(LOG_LEVEL_ERROR == level ? "ERROR" :\
5151
(LOG_LEVEL_CRITICAL == level ? "CRITICAL" :\
52-
"UNKNOWN")))))
52+
(LOG_LEVEL_MAX == level ? "MAX" :\
53+
"UNKNOWN"))))))
5354

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

133134
/** @typdef log_notify_rotate_cb
134135
* @brief Callback that is executed each time we are about to rotate a log.
135-
*
136-
* @param log The log that is about to rotate
136+
*
137+
* @param log The log that is about to rotate
137138
*/
138139
typedef void log_notify_rotate_cb(const struct log *log);
139140

sys/log/full/src/log_console.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ log_console_get(void)
8181
#define COLOR_WRN MK_COLOR(COLOR_YELLOW)
8282
#define COLOR_ERR MK_COLOR(COLOR_RED)
8383
#define COLOR_CRI MK_INV_COLOR(COLOR_RED)
84+
#define COLOR_MAX MK_COLOR(COLOR_BLUE)
8485

8586
#define COLOR_RESET CSI "0" CSE
8687

@@ -104,6 +105,7 @@ log_module_color(uint8_t module, char *color_on, char *color_off)
104105
#define COLOR_WRN ""
105106
#define COLOR_ERR ""
106107
#define COLOR_CRI ""
108+
#define COLOR_MAX ""
107109
#define COLOR_RESET ""
108110
#define log_module_color(hdr, on, off)
109111
#endif
@@ -114,6 +116,8 @@ static const char * const log_level_color[] = {
114116
COLOR_WRN,
115117
COLOR_ERR,
116118
COLOR_CRI,
119+
/* Add new custom log levels here */
120+
COLOR_MAX,
117121
};
118122

119123
static const char * const log_level_str[] = {
@@ -122,8 +126,12 @@ static const char * const log_level_str[] = {
122126
"WRN",
123127
"ERR",
124128
"CRI",
129+
/* Add new custom log levels here */
130+
"MAX",
125131
};
126132

133+
static int real_log_levels = ARRAY_SIZE(log_level_str) - 1;
134+
127135
void
128136
log_console_print_hdr(const struct log_entry_hdr *hdr)
129137
{
@@ -134,6 +142,7 @@ log_console_print_hdr(const struct log_entry_hdr *hdr)
134142
const char *module_name = NULL;
135143
char color[11] = "";
136144
char color_off[6] = "";
145+
uint8_t mapped_log_level;
137146

138147
/* Find module defined in syscfg.logcfg sections */
139148
module_name = log_module_get_name(hdr->ue_module);
@@ -152,14 +161,15 @@ log_console_print_hdr(const struct log_entry_hdr *hdr)
152161
} else {
153162
image_hash_str[0] = 0;
154163
}
155-
if (hdr->ue_level <= LOG_LEVEL_CRITICAL) {
164+
if (hdr->ue_level < real_log_levels || hdr->ue_level == LOG_LEVEL_MAX) {
165+
mapped_log_level = hdr->ue_level < real_log_levels ? hdr->ue_level : real_log_levels;
156166
if (MYNEWT_VAL(LOG_CONSOLE_PRETTY_WITH_COLORS)) {
157-
strcpy(level_str_buf, log_level_color[hdr->ue_level]);
158-
strcat(level_str_buf, log_level_str[hdr->ue_level]);
167+
strcpy(level_str_buf, log_level_color[mapped_log_level]);
168+
strcat(level_str_buf, log_level_str[mapped_log_level]);
159169
strcat(level_str_buf, COLOR_RESET);
160170
level_str = level_str_buf;
161171
} else {
162-
level_str = log_level_str[hdr->ue_level];
172+
level_str = log_level_str[mapped_log_level];
163173
}
164174
} else {
165175
sprintf(level_str_buf, "%-3u", hdr->ue_level);

0 commit comments

Comments
 (0)