diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index 0f879b6ca10..54b5670016e 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -99,8 +99,7 @@ void Logger::log_vprintf_non_main_thread_(uint8_t level, const char *tag, int li static const size_t MAX_CONSOLE_LOG_MSG_SIZE = 144; #endif char console_buffer[MAX_CONSOLE_LOG_MSG_SIZE]; // MUST be stack allocated for thread safety - uint16_t console_pos; - LogBuffer buf(console_buffer, console_pos, MAX_CONSOLE_LOG_MSG_SIZE); + LogBuffer buf{console_buffer, MAX_CONSOLE_LOG_MSG_SIZE}; this->format_log_to_buffer_with_terminator_(level, tag, line, format, args, buf); this->write_to_console_(buf); } @@ -188,7 +187,7 @@ void Logger::process_messages_() { logger::TaskLogBufferHost::LogMessage *message; while (this->log_buffer_->get_message_main_loop(&message)) { const char *thread_name = message->thread_name[0] != '\0' ? message->thread_name : nullptr; - LogBuffer buf(this->tx_buffer_, this->tx_buffer_at_, this->tx_buffer_size_); + LogBuffer buf{this->tx_buffer_, this->tx_buffer_size_}; this->format_buffered_message_and_notify_(message->level, message->tag, message->line, thread_name, message->text, message->text_length, buf); this->log_buffer_->release_message_main_loop(); @@ -200,7 +199,7 @@ void Logger::process_messages_() { void *received_token; while (this->log_buffer_->borrow_message_main_loop(&message, &text, &received_token)) { const char *thread_name = message->thread_name[0] != '\0' ? message->thread_name : nullptr; - LogBuffer buf(this->tx_buffer_, this->tx_buffer_at_, this->tx_buffer_size_); + LogBuffer buf{this->tx_buffer_, this->tx_buffer_size_}; this->format_buffered_message_and_notify_(message->level, message->tag, message->line, thread_name, text, message->text_length, buf); // Release the message to allow other tasks to use it as soon as possible @@ -212,7 +211,7 @@ void Logger::process_messages_() { const char *text; while (this->log_buffer_->borrow_message_main_loop(&message, &text)) { const char *thread_name = message->thread_name[0] != '\0' ? message->thread_name : nullptr; - LogBuffer buf(this->tx_buffer_, this->tx_buffer_at_, this->tx_buffer_size_); + LogBuffer buf{this->tx_buffer_, this->tx_buffer_size_}; this->format_buffered_message_and_notify_(message->level, message->tag, message->line, thread_name, text, message->text_length, buf); // Release the message to allow other tasks to use it as soon as possible diff --git a/esphome/components/logger/logger.h b/esphome/components/logger/logger.h index 62b93ddafa5..a8fa0f34a7d 100644 --- a/esphome/components/logger/logger.h +++ b/esphome/components/logger/logger.h @@ -127,10 +127,8 @@ static constexpr size_t MAX_POINTER_REPRESENTATION = 2 + sizeof(void *) * 2 + 1; // Buffer wrapper for log formatting functions struct LogBuffer { char *data; - uint16_t &pos; uint16_t size; - - LogBuffer(char *buf, uint16_t &buf_pos, uint16_t buf_size) : data(buf), pos(buf_pos = 0), size(buf_size) {} + uint16_t pos{0}; // Replaces the null terminator with a newline for console output. // Must be called after notify_listeners_() since listeners need null-terminated strings. // Console output uses length-based writes (buf.pos), so null terminator is not needed. @@ -447,10 +445,10 @@ class Logger : public Component { #endif // Helper to notify log listeners - inline void HOT notify_listeners_(uint8_t level, const char *tag) { + inline void HOT notify_listeners_(uint8_t level, const char *tag, const LogBuffer &buf) { #ifdef USE_LOG_LISTENERS for (auto *listener : this->log_listeners_) - listener->on_log(level, tag, this->tx_buffer_, this->tx_buffer_at_); + listener->on_log(level, tag, buf.data, buf.pos); #endif } @@ -472,7 +470,7 @@ class Logger : public Component { inline void HOT log_message_to_buffer_and_send_(bool &recursion_guard, uint8_t level, const char *tag, int line, FormatType format, va_list args) { RecursionGuard guard(recursion_guard); - LogBuffer buf(this->tx_buffer_, this->tx_buffer_at_, this->tx_buffer_size_); + LogBuffer buf{this->tx_buffer_, this->tx_buffer_size_}; #ifdef USE_STORE_LOG_STR_IN_FLASH if constexpr (std::is_same_v) { this->format_log_to_buffer_with_terminator_P_(level, tag, line, format, args, buf); @@ -481,7 +479,7 @@ class Logger : public Component { { this->format_log_to_buffer_with_terminator_(level, tag, line, format, args, buf); } - this->notify_listeners_(level, tag); + this->notify_listeners_(level, tag, buf); this->write_log_buffer_to_console_(buf); } @@ -493,7 +491,7 @@ class Logger : public Component { LogBuffer &buf) { buf.write_header(level, tag, line, thread_name); buf.write_body(text, text_length); - this->notify_listeners_(level, tag); + this->notify_listeners_(level, tag, buf); } #endif @@ -550,7 +548,6 @@ class Logger : public Component { #endif // Group smaller types together at the end - uint16_t tx_buffer_at_{0}; uint16_t tx_buffer_size_{0}; uint8_t current_level_{ESPHOME_LOG_LEVEL_VERY_VERBOSE}; #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_ZEPHYR)