Dynamic UART Logging Control in Zephyr

Hello everyone, I’m currently working on a project where we’re upgrading from NCS 2.3.0 to NCS 2.6.1, and we’ve encountered an interesting challenge with UART logging. In our setup, we use USB UART for development purposes, but once the product is ready for deployment, we need to disable this logging. However, we also want the flexibility to re-enable it if needed for debugging purposes. This has led us to explore dynamic logging control during runtime.

In our current implementation with NCS 2.3.1, we disable UART logging by retrieving the backend using log_backend_get_by_name() and then calling log_backend_disable(). Re-enabling involves a similar process with log_backend_enable(). However, when we moved to NCS 2.6.1, we encountered a crash when trying to re-enable logging due to the ctx parameter being NULL. After some research, we found that passing backend->cb->ctx instead of NULL resolved the issue, but this feels like a workaround rather than a clean solution.

We’re wondering if there’s a more elegant API-based approach to achieve this without delving into implementation details that are likely meant to be internal. Has anyone else faced similar challenges or found a cleaner solution? Any insights or suggestions would be greatly appreciated!