mirror of
https://github.com/esphome/esphome.git
synced 2025-07-30 23:26:36 +00:00
Avoid strlen for logger on all platforms that support it
This commit is contained in:
parent
cf73f72119
commit
abac07d676
@ -65,7 +65,7 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
|
|||||||
uint16_t buffer_at = 0; // Initialize buffer position
|
uint16_t buffer_at = 0; // Initialize buffer position
|
||||||
this->format_log_to_buffer_with_terminator_(level, tag, line, format, args, console_buffer, &buffer_at,
|
this->format_log_to_buffer_with_terminator_(level, tag, line, format, args, console_buffer, &buffer_at,
|
||||||
MAX_CONSOLE_LOG_MSG_SIZE);
|
MAX_CONSOLE_LOG_MSG_SIZE);
|
||||||
this->write_msg_(console_buffer);
|
this->write_msg_(console_buffer, buffer_at);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the recursion guard for this task
|
// Reset the recursion guard for this task
|
||||||
@ -136,11 +136,11 @@ void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __Flas
|
|||||||
&this->tx_buffer_at_, this->tx_buffer_size_);
|
&this->tx_buffer_at_, this->tx_buffer_size_);
|
||||||
|
|
||||||
// Write to console and send callback starting at the msg_start
|
// Write to console and send callback starting at the msg_start
|
||||||
if (this->baud_rate_ > 0) {
|
|
||||||
this->write_msg_(this->tx_buffer_ + msg_start);
|
|
||||||
}
|
|
||||||
size_t msg_length =
|
size_t msg_length =
|
||||||
this->tx_buffer_at_ - msg_start; // Don't subtract 1 - tx_buffer_at_ is already at the null terminator position
|
this->tx_buffer_at_ - msg_start; // Don't subtract 1 - tx_buffer_at_ is already at the null terminator position
|
||||||
|
if (this->baud_rate_ > 0) {
|
||||||
|
this->write_msg_(this->tx_buffer_ + msg_start, msg_length);
|
||||||
|
}
|
||||||
this->log_callback_.call(level, tag, this->tx_buffer_ + msg_start, msg_length);
|
this->log_callback_.call(level, tag, this->tx_buffer_ + msg_start, msg_length);
|
||||||
|
|
||||||
global_recursion_guard_ = false;
|
global_recursion_guard_ = false;
|
||||||
@ -224,7 +224,7 @@ void Logger::process_messages_() {
|
|||||||
// Note: Messages may appear slightly out of order due to async processing, but
|
// Note: Messages may appear slightly out of order due to async processing, but
|
||||||
// this is preferred over corrupted/interleaved console output
|
// this is preferred over corrupted/interleaved console output
|
||||||
if (this->baud_rate_ > 0) {
|
if (this->baud_rate_ > 0) {
|
||||||
this->write_msg_(this->tx_buffer_);
|
this->write_msg_(this->tx_buffer_, msg_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -161,7 +161,7 @@ class Logger : public Component {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void process_messages_();
|
void process_messages_();
|
||||||
void write_msg_(const char *msg);
|
void write_msg_(const char *msg, size_t len);
|
||||||
|
|
||||||
// Format a log message with printf-style arguments and write it to a buffer with header, footer, and null terminator
|
// Format a log message with printf-style arguments and write it to a buffer with header, footer, and null terminator
|
||||||
// It's the caller's responsibility to initialize buffer_at (typically to 0)
|
// It's the caller's responsibility to initialize buffer_at (typically to 0)
|
||||||
@ -194,7 +194,7 @@ class Logger : public Component {
|
|||||||
this->tx_buffer_size_);
|
this->tx_buffer_size_);
|
||||||
|
|
||||||
if (this->baud_rate_ > 0) {
|
if (this->baud_rate_ > 0) {
|
||||||
this->write_msg_(this->tx_buffer_); // If logging is enabled, write to console
|
this->write_msg_(this->tx_buffer_, this->tx_buffer_at_); // If logging is enabled, write to console
|
||||||
}
|
}
|
||||||
this->log_callback_.call(level, tag, this->tx_buffer_, this->tx_buffer_at_);
|
this->log_callback_.call(level, tag, this->tx_buffer_, this->tx_buffer_at_);
|
||||||
}
|
}
|
||||||
|
@ -166,7 +166,7 @@ void Logger::pre_setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_ESP_IDF
|
#ifdef USE_ESP_IDF
|
||||||
void HOT Logger::write_msg_(const char *msg) {
|
void HOT Logger::write_msg_(const char *msg, size_t len) {
|
||||||
if (
|
if (
|
||||||
#if defined(USE_LOGGER_USB_CDC) && !defined(USE_LOGGER_USB_SERIAL_JTAG)
|
#if defined(USE_LOGGER_USB_CDC) && !defined(USE_LOGGER_USB_SERIAL_JTAG)
|
||||||
this->uart_ == UART_SELECTION_USB_CDC
|
this->uart_ == UART_SELECTION_USB_CDC
|
||||||
@ -178,16 +178,26 @@ void HOT Logger::write_msg_(const char *msg) {
|
|||||||
/* DISABLES CODE */ (false) // NOLINT
|
/* DISABLES CODE */ (false) // NOLINT
|
||||||
#endif
|
#endif
|
||||||
) {
|
) {
|
||||||
|
// For USB CDC/Serial JTAG, we use puts() which adds '\n' automatically.
|
||||||
|
// This is safe because the buffer is always null-terminated by format_log_to_buffer_with_terminator_.
|
||||||
|
// The VFS layer handles newline conversion (adding '\r' if configured) in its write function.
|
||||||
|
// While puts() likely calculates strlen internally, the VFS write function already processes
|
||||||
|
// character-by-character for newline conversion, so using puts() is efficient here.
|
||||||
puts(msg);
|
puts(msg);
|
||||||
} else {
|
} else {
|
||||||
// Use tx_buffer_at_ if msg points to tx_buffer_, otherwise fall back to strlen
|
|
||||||
size_t len = (msg == this->tx_buffer_) ? this->tx_buffer_at_ : strlen(msg);
|
|
||||||
uart_write_bytes(this->uart_num_, msg, len);
|
uart_write_bytes(this->uart_num_, msg, len);
|
||||||
|
// ESP-IDF uses only '\n' for historical reasons, while Arduino platforms use '\r\n'
|
||||||
uart_write_bytes(this->uart_num_, "\n", 1);
|
uart_write_bytes(this->uart_num_, "\n", 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
void HOT Logger::write_msg_(const char *msg) { this->hw_serial_->println(msg); }
|
void HOT Logger::write_msg_(const char *msg, size_t len) {
|
||||||
|
// Arduino's println() writes the message followed by "\r\n" (CRLF).
|
||||||
|
// Previously, println() would call write(msg) which uses strlen() internally.
|
||||||
|
// By using write(buffer, size) directly, we avoid the strlen() call.
|
||||||
|
this->hw_serial_->write(reinterpret_cast<const uint8_t *>(msg), len);
|
||||||
|
this->hw_serial_->write(reinterpret_cast<const uint8_t *>("\r\n"), 2);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const char *const UART_SELECTIONS[] = {
|
const char *const UART_SELECTIONS[] = {
|
||||||
|
@ -33,7 +33,13 @@ void Logger::pre_setup() {
|
|||||||
ESP_LOGI(TAG, "Log initialized");
|
ESP_LOGI(TAG, "Log initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
void HOT Logger::write_msg_(const char *msg) { this->hw_serial_->println(msg); }
|
void HOT Logger::write_msg_(const char *msg, size_t len) {
|
||||||
|
// Arduino's println() writes the message followed by "\r\n" (CRLF).
|
||||||
|
// Previously, println() would call write(msg) which uses strlen() internally.
|
||||||
|
// By using write(buffer, size) directly, we avoid the strlen() call.
|
||||||
|
this->hw_serial_->write(reinterpret_cast<const uint8_t *>(msg), len);
|
||||||
|
this->hw_serial_->write(reinterpret_cast<const uint8_t *>("\r\n"), 2);
|
||||||
|
}
|
||||||
|
|
||||||
const char *const UART_SELECTIONS[] = {"UART0", "UART1", "UART0_SWAP"};
|
const char *const UART_SELECTIONS[] = {"UART0", "UART1", "UART0_SWAP"};
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
namespace esphome::logger {
|
namespace esphome::logger {
|
||||||
|
|
||||||
void HOT Logger::write_msg_(const char *msg) {
|
void HOT Logger::write_msg_(const char *msg, size_t len) {
|
||||||
time_t rawtime;
|
time_t rawtime;
|
||||||
struct tm *timeinfo;
|
struct tm *timeinfo;
|
||||||
char buffer[80];
|
char buffer[80];
|
||||||
|
@ -49,7 +49,13 @@ void Logger::pre_setup() {
|
|||||||
ESP_LOGI(TAG, "Log initialized");
|
ESP_LOGI(TAG, "Log initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
void HOT Logger::write_msg_(const char *msg) { this->hw_serial_->println(msg); }
|
void HOT Logger::write_msg_(const char *msg, size_t len) {
|
||||||
|
// Arduino's println() writes the message followed by "\r\n" (CRLF).
|
||||||
|
// Previously, println() would call write(msg) which uses strlen() internally.
|
||||||
|
// By using write(buffer, size) directly, we avoid the strlen() call.
|
||||||
|
this->hw_serial_->write(reinterpret_cast<const uint8_t *>(msg), len);
|
||||||
|
this->hw_serial_->write(reinterpret_cast<const uint8_t *>("\r\n"), 2);
|
||||||
|
}
|
||||||
|
|
||||||
const char *const UART_SELECTIONS[] = {"DEFAULT", "UART0", "UART1", "UART2"};
|
const char *const UART_SELECTIONS[] = {"DEFAULT", "UART0", "UART1", "UART2"};
|
||||||
|
|
||||||
|
@ -27,7 +27,13 @@ void Logger::pre_setup() {
|
|||||||
ESP_LOGI(TAG, "Log initialized");
|
ESP_LOGI(TAG, "Log initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
void HOT Logger::write_msg_(const char *msg) { this->hw_serial_->println(msg); }
|
void HOT Logger::write_msg_(const char *msg, size_t len) {
|
||||||
|
// Arduino's println() writes the message followed by "\r\n" (CRLF).
|
||||||
|
// Previously, println() would call write(msg) which uses strlen() internally.
|
||||||
|
// By using write(buffer, size) directly, we avoid the strlen() call.
|
||||||
|
this->hw_serial_->write(reinterpret_cast<const uint8_t *>(msg), len);
|
||||||
|
this->hw_serial_->write(reinterpret_cast<const uint8_t *>("\r\n"), 2);
|
||||||
|
}
|
||||||
|
|
||||||
const char *const UART_SELECTIONS[] = {"UART0", "UART1", "USB_CDC"};
|
const char *const UART_SELECTIONS[] = {"UART0", "UART1", "USB_CDC"};
|
||||||
|
|
||||||
|
@ -63,16 +63,15 @@ void Logger::pre_setup() {
|
|||||||
ESP_LOGI(TAG, "Log initialized");
|
ESP_LOGI(TAG, "Log initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
void HOT Logger::write_msg_(const char *msg) {
|
void HOT Logger::write_msg_(const char *msg, size_t len) {
|
||||||
#ifdef CONFIG_PRINTK
|
#ifdef CONFIG_PRINTK
|
||||||
printk("%s\n", msg);
|
printk("%s\n", msg);
|
||||||
#endif
|
#endif
|
||||||
if (nullptr == this->uart_dev_) {
|
if (nullptr == this->uart_dev_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
while (*msg) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
uart_poll_out(this->uart_dev_, *msg);
|
uart_poll_out(this->uart_dev_, msg[i]);
|
||||||
++msg;
|
|
||||||
}
|
}
|
||||||
uart_poll_out(this->uart_dev_, '\n');
|
uart_poll_out(this->uart_dev_, '\n');
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user