From 70debb1c98fb5a8ff2cbf868229314fde6d250f2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 4 Feb 2026 06:04:27 +0100 Subject: [PATCH] wip --- esphome/components/logger/logger.cpp | 10 ++-------- esphome/components/logger/logger.h | 10 ++++++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index f79508b0cc..2e86c00b7e 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -36,9 +36,7 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch // Fast path: main thread, no recursion (99.9% of all logs) if (is_main_task && !this->main_task_recursion_guard_) [[likely]] { - RecursionGuard guard(this->main_task_recursion_guard_); - // Format and send to both console and callbacks - this->log_message_to_buffer_and_send_(level, tag, line, format, args); + this->log_message_to_buffer_and_send_(this->main_task_recursion_guard_, level, tag, line, format, args); return; } @@ -117,11 +115,7 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch if (level > this->level_for(tag) || global_recursion_guard_) return; - RecursionGuard guard(global_recursion_guard_); - LogBuffer buf(this->tx_buffer_, this->tx_buffer_at_, this->tx_buffer_size_); - this->format_log_to_buffer_with_terminator_(level, tag, line, format, args, buf); - this->notify_listeners_(level, tag); - this->write_log_buffer_to_console_(buf); + this->log_message_to_buffer_and_send_(global_recursion_guard_, level, tag, line, format, args); } #endif // USE_ESP32 / USE_HOST / USE_LIBRETINY diff --git a/esphome/components/logger/logger.h b/esphome/components/logger/logger.h index 623e8ed1b6..28bfa2e8df 100644 --- a/esphome/components/logger/logger.h +++ b/esphome/components/logger/logger.h @@ -443,6 +443,16 @@ class Logger : public Component { } } + // Helper to format and send a log message to both console and listeners + inline void HOT log_message_to_buffer_and_send_(bool &recursion_guard, uint8_t level, const char *tag, int line, + const char *format, va_list args) { + RecursionGuard guard(recursion_guard); + LogBuffer buf(this->tx_buffer_, this->tx_buffer_at_, this->tx_buffer_size_); + this->format_log_to_buffer_with_terminator_(level, tag, line, format, args, buf); + this->notify_listeners_(level, tag); + this->write_log_buffer_to_console_(buf); + } + #ifdef USE_ESPHOME_TASK_LOG_BUFFER // Helper to format a pre-formatted message from the task log buffer and notify listeners // Used by process_messages_ to avoid code duplication between ESP32 and host platforms