|
ESPHome 2026.5.0-dev
|
Task log buffer for ESP32 platform using FreeRTOS ring buffer. More...
#include <task_log_buffer_esp32.h>
Data Structures | |
| struct | LogMessage |
Public Member Functions | |
| TaskLogBuffer () | |
| ~TaskLogBuffer () | |
| bool | borrow_message_main_loop (LogMessage *&message, uint16_t &text_length) |
| void | release_message_main_loop () |
| bool | send_message_thread_safe (uint8_t level, const char *tag, uint16_t line, const char *thread_name, const char *format, va_list args) |
| bool HOT | has_messages () const |
| TaskLogBuffer ()=default | |
| bool | borrow_message_main_loop (LogMessage *&message, uint16_t &text_length) |
| void | release_message_main_loop () |
| bool | send_message_thread_safe (uint8_t level, const char *tag, uint16_t line, const char *thread_name, const char *format, va_list args) |
| bool HOT | has_messages () const |
| TaskLogBuffer () | |
| ~TaskLogBuffer () | |
| bool | borrow_message_main_loop (LogMessage *&message, uint16_t &text_length) |
| void | release_message_main_loop () |
| bool | send_message_thread_safe (uint8_t level, const char *tag, uint16_t line, const char *thread_name, const char *format, va_list args) |
| bool HOT | has_messages () const |
| TaskLogBuffer () | |
| ~TaskLogBuffer ()=default | |
| bool HOT | has_messages () |
| bool | borrow_message_main_loop (LogMessage *&message, uint16_t &text_length) |
| void | release_message_main_loop () |
| bool | send_message_thread_safe (uint8_t level, const char *tag, uint16_t line, const char *thread_name, const char *format, va_list args) |
Static Public Member Functions | |
| static constexpr size_t | size () |
| static constexpr size_t | size () |
| static constexpr size_t | size () |
| static constexpr size_t | size () |
Static Public Attributes | |
| static constexpr uint8_t | PADDING_MARKER_LEVEL = 0xFF |
Protected Attributes | |
| uint32_t | buf_storage_ [BUF_WORD_COUNT] |
| mpsc_pbuf_buffer_config | mpsc_config_ {} |
| mpsc_pbuf_buffer | log_buffer_ {} |
| const mpsc_pbuf_generic * | current_token_ {} |
Static Protected Attributes | |
| static constexpr size_t | BUF_WORD_COUNT = (ESPHOME_TASK_LOG_BUFFER_SIZE + 3) / sizeof(uint32_t) |
Task log buffer for ESP32 platform using FreeRTOS ring buffer.
Task log buffer for LibreTiny platform using mutex-protected circular buffer.
Lock-free task log buffer for host platform.
Threading Model: Multi-Producer Single-Consumer (MPSC)
This uses the FreeRTOS ring buffer (RINGBUF_TYPE_NOSPLIT) which provides built-in thread-safety for the MPSC pattern. The ring buffer ensures message integrity - each message is stored contiguously.
Design:
Threading Model: Multi-Producer Single-Consumer (MPSC)
Only the main loop thread calls borrow_message_main_loop() and release_message_main_loop()
Producers (multiple threads) Consumer (main loop only) │ │ ▼ ▼ acquire_write_slot_() bool borrow_message_main_loop() CAS on reserve_index_ read write_index_ │ check ready flag ▼ │ write to slot (exclusive) ▼ │ read slot data ▼ │ commit_write_slot_() ▼ set ready=true release_message_main_loop() advance write_index_ set ready=false advance read_index_
This implements a lock-free ring buffer for log messages on the host platform. It uses atomic compare-and-swap (CAS) operations for thread-safe slot reservation without requiring mutexes in the hot path.
Design:
Why This Is Critical: Without thread-safe logging, when a non-main task logs a message, it would directly call the logger which builds a protobuf message in a shared buffer. If this happens while the main loop is also using that buffer (e.g., sending API responses), the buffer gets corrupted, sending garbage to all connected API clients and breaking their connections. This buffer ensures log messages from other tasks are queued safely and processed only from the main loop.
Threading Model: Multi-Producer Single-Consumer (MPSC)
This uses a simple circular buffer protected by a FreeRTOS mutex. Unlike ESP32, LibreTiny lacks hardware atomic support (ARM968E-S has no LDREX/STREX), so we use a volatile counter for fast has_messages() checks instead of atomics.
Design:
Definition at line 18 of file task_log_buffer_zephyr.h.
| esphome::logger::TaskLogBuffer::TaskLogBuffer | ( | ) |
Definition at line 10 of file task_log_buffer_esp32.cpp.
| esphome::logger::TaskLogBuffer::~TaskLogBuffer | ( | ) |
Definition at line 17 of file task_log_buffer_esp32.cpp.
|
default |
| esphome::logger::TaskLogBuffer::TaskLogBuffer | ( | ) |
| esphome::logger::TaskLogBuffer::~TaskLogBuffer | ( | ) |
| esphome::logger::TaskLogBuffer::TaskLogBuffer | ( | ) |
|
default |
| bool esphome::logger::TaskLogBuffer::borrow_message_main_loop | ( | LogMessage *& | message, |
| uint16_t & | text_length ) |
Definition at line 24 of file task_log_buffer_esp32.cpp.
| bool esphome::logger::TaskLogBuffer::borrow_message_main_loop | ( | LogMessage *& | message, |
| uint16_t & | text_length ) |
| bool esphome::logger::TaskLogBuffer::borrow_message_main_loop | ( | LogMessage *& | message, |
| uint16_t & | text_length ) |
| bool esphome::logger::TaskLogBuffer::borrow_message_main_loop | ( | LogMessage *& | message, |
| uint16_t & | text_length ) |
|
inline |
Definition at line 40 of file task_log_buffer_zephyr.h.
|
inline |
Definition at line 63 of file task_log_buffer_esp32.h.
|
inline |
Definition at line 88 of file task_log_buffer_host.h.
|
inline |
Definition at line 77 of file task_log_buffer_libretiny.h.
| void esphome::logger::TaskLogBuffer::release_message_main_loop | ( | ) |
Definition at line 43 of file task_log_buffer_esp32.cpp.
| void esphome::logger::TaskLogBuffer::release_message_main_loop | ( | ) |
| void esphome::logger::TaskLogBuffer::release_message_main_loop | ( | ) |
| void esphome::logger::TaskLogBuffer::release_message_main_loop | ( | ) |
| bool esphome::logger::TaskLogBuffer::send_message_thread_safe | ( | uint8_t | level, |
| const char * | tag, | ||
| uint16_t | line, | ||
| const char * | thread_name, | ||
| const char * | format, | ||
| va_list | args ) |
Definition at line 53 of file task_log_buffer_esp32.cpp.
| bool esphome::logger::TaskLogBuffer::send_message_thread_safe | ( | uint8_t | level, |
| const char * | tag, | ||
| uint16_t | line, | ||
| const char * | thread_name, | ||
| const char * | format, | ||
| va_list | args ) |
| bool esphome::logger::TaskLogBuffer::send_message_thread_safe | ( | uint8_t | level, |
| const char * | tag, | ||
| uint16_t | line, | ||
| const char * | thread_name, | ||
| const char * | format, | ||
| va_list | args ) |
| bool esphome::logger::TaskLogBuffer::send_message_thread_safe | ( | uint8_t | level, |
| const char * | tag, | ||
| uint16_t | line, | ||
| const char * | thread_name, | ||
| const char * | format, | ||
| va_list | args ) |
|
inlinestaticconstexpr |
Definition at line 68 of file task_log_buffer_esp32.h.
|
inlinestaticconstexpr |
Definition at line 93 of file task_log_buffer_host.h.
|
inlinestaticconstexpr |
Definition at line 80 of file task_log_buffer_libretiny.h.
|
inlinestaticconstexpr |
Definition at line 43 of file task_log_buffer_zephyr.h.
|
protected |
Definition at line 58 of file task_log_buffer_zephyr.h.
|
staticconstexprprotected |
Definition at line 57 of file task_log_buffer_zephyr.h.
|
protected |
Definition at line 61 of file task_log_buffer_zephyr.h.
|
protected |
Definition at line 60 of file task_log_buffer_zephyr.h.
|
protected |
Definition at line 59 of file task_log_buffer_zephyr.h.
|
staticconstexpr |
Definition at line 60 of file task_log_buffer_libretiny.h.