ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
task_log_buffer_libretiny.cpp
Go to the documentation of this file.
1#ifdef USE_LIBRETINY
2
4#include "esphome/core/log.h"
5
6#ifdef USE_ESPHOME_TASK_LOG_BUFFER
7
8namespace esphome::logger {
9
11 // Create mutex for thread-safe access
12 // Storage is a member array (embedded in Logger), no heap allocation needed
13 this->mutex_ = xSemaphoreCreateMutex();
14}
15
17 if (this->mutex_ != nullptr) {
18 vSemaphoreDelete(this->mutex_);
19 this->mutex_ = nullptr;
20 }
21}
22
23size_t TaskLogBuffer::available_contiguous_space_() const {
24 if (this->head_ >= this->tail_) {
25 // head is ahead of or equal to tail
26 // Available space is from head to end, plus from start to tail
27 // But for contiguous, just from head to end (minus 1 to avoid head==tail ambiguity)
28 size_t space_to_end = CAPACITY - this->head_;
29 if (this->tail_ == 0) {
30 // Can't use the last byte or head would equal tail
31 return space_to_end > 0 ? space_to_end - 1 : 0;
32 }
33 return space_to_end;
34 } else {
35 // tail is ahead of head
36 // Available contiguous space is from head to tail - 1
37 return this->tail_ - this->head_ - 1;
38 }
39}
40
41bool TaskLogBuffer::borrow_message_main_loop(LogMessage *&message, uint16_t &text_length) {
42 // Check if mutex was initialized successfully
43 if (this->mutex_ == nullptr) {
44 return false;
45 }
46
47 // Try to take mutex without blocking - if busy, we'll get messages next loop iteration
48 if (xSemaphoreTake(this->mutex_, 0) != pdTRUE) {
49 return false;
50 }
51
52 if (this->head_ == this->tail_) {
53 xSemaphoreGive(this->mutex_);
54 return false;
55 }
56
57 // Less than a header before the end means the writer wrapped without a marker
58 if (CAPACITY - this->tail_ < sizeof(LogMessage) ||
59 reinterpret_cast<LogMessage *>(this->storage_ + this->tail_)->level == PADDING_MARKER_LEVEL) {
60 this->tail_ = 0;
61 }
62 LogMessage *msg = reinterpret_cast<LogMessage *>(this->storage_ + this->tail_);
63 message = msg;
64 this->current_message_size_ = message_total_size(msg->text_length);
65 text_length = msg->text_length;
66
67 // Keep mutex held until release_message_main_loop()
68 return true;
69}
70
72 // Advance tail past the current message
73 this->tail_ += this->current_message_size_;
74
75 // Handle wrap-around if we've reached the end
76 if (this->tail_ >= CAPACITY) {
77 this->tail_ = 0;
78 }
79
80 this->message_count_ = this->message_count_ - 1;
81 this->current_message_size_ = 0;
82
83 xSemaphoreGive(this->mutex_);
84}
85
86bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
87 const char *format, va_list args) {
88 // First, calculate the exact length needed using a null buffer (no actual writing)
89 va_list args_copy;
90 va_copy(args_copy, args);
91 int ret = vsnprintf(nullptr, 0, format, args_copy);
92 va_end(args_copy);
93
94 if (ret <= 0) {
95 return false; // Formatting error or empty message
96 }
97
98 // Calculate actual text length (capped to maximum size)
99 static constexpr size_t MAX_TEXT_SIZE = 255;
100 size_t text_length = (static_cast<size_t>(ret) > MAX_TEXT_SIZE) ? MAX_TEXT_SIZE : ret;
101
102 // Calculate total size needed (header + text length + null terminator)
103 size_t total_size = message_total_size(text_length);
104
105 // Check if mutex was initialized successfully
106 if (this->mutex_ == nullptr) {
107 return false; // Mutex not initialized, fall back to direct output
108 }
109
110 // Try to acquire mutex without blocking - don't block logging tasks
111 if (xSemaphoreTake(this->mutex_, 0) != pdTRUE) {
112 return false; // Mutex busy, fall back to direct output
113 }
114
115 size_t write_pos = this->head_;
116 if (this->available_contiguous_space_() < total_size) {
117 // Wrap only if the start has room; head == tail would mean empty
118 if (this->head_ < this->tail_ || this->head_ == 0 || this->tail_ <= total_size) {
119 xSemaphoreGive(this->mutex_);
120 return false;
121 }
122 // Mark the skipped end when a header fits there
123 if (CAPACITY - this->head_ >= sizeof(LogMessage)) {
124 reinterpret_cast<LogMessage *>(this->storage_ + this->head_)->level = PADDING_MARKER_LEVEL;
125 }
126 write_pos = 0;
127 }
128
129 // Write message header
130 LogMessage *msg = reinterpret_cast<LogMessage *>(this->storage_ + write_pos);
131 msg->level = level;
132 msg->tag = tag;
133 msg->line = line;
134
135 // Store the thread name now to avoid crashes if task is deleted before processing
136 if (thread_name != nullptr) {
137 strncpy(msg->thread_name, thread_name, sizeof(msg->thread_name) - 1);
138 msg->thread_name[sizeof(msg->thread_name) - 1] = '\0';
139 } else {
140 msg->thread_name[0] = '\0';
141 }
142
143 // Format the message text directly into the buffer
144 char *text_area = msg->text_data();
145 ret = vsnprintf(text_area, text_length + 1, format, args);
146
147 if (ret <= 0) {
148 xSemaphoreGive(this->mutex_);
149 return false;
150 }
151
152 // Trim before storing so the next message reuses the freed bytes
153 while (text_length > 0 && text_area[text_length - 1] == '\n') {
154 text_length--;
155 }
156 text_area[text_length] = '\0';
157 msg->text_length = text_length;
158
159 this->head_ = write_pos + message_total_size(text_length);
160 if (this->head_ >= CAPACITY) {
161 this->head_ = 0;
162 }
163
164 this->message_count_ = this->message_count_ + 1;
165
166 xSemaphoreGive(this->mutex_);
167 return true;
168}
169
170} // namespace esphome::logger
171
172#endif // USE_ESPHOME_TASK_LOG_BUFFER
173#endif // USE_LIBRETINY
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)
static constexpr uint8_t PADDING_MARKER_LEVEL
const LogString * message
Definition component.cpp:35
int ret
const char * format
va_end(args)