ESPHome 2026.4.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
15namespace esphome::logger {
16
33class TaskLogBuffer {
34 public:
35 // Structure for a log message header (text data follows immediately after)
36 struct LogMessage {
37 const char *tag; // We store the pointer, assuming tags are static
38 char thread_name[16]; // Store thread name directly (only used for non-main threads)
39 uint16_t text_length; // Length of the message text (up to ~64KB)
40 uint16_t line; // Source code line number
41 uint8_t level; // Log level (0-7)
42
43 // Methods for accessing message contents
44 inline char *text_data() { return reinterpret_cast<char *>(this) + sizeof(LogMessage); }
45
46 inline const char *text_data() const { return reinterpret_cast<const char *>(this) + sizeof(LogMessage); }
47 };
48
51
52 // NOT thread-safe - borrow a message from the ring buffer, only call from main loop
53 bool borrow_message_main_loop(LogMessage *&message, uint16_t &text_length);
54
55 // NOT thread-safe - release a message buffer and update the counter, only call from main loop
57
58 // Thread-safe - send a message to the ring buffer from any thread
59 bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
60 const char *format, va_list args);
61
62 // Check if there are messages ready to be processed using an atomic counter for performance
63 inline bool HOT has_messages() const {
64 return message_counter_.load(std::memory_order_relaxed) != last_processed_counter_;
65 }
66
67 // Get the total buffer size in bytes
68 static constexpr size_t size() { return ESPHOME_TASK_LOG_BUFFER_SIZE; }
69
70 private:
71 RingbufHandle_t ring_buffer_{nullptr}; // FreeRTOS ring buffer handle
72 StaticRingbuffer_t structure_; // Static structure for the ring buffer
73 uint8_t storage_[ESPHOME_TASK_LOG_BUFFER_SIZE]; // Embedded in Logger (no separate heap allocation)
74
75 // Atomic counter for message tracking (only differences matter)
76 std::atomic<uint16_t> message_counter_{0}; // Incremented when messages are committed
77 mutable uint16_t last_processed_counter_{0}; // Tracks last processed message
78 void *current_token_{nullptr};
79};
80
81} // namespace esphome::logger
82
83#endif // USE_ESPHOME_TASK_LOG_BUFFER
84#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 char * message
Definition component.cpp:35
const char int line
Definition log.h:74
const char * tag
Definition log.h:74
const char int const __FlashStringHelper * format
Definition log.h:74
const char int const __FlashStringHelper va_list args
Definition log.h:74