7#ifdef USE_ESPHOME_TASK_LOG_BUFFER
12 this->size_ = total_buffer_size;
14 RAMAllocator<uint8_t> allocator;
15 this->storage_ = allocator.allocate(this->size_);
17 this->mutex_ = xSemaphoreCreateMutex();
21 if (this->mutex_ !=
nullptr) {
22 vSemaphoreDelete(this->mutex_);
23 this->mutex_ =
nullptr;
25 if (this->storage_ !=
nullptr) {
26 RAMAllocator<uint8_t> allocator;
27 allocator.deallocate(this->storage_, this->size_);
28 this->storage_ =
nullptr;
32size_t TaskLogBuffer::available_contiguous_space()
const {
33 if (this->head_ >= this->tail_) {
37 size_t space_to_end = this->size_ - this->head_;
38 if (this->tail_ == 0) {
40 return space_to_end > 0 ? space_to_end - 1 : 0;
46 return this->tail_ - this->head_ - 1;
52 if (this->mutex_ ==
nullptr || this->storage_ ==
nullptr) {
57 if (xSemaphoreTake(this->mutex_, 0) != pdTRUE) {
61 if (this->head_ == this->tail_) {
62 xSemaphoreGive(this->mutex_);
67 LogMessage *msg =
reinterpret_cast<LogMessage *
>(this->storage_ + this->tail_);
74 msg =
reinterpret_cast<LogMessage *
>(this->storage_);
77 text_length = msg->text_length;
78 this->current_message_size_ = message_total_size(msg->text_length);
86 this->tail_ += this->current_message_size_;
89 if (this->tail_ >= this->size_) {
93 this->message_count_--;
94 this->current_message_size_ = 0;
96 xSemaphoreGive(this->mutex_);
100 const char *format, va_list args) {
103 va_copy(args_copy, args);
104 int ret = vsnprintf(
nullptr, 0, format, args_copy);
112 static constexpr size_t MAX_TEXT_SIZE = 255;
113 size_t text_length = (
static_cast<size_t>(ret) > MAX_TEXT_SIZE) ? MAX_TEXT_SIZE : ret;
116 size_t total_size = message_total_size(text_length);
119 if (this->mutex_ ==
nullptr || this->storage_ ==
nullptr) {
124 if (xSemaphoreTake(this->mutex_, 0) != pdTRUE) {
129 size_t contiguous = this->available_contiguous_space();
131 if (contiguous < total_size) {
134 size_t space_at_start = (this->head_ >= this->tail_) ? this->tail_ : 0;
135 if (space_at_start > 0) {
140 constexpr size_t PADDING_MARKER_MIN_SPACE = offsetof(LogMessage, level) + 1;
142 if (space_at_start >= total_size && this->head_ > 0 && contiguous >= PADDING_MARKER_MIN_SPACE) {
144 LogMessage *padding =
reinterpret_cast<LogMessage *
>(this->storage_ + this->head_);
149 xSemaphoreGive(this->mutex_);
155 LogMessage *msg =
reinterpret_cast<LogMessage *
>(this->storage_ + this->head_);
161 if (thread_name !=
nullptr) {
162 strncpy(msg->thread_name, thread_name,
sizeof(msg->thread_name) - 1);
163 msg->thread_name[
sizeof(msg->thread_name) - 1] =
'\0';
165 msg->thread_name[0] =
'\0';
169 char *text_area = msg->text_data();
170 ret = vsnprintf(text_area, text_length + 1, format, args);
173 xSemaphoreGive(this->mutex_);
178 while (text_length > 0 && text_area[text_length - 1] ==
'\n') {
182 msg->text_length = text_length;
185 this->head_ += total_size;
188 if (this->head_ >= this->size_) {
192 this->message_count_++;
194 xSemaphoreGive(this->mutex_);
void release_message_main_loop()
TaskLogBuffer(size_t total_buffer_size)
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