From f852fb4300058ad1a70e67d4f8aafbc4284e3a68 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 Jan 2026 12:14:49 -1000 Subject: [PATCH] tweak --- esphome/components/logger/logger.cpp | 21 ++++++++++++++++++--- esphome/components/logger/logger.h | 10 +++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index 89e8edcf44..d7ed39c8e8 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -29,7 +29,13 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch if (level > this->level_for(tag)) return; - const bool is_main_task = this->is_current_main_task_(); +#if defined(USE_ESP32) || defined(USE_LIBRETINY) + // Get task handle once - used for both main task check and passing to non-main thread handler + TaskHandle_t current_task = xTaskGetCurrentTaskHandle(); + const bool is_main_task = (current_task == this->main_task_); +#else // USE_HOST + const bool is_main_task = pthread_equal(pthread_self(), this->main_thread_); +#endif // Fast path: main thread, no recursion (99.9% of all logs) if (is_main_task && !this->main_task_recursion_guard_) [[likely]] { @@ -45,12 +51,21 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch } // Non-main thread handling (~0.1% of logs) +#if defined(USE_ESP32) || defined(USE_LIBRETINY) + this->log_vprintf_non_main_thread_(level, tag, line, format, args, current_task); +#else // USE_HOST this->log_vprintf_non_main_thread_(level, tag, line, format, args); +#endif } // Handles non-main thread logging only // Kept separate from hot path to improve instruction cache performance +#if defined(USE_ESP32) || defined(USE_LIBRETINY) +void Logger::log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args, + TaskHandle_t current_task) { +#else // USE_HOST void Logger::log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args) { +#endif // Check if already in recursion for this non-main thread/task if (this->is_non_main_task_recursive_()) { return; @@ -63,8 +78,8 @@ void Logger::log_vprintf_non_main_thread_(uint8_t level, const char *tag, int li #ifdef USE_ESPHOME_TASK_LOG_BUFFER // For non-main threads/tasks, queue the message for callbacks #if defined(USE_ESP32) || defined(USE_LIBRETINY) - message_sent = this->log_buffer_->send_message_thread_safe(level, tag, static_cast(line), - xTaskGetCurrentTaskHandle(), format, args); + message_sent = + this->log_buffer_->send_message_thread_safe(level, tag, static_cast(line), current_task, format, args); #else // USE_HOST message_sent = this->log_buffer_->send_message_thread_safe(level, tag, static_cast(line), format, args); #endif diff --git a/esphome/components/logger/logger.h b/esphome/components/logger/logger.h index 1e8ddf25d5..12b3c27db1 100644 --- a/esphome/components/logger/logger.h +++ b/esphome/components/logger/logger.h @@ -243,13 +243,13 @@ class Logger : public Component { #if defined(USE_ESP32) || defined(USE_HOST) || defined(USE_LIBRETINY) // Handles non-main thread logging only (~0.1% of calls) - void log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args); - - // Platform-specific main task/thread check - inlined for fast path performance #if defined(USE_ESP32) || defined(USE_LIBRETINY) - inline bool is_current_main_task_() const { return xTaskGetCurrentTaskHandle() == this->main_task_; } + // ESP32/LibreTiny: Pass task handle to avoid calling xTaskGetCurrentTaskHandle() twice + void log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args, + TaskHandle_t current_task); #else // USE_HOST - inline bool is_current_main_task_() const { return pthread_equal(pthread_self(), this->main_thread_); } + // Host: No task handle parameter needed (not used in send_message_thread_safe) + void log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args); #endif #endif void process_messages_();