From 76f74344a89ba240d700aaae2dcf9fde6e8e2fbd Mon Sep 17 00:00:00 2001 From: Jonathan Rico Date: Wed, 8 Jun 2022 14:49:43 -0700 Subject: [PATCH] Shell: kernel: Add runtime log filtering command Use `kernel log-level modulename severity` Also enable it for the Bluetooth Shell. Then one can compile-in a lot of BT modules like so: CONFIG_BT_DEBUG_HCI_CORE=y CONFIG_BT_DEBUG_L2CAP=y CONFIG_BT_DEBUG_ATT=y CONFIG_BT_DEBUG_GATT=y And at runtime select only, e.g. GATT kernel log-level bt_hci_core 0 kernel log-level bt_l2cap 0 kernel log-level bt_att 0 And then re-enable L2CAP if needed later kernel log-level bt_l2cap 4 And so on.. Signed-off-by: Jonathan Rico --- subsys/shell/modules/kernel_service.c | 42 +++++++++++++++++++++++++++ tests/bluetooth/shell/prj.conf | 3 ++ 2 files changed, 45 insertions(+) diff --git a/subsys/shell/modules/kernel_service.c b/subsys/shell/modules/kernel_service.c index 10fbdad2e71..b5c1136dfd5 100644 --- a/subsys/shell/modules/kernel_service.c +++ b/subsys/shell/modules/kernel_service.c @@ -16,6 +16,9 @@ #include #include #include +#if defined(CONFIG_LOG_RUNTIME_FILTERING) +#include +#endif static int cmd_kernel_version(const struct shell *shell, size_t argc, char **argv) @@ -240,6 +243,41 @@ static int cmd_kernel_sleep(const struct shell *sh, return 0; } +#if defined(CONFIG_LOG_RUNTIME_FILTERING) +static int cmd_kernel_log_level_set(const struct shell *sh, + size_t argc, char **argv) +{ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + int err = 0; + + uint8_t severity = shell_strtoul(argv[2], 10, &err); + + if (err) { + shell_error(sh, "Unable to parse log severity (err %d)", err); + + return err; + } + + if (severity > LOG_LEVEL_DBG) { + shell_error(sh, "Invalid log level: %d", severity); + shell_help(sh); + return SHELL_CMD_HELP_PRINTED; + } + + int source_id = log_source_id_get(argv[1]); + + /* log_filter_set() takes an int16_t for the source ID */ + if (source_id < 0) { + shell_error(sh, "Unable to find log source: %s", argv[1]); + } + + log_filter_set(NULL, 0, (int16_t)source_id, severity); + + return 0; +} +#endif + #if defined(CONFIG_REBOOT) static int cmd_kernel_reboot_warm(const struct shell *shell, size_t argc, char **argv) @@ -285,6 +323,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_kernel, SHELL_CMD(uptime, NULL, "Kernel uptime.", cmd_kernel_uptime), SHELL_CMD(version, NULL, "Kernel version.", cmd_kernel_version), SHELL_CMD_ARG(sleep, NULL, "ms", cmd_kernel_sleep, 2, 0), +#if defined(CONFIG_LOG_RUNTIME_FILTERING) + SHELL_CMD_ARG(log-level, NULL, " ", + cmd_kernel_log_level_set, 3, 0), +#endif SHELL_SUBCMD_SET_END /* Array terminated. */ ); diff --git a/tests/bluetooth/shell/prj.conf b/tests/bluetooth/shell/prj.conf index 2780fb334f8..58d31802a4a 100644 --- a/tests/bluetooth/shell/prj.conf +++ b/tests/bluetooth/shell/prj.conf @@ -52,3 +52,6 @@ CONFIG_BT_ISO_BROADCASTER=y CONFIG_BT_ISO_SYNC_RECEIVER=y CONFIG_BT_ISO_CENTRAL=y CONFIG_BT_ISO_PERIPHERAL=y + +# See commit message for usage +CONFIG_LOG_RUNTIME_FILTERING=y