From 8dd9d924fe012619f465ce0375df692ed26fd4fa Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Fri, 31 Jan 2025 14:57:56 +0800 Subject: [PATCH] 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 --- 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 ce948bcf6c5..0dd2468d5c5 100644 --- a/subsys/logging/log_core.c +++ b/subsys/logging/log_core.c @@ -330,8 +330,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 85d5e578f47..a004a4ab27c 100644 --- a/subsys/logging/log_mgmt.c +++ b/subsys/logging/log_mgmt.c @@ -560,12 +560,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);