This is an automated email from the ASF dual-hosted git repository.

janc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 65eb174e5 sys/log: Improve the console log level indication
65eb174e5 is described below

commit 65eb174e5831f94e5e5c39b2bba6d5c97a8cc835
Author: Peter Csaszar <peter.csas...@juul.com>
AuthorDate: Thu Jul 17 11:46:44 2025 -0700

    sys/log: Improve the console log level indication
    
    Added the option to configure the string & color of MODLOG levels in the
    console printout from sysconfig; also made text highlighting attribute
    (for system log modules & highlighted log levels) configurable.
    
    Since the maximum MODLOG level is often being used to ensure
    unconditional logging, it too received its own configuration support.
---
 sys/log/common/include/log_common/log_common.h |  7 ++-
 sys/log/full/src/log_console.c                 | 79 ++++++++++++------------
 sys/log/full/syscfg.yml                        | 85 ++++++++++++++++++++++++++
 3 files changed, 128 insertions(+), 43 deletions(-)

diff --git a/sys/log/common/include/log_common/log_common.h 
b/sys/log/common/include/log_common/log_common.h
index 9974fa2ef..fec841c79 100644
--- a/sys/log/common/include/log_common/log_common.h
+++ b/sys/log/common/include/log_common/log_common.h
@@ -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
@@ -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);
 
diff --git a/sys/log/full/src/log_console.c b/sys/log/full/src/log_console.c
index 155a7dffe..d836a413f 100644
--- a/sys/log/full/src/log_console.c
+++ b/sys/log/full/src/log_console.c
@@ -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
@@ -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)
@@ -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)
 {
@@ -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);
@@ -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);
diff --git a/sys/log/full/syscfg.yml b/sys/log/full/syscfg.yml
index b6efac004..ffd117a37 100644
--- a/sys/log/full/syscfg.yml
+++ b/sys/log/full/syscfg.yml
@@ -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.
@@ -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
 

Reply via email to