ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
task_log_buffer_esp32.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
7
8#ifdef USE_ESPHOME_TASK_LOG_BUFFER
9#include <cstddef>
10#include <cstring>
11#include <atomic>
12#include <freertos/FreeRTOS.h>
13#include <freertos/ringbuf.h>
14
15#include "task_log_message.h"
16
17namespace esphome::logger {
18
35class TaskLogBuffer {
36 public:
38
40 // No destructor: Logger is never destroyed
41
42 // NOT thread-safe - borrow a message from the ring buffer, only call from main loop
43 bool borrow_message_main_loop(LogMessage *&message, uint16_t &text_length);
44
45 // NOT thread-safe - release a message buffer and update the counter, only call from main loop
47
48 // Thread-safe - send a message to the ring buffer from any thread
49 bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
50 const char *format, va_list args);
51
52 // Check if there are messages ready to be processed using an atomic counter for performance
53 inline bool HOT has_messages() const {
54 return message_counter_.load(std::memory_order_relaxed) != last_processed_counter_;
55 }
56
57 // Get the total buffer size in bytes
58 static constexpr size_t size() { return ESPHOME_TASK_LOG_BUFFER_SIZE; }
59
60 private:
61 // xRingbufferCreateStatic() returns the static structure itself as the handle; it only
62 // returns NULL for a no-split size that is unaligned or under two item headers
63 static_assert(ESPHOME_TASK_LOG_BUFFER_SIZE % 4 == 0, "task_log_buffer_size must be a multiple of 4");
64 RingbufHandle_t handle_() { return &this->structure_; }
65
66 StaticRingbuffer_t structure_; // Static structure for the ring buffer
67 uint8_t storage_[ESPHOME_TASK_LOG_BUFFER_SIZE]; // Embedded in Logger (no separate heap allocation)
68
69 // Atomic counter for message tracking (only differences matter)
70 std::atomic<uint16_t> message_counter_{0}; // Incremented when messages are committed
71 mutable uint16_t last_processed_counter_{0}; // Tracks last processed message
72 void *current_token_{nullptr};
73};
74
75} // namespace esphome::logger
76
77#endif // USE_ESPHOME_TASK_LOG_BUFFER
78#endif // USE_ESP32
bool borrow_message_main_loop(LogMessage *&message, uint16_t &text_length)
bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name, const char *format, va_list args)
const LogString * message
Definition component.cpp:35
void const char int line
Definition log.h:110
void const char int const char * format
Definition log.h:110
void const char * tag
Definition log.h:110