You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2019/01/09 10:07:06 UTC
[mynewt-core] 05/08: [wip] sys/log: Add pretty-printing option for
log_console
This is an automated email from the ASF dual-hosted git repository.
andk pushed a commit to branch bus-dev-test
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 16348e78c73c77989917e4d344b9fd7cc1957453
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Wed Nov 21 15:24:01 2018 +0100
[wip] sys/log: Add pretty-printing option for log_console
---
sys/log/full/src/log_console.c | 135 ++++++++++++++++++++++++++++++++++-------
sys/log/full/syscfg.yml | 30 +++++++++
2 files changed, 142 insertions(+), 23 deletions(-)
diff --git a/sys/log/full/src/log_console.c b/sys/log/full/src/log_console.c
index 6a00a17..cb736fa 100644
--- a/sys/log/full/src/log_console.c
+++ b/sys/log/full/src/log_console.c
@@ -27,53 +27,142 @@
static struct log log_console;
+#if MYNEWT_VAL(LOG_CONSOLE_FORMATTER) == 1
+#define LOG_CONSOLE_COLOR_DEFAULT "\x1B[0m"
+#if MYNEWT_VAL(LOG_CONSOLE_PRETTY_BRIGHT_COLORS)
+#define LOG_CONSOLE_COLOR_RED "\x1B[1;31m"
+#define LOG_CONSOLE_COLOR_YELLOW "\x1B[1;33m"
+#define LOG_CONSOLE_COLOR_MAGENTA "\x1B[1;35m"
+#else
+#define LOG_CONSOLE_COLOR_RED "\x1B[31m"
+#define LOG_CONSOLE_COLOR_YELLOW "\x1B[33m"
+#define LOG_CONSOLE_COLOR_MAGENTA "\x1B[35m"
+#endif
+
+#define LOG_CONSOLE_LEVEL_DEF(_str, _color) \
+ { \
+ .level_str = (_str), \
+ .color_seq = (_color), \
+ }
+
+#define LOG_CONSOLE_LEVEL_DEF_MAX \
+ ((sizeof(log_console_level_defs) / sizeof(log_console_level_defs[0])) - 1)
+
+struct log_console_level_def {
+ const char *level_str;
+ const char *color_seq;
+};
+
+static const struct log_console_level_def log_console_level_defs[] = {
+ LOG_CONSOLE_LEVEL_DEF("D", LOG_CONSOLE_COLOR_DEFAULT),
+ LOG_CONSOLE_LEVEL_DEF("I", LOG_CONSOLE_COLOR_DEFAULT),
+ LOG_CONSOLE_LEVEL_DEF("W", LOG_CONSOLE_COLOR_YELLOW),
+ LOG_CONSOLE_LEVEL_DEF("E", LOG_CONSOLE_COLOR_RED),
+ LOG_CONSOLE_LEVEL_DEF("C", LOG_CONSOLE_COLOR_RED),
+ LOG_CONSOLE_LEVEL_DEF("U", LOG_CONSOLE_COLOR_MAGENTA), /* unknown level */
+};
+#endif
+
struct log *
log_console_get(void)
{
return &log_console;
}
+#if MYNEWT_VAL(LOG_CONSOLE_FORMATTER) == 0
static void
-log_console_print_hdr(const struct log_entry_hdr *hdr)
+log_console_write(const struct log_entry_hdr *hdr, const void *body, int length)
{
- console_printf("[ts=%lluus, mod=%u level=%u] ",
- hdr->ue_ts, hdr->ue_module, hdr->ue_level);
-}
+ if (!console_is_init()) {
+ return;
+ }
-static int
-log_console_append(struct log *log, void *buf, int len)
+ if (!console_is_midline) {
+ console_printf("[ts=%lluus, mod=%u level=%u] ", hdr->ue_ts,
+ hdr->ue_module, hdr->ue_level);
+ }
+
+ console_write(body, length);
+}
+#elif MYNEWT_VAL(LOG_CONSOLE_FORMATTER) == 1
+static void
+log_console_write(const struct log_entry_hdr *hdr, const void *body, int length)
{
- struct log_entry_hdr *hdr;
+ const struct log_console_level_def *def;
+ const char *mod_name;
+ int level;
+ bool is_midline;
if (!console_is_init()) {
- return (0);
+ return;
}
- if (!console_is_midline) {
- hdr = (struct log_entry_hdr *) buf;
- log_console_print_hdr(hdr);
+ level = min(hdr->ue_level, LOG_CONSOLE_LEVEL_DEF_MAX);
+ def = &log_console_level_defs[level];
+
+ is_midline = console_is_midline;
+
+ if (!is_midline) {
+#if MYNEWT_VAL(LOG_CONSOLE_PRETTY_TS_WIDTH) > 0
+ console_printf("%s%0*llu ", def->color_seq,
+ MYNEWT_VAL(LOG_CONSOLE_PRETTY_TS_WIDTH),
+ hdr->ue_ts / MYNEWT_VAL(LOG_CONSOLE_PRETTY_TS_DIV));
+#else
+ console_printf("%s", def->color_seq);
+#endif
+
+ console_write_str(def->level_str);
+ console_write_str("/");
+
+ mod_name = log_module_get_name(hdr->ue_module);
+ if (mod_name) {
+ console_write_str(mod_name);
+ } else {
+ console_printf("%u", hdr->ue_module);
+ }
+
+#if MYNEWT_VAL(LOG_CONSOLE_PRETTY_USE_TASK_NAME)
+ console_write_str(" (");
+ console_write_str(os_sched_get_current_task()->t_name);
+ console_write_str("): ");
+#endif
}
- console_write((char *) buf + LOG_ENTRY_HDR_SIZE, len - LOG_ENTRY_HDR_SIZE);
+ if (((const char *)body)[length - 1] == '\n') {
+ /*
+ * Need to reset to default color before printing \n as otherwise escape
+ * sequence would make console think it's mid-line.
+ */
+ console_write(body, length - 1);
+ console_write_str(LOG_CONSOLE_COLOR_DEFAULT);
+ console_write_str("\n");
+ } else {
+ console_write(body, length);
+ }
+}
+#else
+#error Unsupported console formatter selected.
+#endif
- return (0);
+static int
+log_console_append(struct log *log, void *buf, int len)
+{
+ struct log_entry_hdr *hdr = (struct log_entry_hdr *) buf;
+ const void *body = buf + LOG_ENTRY_HDR_SIZE;
+ int body_len = len - LOG_ENTRY_HDR_SIZE;
+
+ log_console_write(hdr, body, body_len);
+
+ return 0;
}
static int
log_console_append_body(struct log *log, const struct log_entry_hdr *hdr,
const void *body, int body_len)
{
- if (!console_is_init()) {
- return (0);
- }
-
- if (!console_is_midline) {
- log_console_print_hdr(hdr);
- }
-
- console_write(body, body_len);
+ log_console_write(hdr, body, body_len);
- return (0);
+ return 0;
}
static int
diff --git a/sys/log/full/syscfg.yml b/sys/log/full/syscfg.yml
index 9e530fd..61556e6 100644
--- a/sys/log/full/syscfg.yml
+++ b/sys/log/full/syscfg.yml
@@ -50,6 +50,33 @@ syscfg.defs:
LOG_CONSOLE:
description: 'Support logging to console.'
value: 1
+ LOG_CONSOLE_FORMATTER:
+ description: >
+ Defines formatter used for console output. Supported values are:
+ 0 = standard formatter
+ 1 = pretty-printing formatter
+ value: 1
+ LOG_CONSOLE_PRETTY_BRIGHT_COLORS:
+ description: >
+ Use bright colors when using pretty-printing. Useful on dark
+ terminals for better contrast.
+ value: 1
+ LOG_CONSOLE_PRETTY_TS_WIDTH:
+ description: >
+ Width of timestamp field when using pretty-printing formatter.
+ Set to 0 to disable printing of timestamp.
+ value: 0
+ LOG_CONSOLE_PRETTY_TS_DIV:
+ description: >
+ Divisor for timestamp value when pretty-printing formatter.
+ Each timestamp will be divided by this number, i.e. a value of 1000
+ will scale timestamp from default [ns] to [us]. This reduces width
+ of printed timestamp but also reduces timestamp resolution,
+ value: 1000
+ LOG_CONSOLE_PRETTY_USE_TASK_NAME:
+ description: >
+ Enables printing of current task name for each log entry.
+ value: 1
LOG_CLI:
description: 'Expose "log" command in shell.'
@@ -100,3 +127,6 @@ syscfg.defs:
description: >
Sysinit stage for logging to the second image slot.
value: 101
+
+syscfg.restrictions:
+ - LOG_CONSOLE_FORMATTER == 0 || LOG_CONSOLE_FORMATTER == 1