From dba502c75611a0f43fcd6a778f239e93c1ff3625 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Tue, 7 Sep 2021 22:57:20 +0200 Subject: [PATCH] Logger prevent recursive logging (#2251) --- esphome/components/logger/logger.cpp | 8 ++++++-- esphome/components/logger/logger.h | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index ce82a51b94..9d79037087 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -43,21 +43,24 @@ void Logger::write_header_(int level, const char *tag, int line) { } void HOT Logger::log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) { // NOLINT - if (level > this->level_for(tag)) + if (level > this->level_for(tag) || recursion_guard_) return; + recursion_guard_ = true; this->reset_buffer_(); this->write_header_(level, tag, line); this->vprintf_to_buffer_(format, args); this->write_footer_(); this->log_message_(level, tag); + recursion_guard_ = false; } #ifdef USE_STORE_LOG_STR_IN_FLASH void Logger::log_vprintf_(int level, const char *tag, int line, const __FlashStringHelper *format, va_list args) { // NOLINT - if (level > this->level_for(tag)) + if (level > this->level_for(tag) || recursion_guard_) return; + recursion_guard_ = true; this->reset_buffer_(); // copy format string const char *format_pgm_p = (PGM_P) format; @@ -78,6 +81,7 @@ void Logger::log_vprintf_(int level, const char *tag, int line, const __FlashStr this->vprintf_to_buffer_(this->tx_buffer_, args); this->write_footer_(); this->log_message_(level, tag, offset); + recursion_guard_ = false; } #endif diff --git a/esphome/components/logger/logger.h b/esphome/components/logger/logger.h index 1724875229..365261cb91 100644 --- a/esphome/components/logger/logger.h +++ b/esphome/components/logger/logger.h @@ -113,6 +113,8 @@ class Logger : public Component { }; std::vector log_levels_; CallbackManager log_callback_{}; + /// Prevents recursive log calls, if true a log message is already being processed. + bool recursion_guard_ = false; }; extern Logger *global_logger; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)