These changes enhance ESPHome's logging system on ESP32 multi-task environments:
1. **Emergency Console Logging**:
- Added fallback console logging when the task log buffer is full or disabled
- Ensures critical messages are still visible even when the ring buffer fails
2. **Improved Console Output**:
- Messages successfully sent to the ring buffer now also display on the console
- Ensures consistent console output for all log messages regardless of source
3. **Optimized Resource Usage**:
- Release ring buffer messages earlier after transferring to tx_buffer
- Reduces contention for the shared log buffer in multi-task environments
1. **Stack Memory Efficiency**:
- No longer need to allocate stack memory for console output when ring buffer is available
- Only uses stack memory for emergency fallback cases, reducing stack usage in normal operation
2. **Console Output Integrity**:
- Prevents console output corruption that could occur with concurrent writes from multiple tasks
- Serializes all console output through the main loop when possible
3. **Message Ordering**:
- Messages from different tasks may appear slightly out of order due to async delivery to main loop
- This trade-off is preferable to corrupted console output from concurrent writes
These improvements provide more reliable logging behavior, particularly under memory constraints or high logging volume, while maintaining thread safety and minimizing resource contention.
- Replaced inefficient vector-based buffer with a queue of discrete message buffers
- Moved common code to base class to reduce duplication
- Removed unnecessary data copying after partial sends
- Added small-buffer optimization to allow writing with backlog <256 bytes
- Moved common code to base class to reduce duplication
- Replaced inefficient vector-based buffer with a queue of discrete message buffers
- Moved common code to base class to reduce duplication
- Removed unnecessary data copying after partial sends
- Added small-buffer optimization to allow writing with backlog <256 bytes
- Moved common code to base class to reduce duplication
This PR optimizes memory usage in the APIPlaintextFrameHelper by replacing the vector-based header buffer with a fixed-size buffer, similar to what's already done in the noise protocol implementation.
- Replaced `std::vector<uint8_t> rx_header_buf_` with a 5-byte fixed buffer `uint8_t rx_header_buf_[5]`
- Implemented a modified protocol parsing algorithm that validates the indicator byte without storing it
- Ensured compatibility with noise protocol by supporting message sizes up to at least 65535 (requires 3-byte varint)
- Improved readability with clearer conditional logic and better comments
- Added detailed notes about protocol limitations and variable length encoding
- Reduces memory fragmentation by eliminating dynamic allocations during header parsing
This PR optimizes memory usage in the APIPlaintextFrameHelper by replacing the vector-based header buffer with a fixed-size buffer, similar to what's already done in the noise protocol implementation.
- Replaced `std::vector<uint8_t> rx_header_buf_` with a 5-byte fixed buffer `uint8_t rx_header_buf_[5]`
- Implemented a modified protocol parsing algorithm that validates the indicator byte without storing it
- Ensured compatibility with noise protocol by supporting message sizes up to at least 65535 (requires 3-byte varint)
- Improved readability with clearer conditional logic and better comments
- Added detailed notes about protocol limitations and variable length encoding
- Reduces memory fragmentation by eliminating dynamic allocations during header parsing