From ab9f628bc6d0a8aeae18d6a8c4cbc4c4d21e4e4c Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Fri, 31 Jan 2025 14:57:56 +0800 Subject: [PATCH 1/2] logging: init backend id regardless of autostart The `id` is basically a compile-time constant. Setting it every time the backend is enabled is unnecessary. Instead, set it on `z_log_init()` regardless of whether or not it requires to be `autostart`ed. Fixes an issue where the `filter_get`/`filter_set` accessed the wrong index and displayed the wrong log level when user accesses the status of an uninitialized backend via: `log backend status`. Also fixes an issue when user tries to list the backends via: `log list_backends`, where all uninitialized backends will have ID = 0. Signed-off-by: Yong Cong Sin Signed-off-by: Yong Cong Sin (cherry picked from commit 8dd9d924fe012619f465ce0375df692ed26fd4fa) --- subsys/logging/log_core.c | 8 +++++++- subsys/logging/log_mgmt.c | 6 ------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/subsys/logging/log_core.c b/subsys/logging/log_core.c index 841677257dc31..00d69529975d8 100644 --- a/subsys/logging/log_core.c +++ b/subsys/logging/log_core.c @@ -328,8 +328,14 @@ static uint32_t z_log_init(bool blocking, bool can_sleep) int backend_index = 0; - /* Activate autostart backends */ STRUCT_SECTION_FOREACH(log_backend, backend) { + uint32_t id; + /* As first slot in filtering mask is reserved, backend ID has offset.*/ + id = LOG_FILTER_FIRST_BACKEND_SLOT_IDX; + id += backend - log_backend_get(0); + log_backend_id_set(backend, id); + + /* Activate autostart backends */ if (backend->autostart) { log_backend_init(backend); diff --git a/subsys/logging/log_mgmt.c b/subsys/logging/log_mgmt.c index dbde3d24b44ce..b211b38066132 100644 --- a/subsys/logging/log_mgmt.c +++ b/subsys/logging/log_mgmt.c @@ -553,12 +553,6 @@ void log_backend_enable(struct log_backend const *const backend, void *ctx, uint32_t level) { - /* As first slot in filtering mask is reserved, backend ID has offset.*/ - uint32_t id = LOG_FILTER_FIRST_BACKEND_SLOT_IDX; - - id += backend - log_backend_get(0); - - log_backend_id_set(backend, id); backend->cb->level = level; backend_filter_set(backend, level); log_backend_activate(backend, ctx); From 2cfbc493bed2863290de6a9288575b871ca60394 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Fri, 31 Jan 2025 15:30:15 +0800 Subject: [PATCH 2/2] logging: log_cmds: init uninitialized backend on `log_go()` For backends that do not autostart themselves, initialize & enable them on `log backend go`, so that they function properly. Signed-off-by: Yong Cong Sin Signed-off-by: Yong Cong Sin (cherry picked from commit f840a35be3438abdc18619c1f0609e7d7f98b20c) --- include/zephyr/logging/log_backend.h | 2 ++ subsys/logging/log_cmds.c | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/zephyr/logging/log_backend.h b/include/zephyr/logging/log_backend.h index e772ad2b9c82d..c68d210af7350 100644 --- a/include/zephyr/logging/log_backend.h +++ b/include/zephyr/logging/log_backend.h @@ -83,6 +83,7 @@ struct log_backend_control_block { void *ctx; uint8_t id; bool active; + bool initialized; /* Initialization level. */ uint8_t level; @@ -140,6 +141,7 @@ static inline void log_backend_init(const struct log_backend *const backend) if (backend->api->init) { backend->api->init(backend); } + backend->cb->initialized = true; } /** diff --git a/subsys/logging/log_cmds.c b/subsys/logging/log_cmds.c index 79e4457184f62..fc44f4be81f42 100644 --- a/subsys/logging/log_cmds.c +++ b/subsys/logging/log_cmds.c @@ -342,7 +342,19 @@ static int log_go(const struct shell *sh, char **argv) { if (backend || !IS_ENABLED(CONFIG_LOG_FRONTEND)) { - log_backend_activate(backend, backend->cb->ctx); + if (!backend->cb->initialized) { + log_backend_init(backend); + while (log_backend_is_ready(backend) != 0) { + if (IS_ENABLED(CONFIG_MULTITHREADING)) { + k_msleep(10); + } + } + if (log_backend_is_ready(backend) == 0) { + log_backend_enable(backend, backend->cb->ctx, CONFIG_LOG_MAX_LEVEL); + } + } else { + log_backend_activate(backend, backend->cb->ctx); + } return 0; }