ESPHome 2025.5.0
Loading...
Searching...
No Matches
task_log_buffer.h
Go to the documentation of this file.
1#pragma once
2
5
6#ifdef USE_ESPHOME_TASK_LOG_BUFFER
7#include <cstddef>
8#include <cstring>
9#include <memory>
10#include <atomic>
11#include <freertos/FreeRTOS.h>
12#include <freertos/ringbuf.h>
13
14namespace esphome {
15namespace logger {
16
18 public:
19 // Structure for a log message header (text data follows immediately after)
20 struct LogMessage {
21 const char *tag; // We store the pointer, assuming tags are static
22 char thread_name[16]; // Store thread name directly (only used for non-main threads)
23 uint16_t text_length; // Length of the message text (up to ~64KB)
24 uint16_t line; // Source code line number
25 uint8_t level; // Log level (0-7)
26
27 // Methods for accessing message contents
28 inline char *text_data() { return reinterpret_cast<char *>(this) + sizeof(LogMessage); }
29
30 inline const char *text_data() const { return reinterpret_cast<const char *>(this) + sizeof(LogMessage); }
31 };
32
33 // Constructor that takes a total buffer size
34 explicit TaskLogBuffer(size_t total_buffer_size);
36
37 // NOT thread-safe - borrow a message from the ring buffer, only call from main loop
38 bool borrow_message_main_loop(LogMessage **message, const char **text, void **received_token);
39
40 // NOT thread-safe - release a message buffer and update the counter, only call from main loop
41 void release_message_main_loop(void *token);
42
43 // Thread-safe - send a message to the ring buffer from any thread
44 bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, TaskHandle_t task_handle,
45 const char *format, va_list args);
46
47 // Check if there are messages ready to be processed using an atomic counter for performance
48 inline bool HOT has_messages() const {
49 return message_counter_.load(std::memory_order_relaxed) != last_processed_counter_;
50 }
51
52 // Get the total buffer size in bytes
53 inline size_t size() const { return size_; }
54
55 private:
56 RingbufHandle_t ring_buffer_{nullptr}; // FreeRTOS ring buffer handle
57 StaticRingbuffer_t structure_; // Static structure for the ring buffer
58 uint8_t *storage_{nullptr}; // Pointer to allocated memory
59 size_t size_{0}; // Size of allocated memory
60
61 // Atomic counter for message tracking (only differences matter)
62 std::atomic<uint16_t> message_counter_{0}; // Incremented when messages are committed
63 mutable uint16_t last_processed_counter_{0}; // Tracks last processed message
64};
65
66} // namespace logger
67} // namespace esphome
68
69#endif // USE_ESPHOME_TASK_LOG_BUFFER
TaskLogBuffer(size_t total_buffer_size)
bool borrow_message_main_loop(LogMessage **message, const char **text, void **received_token)
bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, TaskHandle_t task_handle, const char *format, va_list args)
void release_message_main_loop(void *token)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7