1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-19 00:05:43 +00:00

Merge branch 'integration' into memory_api

This commit is contained in:
J. Nick Koston
2025-11-15 18:15:26 -06:00
2 changed files with 19 additions and 14 deletions

View File

@@ -65,6 +65,8 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
uint16_t buffer_at = 0; // Initialize buffer position
this->format_log_to_buffer_with_terminator_(level, tag, line, format, args, console_buffer, &buffer_at,
MAX_CONSOLE_LOG_MSG_SIZE);
// Add newline if platform needs it (ESP32 doesn't add via write_msg_)
this->add_newline_to_buffer_if_needed_(console_buffer, &buffer_at, MAX_CONSOLE_LOG_MSG_SIZE);
this->write_msg_(console_buffer, buffer_at);
}
@@ -212,10 +214,7 @@ void Logger::process_messages_() {
// This ensures all log messages appear on the console in a clean, serialized manner
// Note: Messages may appear slightly out of order due to async processing, but
// this is preferred over corrupted/interleaved console output
if (this->baud_rate_ > 0) {
this->add_newline_to_buffer_if_needed_();
this->write_msg_(this->tx_buffer_, this->tx_buffer_at_);
}
this->write_tx_buffer_to_console_();
}
} else {
// No messages to process, disable loop if appropriate

View File

@@ -212,23 +212,32 @@ class Logger : public Component {
}
// Helper to add newline to buffer for platforms that need it
inline void HOT add_newline_to_buffer_if_needed_() {
// Modifies buffer_at to include the newline
inline void HOT add_newline_to_buffer_if_needed_(char *buffer, uint16_t *buffer_at, uint16_t buffer_size) {
if constexpr (!WRITE_MSG_ADDS_NEWLINE) {
// Add newline - don't need to maintain null termination
// write_msg_ now always receives explicit length, so we can safely overwrite the null terminator
// This is safe because:
// 1. Callbacks already received the message (before we add newline)
// 2. write_msg_ receives the length explicitly (doesn't need null terminator)
if (this->tx_buffer_at_ < this->tx_buffer_size_) {
this->tx_buffer_[this->tx_buffer_at_++] = '\n';
} else if (this->tx_buffer_size_ > 0) {
if (*buffer_at < buffer_size) {
buffer[(*buffer_at)++] = '\n';
} else if (buffer_size > 0) {
// Buffer was full - replace last char with newline to ensure it's visible
this->tx_buffer_[this->tx_buffer_size_ - 1] = '\n';
this->tx_buffer_at_ = this->tx_buffer_size_;
buffer[buffer_size - 1] = '\n';
*buffer_at = buffer_size;
}
}
}
// Helper to write tx_buffer_ to console if logging is enabled
inline void HOT write_tx_buffer_to_console_() {
if (this->baud_rate_ > 0) {
this->add_newline_to_buffer_if_needed_(this->tx_buffer_, &this->tx_buffer_at_, this->tx_buffer_size_);
this->write_msg_(this->tx_buffer_, this->tx_buffer_at_);
}
}
// Helper to format and send a log message to both console and callbacks
inline void HOT log_message_to_buffer_and_send_(uint8_t level, const char *tag, int line, const char *format,
va_list args) {
@@ -241,10 +250,7 @@ class Logger : public Component {
this->log_callback_.call(level, tag, this->tx_buffer_, this->tx_buffer_at_);
// Console gets message WITH newline (if platform needs it)
if (this->baud_rate_ > 0) {
this->add_newline_to_buffer_if_needed_();
this->write_msg_(this->tx_buffer_, this->tx_buffer_at_);
}
this->write_tx_buffer_to_console_();
}
// Write the body of the log message to the buffer