From fc85f8a47d4821575c13a1d5df4638f9cea934d2 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Thu, 18 Feb 2021 22:43:17 +0100 Subject: [PATCH 1/2] esp_log: Implemented esp_log_level_get(TAG) Closes https://github.com/espressif/esp-idf/pull/6573 --- components/log/include/esp_log.h | 10 ++++++++++ components/log/log.c | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/components/log/include/esp_log.h b/components/log/include/esp_log.h index 02f7d167fe..d46e8068b1 100644 --- a/components/log/include/esp_log.h +++ b/components/log/include/esp_log.h @@ -71,6 +71,16 @@ extern esp_log_level_t esp_log_default_level; */ void esp_log_level_set(const char* tag, esp_log_level_t level); +/** + * @brief Get log level for given tag, can be used to avoid expensive log statements + * + * @param tag Tag of the log to query current level. Must be a non-NULL zero terminated + * string. + * + * @return The current log level for the given tag + */ +esp_log_level_t esp_log_level_get(const char* tag); + /** * @brief Set function used to output log entries * diff --git a/components/log/log.c b/components/log/log.c index 9af956516c..84b36638da 100644 --- a/components/log/log.c +++ b/components/log/log.c @@ -141,6 +141,25 @@ void esp_log_level_set(const char *tag, esp_log_level_t level) esp_log_impl_unlock(); } +esp_log_level_t esp_log_level_get(const char* tag) +{ + esp_log_impl_lock(); + esp_log_level_t level_for_tag; + // Look for the tag in cache first, then in the linked list of all tags + if (!get_cached_log_level(tag, &level_for_tag)) { + if (!get_uncached_log_level(tag, &level_for_tag)) { + level_for_tag = esp_log_default_level; + } + add_to_cache(tag, level_for_tag); +#ifdef LOG_BUILTIN_CHECKS + ++s_log_cache_misses; +#endif + } + esp_log_impl_unlock(); + + return level_for_tag; +} + void clear_log_level_list(void) { uncached_tag_entry_t *it; From 6c44fe291cc054620fef80956958965ca6055202 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Mon, 7 Jun 2021 17:11:16 +1000 Subject: [PATCH 2/2] esp_log: Refactor esp_log_level_get() into a common static function Removes duplicate code for a small runtime & size overhead if esp_log_level_get() is not called in the firmware. Follow-up to https://github.com/espressif/esp-idf/pull/6573 --- components/log/log.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/components/log/log.c b/components/log/log.c index 84b36638da..4ac47c5b14 100644 --- a/components/log/log.c +++ b/components/log/log.c @@ -141,9 +141,13 @@ void esp_log_level_set(const char *tag, esp_log_level_t level) esp_log_impl_unlock(); } -esp_log_level_t esp_log_level_get(const char* tag) + +/* Common code for getting the log level from cache, esp_log_impl_lock() + should be called before calling this function. The function unlocks, + as indicated in the name. +*/ +static esp_log_level_t s_log_level_get_and_unlock(const char *tag) { - esp_log_impl_lock(); esp_log_level_t level_for_tag; // Look for the tag in cache first, then in the linked list of all tags if (!get_cached_log_level(tag, &level_for_tag)) { @@ -160,6 +164,12 @@ esp_log_level_t esp_log_level_get(const char* tag) return level_for_tag; } +esp_log_level_t esp_log_level_get(const char *tag) +{ + esp_log_impl_lock(); + return s_log_level_get_and_unlock(tag); +} + void clear_log_level_list(void) { uncached_tag_entry_t *it; @@ -182,18 +192,7 @@ void esp_log_writev(esp_log_level_t level, if (!esp_log_impl_lock_timeout()) { return; } - esp_log_level_t level_for_tag; - // Look for the tag in cache first, then in the linked list of all tags - if (!get_cached_log_level(tag, &level_for_tag)) { - if (!get_uncached_log_level(tag, &level_for_tag)) { - level_for_tag = esp_log_default_level; - } - add_to_cache(tag, level_for_tag); -#ifdef LOG_BUILTIN_CHECKS - ++s_log_cache_misses; -#endif - } - esp_log_impl_unlock(); + esp_log_level_t level_for_tag = s_log_level_get_and_unlock(tag); if (!should_output(level, level_for_tag)) { return; }