From 2e1d6e4d580d889f3e2fa9a0d9b1d0c149c90a24 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 29 Mar 2026 13:52:29 -1000 Subject: [PATCH] [api] Move HELPER_LOG_PACKETS logging to call sites before fast path LOG_PACKET_SENDING was in write_raw_iov_ which is only called on the slow path. Moved to write_protobuf_packet and write_protobuf_messages so all packets are logged regardless of which write path is taken. --- esphome/components/api/api_frame_helper.cpp | 6 ------ esphome/components/api/api_frame_helper_noise.cpp | 6 ++++++ esphome/components/api/api_frame_helper_plaintext.cpp | 6 ++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/esphome/components/api/api_frame_helper.cpp b/esphome/components/api/api_frame_helper.cpp index 89ae9b3975f..a8d0ec9d71f 100644 --- a/esphome/components/api/api_frame_helper.cpp +++ b/esphome/components/api/api_frame_helper.cpp @@ -123,12 +123,6 @@ APIError APIFrameHelper::write_raw_buf_(const void *data, uint16_t len, ssize_t // or directly from cold paths (handshake, error handling). // sent == -1 means either the fast path write returned -1, or there was overflow backlog. APIError APIFrameHelper::write_raw_iov_(const struct iovec *iov, int iovcnt, uint16_t total_write_len, ssize_t sent) { -#ifdef HELPER_LOG_PACKETS - for (int i = 0; i < iovcnt; i++) { - LOG_PACKET_SENDING(reinterpret_cast(iov[i].iov_base), iov[i].iov_len); - } -#endif - if (sent == -1) { // Either the fast path write returned -1, or we were called directly (cold path) if (!this->overflow_buf_.empty()) { diff --git a/esphome/components/api/api_frame_helper_noise.cpp b/esphome/components/api/api_frame_helper_noise.cpp index 156fcec314f..e5ed1d86578 100644 --- a/esphome/components/api/api_frame_helper_noise.cpp +++ b/esphome/components/api/api_frame_helper_noise.cpp @@ -505,6 +505,7 @@ APIError APINoiseFrameHelper::write_protobuf_packet(uint8_t type, ProtoWriteBuff if (aerr != APIError::OK) return aerr; // buf_start and iov.iov_base point to the same location + LOG_PACKET_SENDING(buf_start, iov.iov_len); return this->write_raw_fast_buf_(buf_start, static_cast(iov.iov_len)); } @@ -528,6 +529,11 @@ APIError APINoiseFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffer, s total_write_len += iov.iov_len; } +#ifdef HELPER_LOG_PACKETS + for (const auto &iov : iovs) { + LOG_PACKET_SENDING(reinterpret_cast(iov.iov_base), iov.iov_len); + } +#endif return this->write_raw_fast_iov_(iovs.data(), iovs.size(), total_write_len); } diff --git a/esphome/components/api/api_frame_helper_plaintext.cpp b/esphome/components/api/api_frame_helper_plaintext.cpp index 6be20f89f37..ee0661c998c 100644 --- a/esphome/components/api/api_frame_helper_plaintext.cpp +++ b/esphome/components/api/api_frame_helper_plaintext.cpp @@ -292,6 +292,7 @@ APIError APIPlaintextFrameHelper::write_protobuf_packet(uint8_t type, ProtoWrite uint8_t *msg_start = write_plaintext_header(buffer_data, msg, frame_header_padding_); uint8_t msg_header_len = static_cast(buffer_data + frame_header_padding_ - msg_start); uint16_t msg_len = static_cast(msg_header_len + msg.payload_size); + LOG_PACKET_SENDING(msg_start, msg_len); return this->write_raw_fast_buf_(msg_start, msg_len); } @@ -315,6 +316,11 @@ APIError APIPlaintextFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffe total_write_len += msg_len; } +#ifdef HELPER_LOG_PACKETS + for (const auto &iov : iovs) { + LOG_PACKET_SENDING(reinterpret_cast(iov.iov_base), iov.iov_len); + } +#endif return this->write_raw_fast_iov_(iovs.data(), iovs.size(), total_write_len); }