mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 16:38:16 +01:00
Logger prevent recursive logging (#2251)
This commit is contained in:
parent
d9cb64b893
commit
dba502c756
2 changed files with 8 additions and 2 deletions
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue