ESPHome 2025.5.0
Loading...
Searching...
No Matches
task_log_buffer.cpp
Go to the documentation of this file.
1
2#include "task_log_buffer.h"
4#include "esphome/core/log.h"
5
6#ifdef USE_ESPHOME_TASK_LOG_BUFFER
7
8namespace esphome {
9namespace logger {
10
11TaskLogBuffer::TaskLogBuffer(size_t total_buffer_size) {
12 // Store the buffer size
13 this->size_ = total_buffer_size;
14 // Allocate memory for the ring buffer using ESPHome's RAM allocator
15 RAMAllocator<uint8_t> allocator;
16 this->storage_ = allocator.allocate(this->size_);
17 // Create a static ring buffer with RINGBUF_TYPE_NOSPLIT for message integrity
18 this->ring_buffer_ = xRingbufferCreateStatic(this->size_, RINGBUF_TYPE_NOSPLIT, this->storage_, &this->structure_);
19}
20
22 if (this->ring_buffer_ != nullptr) {
23 // Delete the ring buffer
24 vRingbufferDelete(this->ring_buffer_);
25 this->ring_buffer_ = nullptr;
26
27 // Free the allocated memory
28 RAMAllocator<uint8_t> allocator;
29 allocator.deallocate(this->storage_, this->size_);
30 this->storage_ = nullptr;
31 }
32}
33
34bool TaskLogBuffer::borrow_message_main_loop(LogMessage **message, const char **text, void **received_token) {
35 if (message == nullptr || text == nullptr || received_token == nullptr) {
36 return false;
37 }
38
39 size_t item_size = 0;
40 void *received_item = xRingbufferReceive(ring_buffer_, &item_size, 0);
41 if (received_item == nullptr) {
42 return false;
43 }
44
45 LogMessage *msg = static_cast<LogMessage *>(received_item);
46 *message = msg;
47 *text = msg->text_data();
48 *received_token = received_item;
49
50 return true;
51}
52
54 if (token == nullptr) {
55 return;
56 }
57 vRingbufferReturnItem(ring_buffer_, token);
58 // Update counter to mark all messages as processed
59 last_processed_counter_ = message_counter_.load(std::memory_order_relaxed);
60}
61
62bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, TaskHandle_t task_handle,
63 const char *format, va_list args) {
64 // First, calculate the exact length needed using a null buffer (no actual writing)
65 va_list args_copy;
66 va_copy(args_copy, args);
67 int ret = vsnprintf(nullptr, 0, format, args_copy);
68 va_end(args_copy);
69
70 if (ret <= 0) {
71 return false; // Formatting error or empty message
72 }
73
74 // Calculate actual text length (capped to maximum size)
75 static constexpr size_t MAX_TEXT_SIZE = 255;
76 size_t text_length = (static_cast<size_t>(ret) > MAX_TEXT_SIZE) ? MAX_TEXT_SIZE : ret;
77
78 // Calculate total size needed (header + text length + null terminator)
79 size_t total_size = sizeof(LogMessage) + text_length + 1;
80
81 // Acquire memory directly from the ring buffer
82 void *acquired_memory = nullptr;
83 BaseType_t result = xRingbufferSendAcquire(ring_buffer_, &acquired_memory, total_size, 0);
84
85 if (result != pdTRUE || acquired_memory == nullptr) {
86 return false; // Failed to acquire memory
87 }
88
89 // Set up the message header in the acquired memory
90 LogMessage *msg = static_cast<LogMessage *>(acquired_memory);
91 msg->level = level;
92 msg->tag = tag;
93 msg->line = line;
94
95 // Store the thread name now instead of waiting until main loop processing
96 // This avoids crashes if the task completes or is deleted between when this message
97 // is enqueued and when it's processed by the main loop
98 const char *thread_name = pcTaskGetName(task_handle);
99 if (thread_name != nullptr) {
100 strncpy(msg->thread_name, thread_name, sizeof(msg->thread_name) - 1);
101 msg->thread_name[sizeof(msg->thread_name) - 1] = '\0'; // Ensure null termination
102 } else {
103 msg->thread_name[0] = '\0'; // Empty string if no thread name
104 }
105
106 // Format the message text directly into the acquired memory
107 // We add 1 to text_length to ensure space for null terminator during formatting
108 char *text_area = msg->text_data();
109 ret = vsnprintf(text_area, text_length + 1, format, args);
110
111 // Handle unexpected formatting error
112 if (ret <= 0) {
113 vRingbufferReturnItem(ring_buffer_, acquired_memory);
114 return false;
115 }
116
117 // Remove trailing newlines
118 while (text_length > 0 && text_area[text_length - 1] == '\n') {
119 text_length--;
120 }
121
122 msg->text_length = text_length;
123 // Complete the send operation with the acquired memory
124 result = xRingbufferSendComplete(ring_buffer_, acquired_memory);
125
126 if (result != pdTRUE) {
127 return false; // Failed to complete the message send
128 }
129
130 // Message sent successfully, increment the counter
131 message_counter_.fetch_add(1, std::memory_order_relaxed);
132 return true;
133}
134
135} // namespace logger
136} // namespace esphome
137
138#endif // USE_ESPHOME_TASK_LOG_BUFFER
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:683
void deallocate(T *p, size_t n)
Definition helpers.h:741
T * allocate(size_t n)
Definition helpers.h:703
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