From ef9e89062a54295e9eee65d309f4140dee43207a Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 21 Jun 2022 17:14:08 +0200 Subject: [PATCH] console: add log_level command to the example This allows calling esp_log_level_set function from the console. Together with CONFIG_LOG_MAXIMUM_LEVEL option, this allows enabling logs from different tags at run time. --- .../components/cmd_system/cmd_system.c | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/examples/system/console/advanced/components/cmd_system/cmd_system.c b/examples/system/console/advanced/components/cmd_system/cmd_system.c index f493f176d5..5c17066240 100644 --- a/examples/system/console/advanced/components/cmd_system/cmd_system.c +++ b/examples/system/console/advanced/components/cmd_system/cmd_system.c @@ -39,6 +39,7 @@ static void register_light_sleep(void); #if WITH_TASKS_INFO static void register_tasks(void); #endif +static void register_log_level(void); void register_system_common(void) { @@ -49,6 +50,7 @@ void register_system_common(void) #if WITH_TASKS_INFO register_tasks(); #endif + register_log_level(); } void register_system_sleep(void) @@ -397,3 +399,68 @@ static void register_light_sleep(void) }; ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); } + +/** log_level command changes log level via esp_log_level_set */ + +static struct { + struct arg_str *tag; + struct arg_str *level; + struct arg_end *end; +} log_level_args; + +static const char* s_log_level_names[] = { + "none", + "error", + "warn", + "info", + "debug", + "verbose" +}; + +static int log_level(int argc, char **argv) +{ + int nerrors = arg_parse(argc, argv, (void **) &log_level_args); + if (nerrors != 0) { + arg_print_errors(stderr, log_level_args.end, argv[0]); + return 1; + } + assert(log_level_args.tag->count == 1); + assert(log_level_args.level->count == 1); + const char* tag = log_level_args.tag->sval[0]; + const char* level_str = log_level_args.level->sval[0]; + esp_log_level_t level; + size_t level_len = strlen(level_str); + for (level = ESP_LOG_NONE; level <= ESP_LOG_VERBOSE; level++) { + if (memcmp(level_str, s_log_level_names[level], level_len) == 0) { + break; + } + } + if (level > ESP_LOG_VERBOSE) { + printf("Invalid log level '%s', choose from none|error|warn|info|debug|verbose\n", level_str); + return 1; + } + if (level > CONFIG_LOG_MAXIMUM_LEVEL) { + printf("Can't set log level to %s, max level limited in menuconfig to %s. " + "Please increase CONFIG_LOG_MAXIMUM_LEVEL in menuconfig.\n", + s_log_level_names[level], s_log_level_names[CONFIG_LOG_MAXIMUM_LEVEL]); + return 1; + } + esp_log_level_set(tag, level); + return 0; +} + +static void register_log_level(void) +{ + log_level_args.tag = arg_str1(NULL, NULL, "", "Log tag to set the level for, or * to set for all tags"); + log_level_args.level = arg_str1(NULL, NULL, "", "Log level to set. Abbreviated words are accepted."); + log_level_args.end = arg_end(2); + + const esp_console_cmd_t cmd = { + .command = "log_level", + .help = "Set log level for all tags or a specific tag.", + .hint = NULL, + .func = &log_level, + .argtable = &log_level_args + }; + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); +}