From 26ed6aa3147407076a3ecf3c61389ce29b332162 Mon Sep 17 00:00:00 2001 From: Tomasz Duda Date: Tue, 10 Feb 2026 23:34:40 +0100 Subject: [PATCH] fix --- esphome/components/logger/logger.cpp | 5 ++++- esphome/components/logger/logger.h | 6 ++++-- esphome/components/logger/task_log_buffer_zephyr.cpp | 11 +++-------- esphome/components/logger/task_log_buffer_zephyr.h | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index 0251e57ffd..9fc33145c6 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -55,8 +55,11 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch // ESP32/LibreTiny: use TaskHandle_t overload to avoid redundant xTaskGetCurrentTaskHandle() // (we already have the handle from the main task check above). // Host: pass a stack buffer for pthread_getname_np to write into. -#if defined(USE_ESP32) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR) +#if defined(USE_ESP32) || defined(USE_LIBRETINY) const char *thread_name = get_thread_name_(current_task); +#elif defined(USE_ZEPHYR) + char thread_name_buf[MAX_POINTER_REPRESENTATION]; + const char *thread_name = get_thread_name_(thread_name_buf, current_task); #else // USE_HOST char thread_name_buf[THREAD_NAME_BUF_SIZE]; const char *thread_name = this->get_thread_name_(thread_name_buf); diff --git a/esphome/components/logger/logger.h b/esphome/components/logger/logger.h index 3d15b9d3d5..956c94d2df 100644 --- a/esphome/components/logger/logger.h +++ b/esphome/components/logger/logger.h @@ -591,8 +591,10 @@ class Logger : public Component { } #elif defined(USE_ZEPHYR) - const char *HOT get_thread_name_(std::span buff) { - k_tid_t current_task = k_current_get(); + const char *HOT get_thread_name_(std::span buff, k_tid_t current_task = nullptr) { + if (current_task == nullptr) { + current_task = k_current_get(); + } if (current_task == main_task_) { return nullptr; // Main task } diff --git a/esphome/components/logger/task_log_buffer_zephyr.cpp b/esphome/components/logger/task_log_buffer_zephyr.cpp index 87ecd9cddd..1c0865b0c8 100644 --- a/esphome/components/logger/task_log_buffer_zephyr.cpp +++ b/esphome/components/logger/task_log_buffer_zephyr.cpp @@ -27,8 +27,8 @@ TaskLogBufferZephyr::TaskLogBufferZephyr(size_t total_buffer_size) { TaskLogBufferZephyr::~TaskLogBufferZephyr() { delete[] this->mpsc_config_.buf; } -bool TaskLogBufferZephyr::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, void *task_handle, - const char *format, va_list args) { +bool TaskLogBufferZephyr::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, + const char *thread_name, const char *format, va_list args) { // First, calculate the exact length needed using a null buffer (no actual writing) va_list args_copy; va_copy(args_copy, args); @@ -51,12 +51,7 @@ bool TaskLogBufferZephyr::send_message_thread_safe(uint8_t level, const char *ta msg->level = level; msg->tag = tag; msg->line = line; - const char *thread_name = k_thread_name_get(static_cast(task_handle)); - if (thread_name) { - strncpy(msg->thread_name, thread_name, sizeof(msg->thread_name) - 1); - } else { - std::snprintf(msg->thread_name, sizeof(msg->thread_name), "%p", task_handle); - } + strncpy(msg->thread_name, thread_name, sizeof(msg->thread_name) - 1); // Format the message text directly into the acquired memory // We add 1 to text_length to ensure space for null terminator during formatting diff --git a/esphome/components/logger/task_log_buffer_zephyr.h b/esphome/components/logger/task_log_buffer_zephyr.h index 4a7d6ff781..2f85f8b652 100644 --- a/esphome/components/logger/task_log_buffer_zephyr.h +++ b/esphome/components/logger/task_log_buffer_zephyr.h @@ -52,8 +52,8 @@ class TaskLogBufferZephyr { 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, void *task_handle, const char *format, - va_list args); + bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name, + const char *format, va_list args); protected: mpsc_pbuf_buffer_config mpsc_config_{};