From 1e7eda1a67cc70ada4e510c7dd15f325f641fe95 Mon Sep 17 00:00:00 2001 From: Tomasz Duda Date: Sun, 8 Feb 2026 14:21:33 +0100 Subject: [PATCH] fix --- esphome/components/logger/task_log_buffer_esp32.cpp | 4 ++-- esphome/components/logger/task_log_buffer_esp32.h | 4 ++-- .../components/logger/task_log_buffer_zephyr.cpp | 13 ++++++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/esphome/components/logger/task_log_buffer_esp32.cpp b/esphome/components/logger/task_log_buffer_esp32.cpp index 90c0161ac8a..a024e6bafbf 100644 --- a/esphome/components/logger/task_log_buffer_esp32.cpp +++ b/esphome/components/logger/task_log_buffer_esp32.cpp @@ -60,7 +60,7 @@ void TaskLogBuffer::release_message_main_loop() { last_processed_counter_ = message_counter_.load(std::memory_order_relaxed); } -bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, TaskHandle_t task_handle, +bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, void *task_handle, const char *format, va_list args) { // First, calculate the exact length needed using a null buffer (no actual writing) va_list args_copy; @@ -96,7 +96,7 @@ bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uin // Store the thread name now instead of waiting until main loop processing // This avoids crashes if the task completes or is deleted between when this message // is enqueued and when it's processed by the main loop - const char *thread_name = pcTaskGetName(task_handle); + const char *thread_name = pcTaskGetName(static_cast(task_handle)); if (thread_name != nullptr) { strncpy(msg->thread_name, thread_name, sizeof(msg->thread_name) - 1); msg->thread_name[sizeof(msg->thread_name) - 1] = '\0'; // Ensure null termination diff --git a/esphome/components/logger/task_log_buffer_esp32.h b/esphome/components/logger/task_log_buffer_esp32.h index e3103a8d345..73b5478e556 100644 --- a/esphome/components/logger/task_log_buffer_esp32.h +++ b/esphome/components/logger/task_log_buffer_esp32.h @@ -58,8 +58,8 @@ class TaskLogBuffer { void release_message_main_loop(); // Thread-safe - send a message to the ring buffer from any thread - bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, TaskHandle_t task_handle, - const char *format, va_list args); + bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, void *task_handle, const char *format, + va_list args); // Check if there are messages ready to be processed using an atomic counter for performance inline bool HOT has_messages() const { diff --git a/esphome/components/logger/task_log_buffer_zephyr.cpp b/esphome/components/logger/task_log_buffer_zephyr.cpp index b788f777d83..5c9bb496421 100644 --- a/esphome/components/logger/task_log_buffer_zephyr.cpp +++ b/esphome/components/logger/task_log_buffer_zephyr.cpp @@ -6,13 +6,19 @@ namespace esphome::logger { __thread bool non_main_task_recursion_guard_; +static inline uint32_t get_wlen(const mpsc_pbuf_generic *item) { + auto *msg = reinterpret_cast(item); + // Calculate total size in 32-bit words needed (header + text length + null terminator + 3(4 bytes alignment) + return (sizeof(TaskLogBufferZephyr::LogMessage) + msg->text_length + 1 + 3) / sizeof(uint32_t); +} + TaskLogBufferZephyr::TaskLogBufferZephyr(size_t total_buffer_size) { // alignment to 4 bytes total_buffer_size = (total_buffer_size + 3) / sizeof(uint32_t); this->mpsc_config_.buf = new uint32_t[total_buffer_size]; this->mpsc_config_.size = total_buffer_size; this->mpsc_config_.flags = MPSC_PBUF_MODE_OVERWRITE; - // .get_wlen = log_msg_generic_get_wlen, + this->mpsc_config_.get_wlen = get_wlen, mpsc_pbuf_init(&this->log_buffer_, &this->mpsc_config_); } @@ -36,7 +42,7 @@ bool TaskLogBufferZephyr::send_message_thread_safe(uint8_t level, const char *ta size_t text_length = (static_cast(ret) > MAX_TEXT_SIZE) ? MAX_TEXT_SIZE : ret; // Calculate total size in 32-bit words needed (header + text length + null terminator + 3(4 bytes alignment) size_t total_size = (sizeof(LogMessage) + text_length + 1 + 3) / sizeof(uint32_t); - auto msg = reinterpret_cast(mpsc_pbuf_alloc(&this->log_buffer_, total_size, K_NO_WAIT)); + auto *msg = reinterpret_cast(mpsc_pbuf_alloc(&this->log_buffer_, total_size, K_NO_WAIT)); if (nullptr == msg) { return false; } @@ -47,7 +53,7 @@ bool TaskLogBufferZephyr::send_message_thread_safe(uint8_t level, const char *ta if (thread_name) { strncpy(msg->thread_name, thread_name, sizeof(msg->thread_name) - 1); } else { - std::snprintf(msg->thread_name, MAX_POINTER_REPRESENTATION, "%p", task_handle); + std::snprintf(msg->thread_name, sizeof(msg->thread_name), "%p", task_handle); } // Format the message text directly into the acquired memory @@ -64,6 +70,7 @@ bool TaskLogBufferZephyr::send_message_thread_safe(uint8_t level, const char *ta text_area[i] = '\n'; } text_area[text_length] = 0; + // do not return false to free the buffer from main thread } msg->text_length = text_length;