ESPHome 2026.12.0-dev
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
16size_t TaskLogBuffer::available_contiguous_space_() const {
17 if (this->head_ >= this->tail_) {
18 // head is ahead of or equal to tail
19 // Available space is from head to end, plus from start to tail
20 // But for contiguous, just from head to end (minus 1 to avoid head==tail ambiguity)
21 size_t space_to_end = CAPACITY - this->head_;
22 if (this->tail_ == 0) {
23 // Can't use the last byte or head would equal tail
24 return space_to_end > 0 ? space_to_end - 1 : 0;
25 }
26 return space_to_end;
27 } else {
28 // tail is ahead of head
29 // Available contiguous space is from head to tail - 1
30 return this->tail_ - this->head_ - 1;
31 }
32}
33
34bool TaskLogBuffer::borrow_message_main_loop(LogMessage *&message, uint16_t &text_length) {
35 // Check if mutex was initialized successfully
36 if (this->mutex_ == nullptr) {
37 return false;
38 }
39
40 // Try to take mutex without blocking - if busy, we'll get messages next loop iteration
41 if (xSemaphoreTake(this->mutex_, 0) != pdTRUE) {
42 return false;
43 }
44
45 if (this->head_ == this->tail_) {
46 xSemaphoreGive(this->mutex_);
47 return false;
48 }
49
50 // Less than a header before the end means the writer wrapped without a marker
51 if (CAPACITY - this->tail_ < sizeof(LogMessage) ||
52 reinterpret_cast<LogMessage *>(this->storage_ + this->tail_)->level == PADDING_MARKER_LEVEL) {
53 this->tail_ = 0;
54 }
55 LogMessage *msg = reinterpret_cast<LogMessage *>(this->storage_ + this->tail_);
56 message = msg;
57 this->current_message_size_ = message_total_size(msg->text_length);
58 text_length = msg->text_length;
59
60 // Keep mutex held until release_message_main_loop()
61 return true;
62}
63
65 // Advance tail past the current message
66 this->tail_ += this->current_message_size_;
67
68 // Handle wrap-around if we've reached the end
69 if (this->tail_ >= CAPACITY) {
70 this->tail_ = 0;
71 }
72
73 this->message_count_ = this->message_count_ - 1;
74 this->current_message_size_ = 0;
75
76 xSemaphoreGive(this->mutex_);
77}
78
79bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
80 const char *format, va_list args) {
81 // Measure on a copy; the caller's va_list formats the text later
82 va_list args_copy;
83 va_copy(args_copy, args);
84 int ret = task_log_cap_length(vsnprintf(nullptr, 0, format, args_copy));
85 va_end(args_copy);
86 if (ret <= 0) {
87 return false; // Formatting error or empty message
88 }
89 uint16_t text_length = ret;
90
91 // Calculate total size needed (header + text length + null terminator)
92 size_t total_size = message_total_size(text_length);
93
94 // Check if mutex was initialized successfully
95 if (this->mutex_ == nullptr) {
96 return false; // Mutex not initialized, fall back to direct output
97 }
98
99 // Try to acquire mutex without blocking - don't block logging tasks
100 if (xSemaphoreTake(this->mutex_, 0) != pdTRUE) {
101 return false; // Mutex busy, fall back to direct output
102 }
103
104 size_t write_pos = this->head_;
105 if (this->available_contiguous_space_() < total_size) {
106 // Wrap only if the start has room; head == tail would mean empty
107 if (this->head_ < this->tail_ || this->head_ == 0 || this->tail_ <= total_size) {
108 xSemaphoreGive(this->mutex_);
109 return false;
110 }
111 // Mark the skipped end when a header fits there
112 if (CAPACITY - this->head_ >= sizeof(LogMessage)) {
113 reinterpret_cast<LogMessage *>(this->storage_ + this->head_)->level = PADDING_MARKER_LEVEL;
114 }
115 write_pos = 0;
116 }
117
118 // Write message header
119 LogMessage *msg = reinterpret_cast<LogMessage *>(this->storage_ + write_pos);
120 fill_task_log_header(msg, level, tag, line, thread_name);
121
122 // Format the message text directly into the buffer
123 char *text_area = msg->text_data();
124 ret = vsnprintf(text_area, text_length + 1, format, args);
125
126 if (ret <= 0) {
127 xSemaphoreGive(this->mutex_);
128 return false;
129 }
130
131 // Trim before storing so the next message reuses the freed bytes
132 text_length = trim_trailing_newlines(text_area, text_length);
133 text_area[text_length] = '\0';
134 msg->text_length = text_length;
135
136 this->head_ = write_pos + message_total_size(text_length);
137 if (this->head_ >= CAPACITY) {
138 this->head_ = 0;
139 }
140
141 this->message_count_ = this->message_count_ + 1;
142
143 xSemaphoreGive(this->mutex_);
144 return true;
145}
146
147} // namespace esphome::logger
148
149#endif // USE_ESPHOME_TASK_LOG_BUFFER
150#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
int task_log_cap_length(int ret)
void fill_task_log_header(Message *msg, uint8_t level, const char *tag, uint16_t line, const char *thread_name)
uint16_t trim_trailing_newlines(const char *text, uint16_t length)
va_end(args)
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