Logger prevent recursive logging (#2251)

This commit is contained in:
Otto Winter 2021-09-07 22:57:20 +02:00 committed by GitHub
parent d9cb64b893
commit dba502c756
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View file

@ -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 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; return;
recursion_guard_ = true;
this->reset_buffer_(); this->reset_buffer_();
this->write_header_(level, tag, line); this->write_header_(level, tag, line);
this->vprintf_to_buffer_(format, args); this->vprintf_to_buffer_(format, args);
this->write_footer_(); this->write_footer_();
this->log_message_(level, tag); this->log_message_(level, tag);
recursion_guard_ = false;
} }
#ifdef USE_STORE_LOG_STR_IN_FLASH #ifdef USE_STORE_LOG_STR_IN_FLASH
void Logger::log_vprintf_(int level, const char *tag, int line, const __FlashStringHelper *format, void Logger::log_vprintf_(int level, const char *tag, int line, const __FlashStringHelper *format,
va_list args) { // NOLINT va_list args) { // NOLINT
if (level > this->level_for(tag)) if (level > this->level_for(tag) || recursion_guard_)
return; return;
recursion_guard_ = true;
this->reset_buffer_(); this->reset_buffer_();
// copy format string // copy format string
const char *format_pgm_p = (PGM_P) format; 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->vprintf_to_buffer_(this->tx_buffer_, args);
this->write_footer_(); this->write_footer_();
this->log_message_(level, tag, offset); this->log_message_(level, tag, offset);
recursion_guard_ = false;
} }
#endif #endif

View file

@ -113,6 +113,8 @@ class Logger : public Component {
}; };
std::vector<LogLevelOverride> log_levels_; std::vector<LogLevelOverride> log_levels_;
CallbackManager<void(int, const char *, const char *)> log_callback_{}; CallbackManager<void(int, const char *, const char *)> 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) extern Logger *global_logger; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)