6#ifdef USE_ESPHOME_TASK_LOG_BUFFER
13 this->mutex_ = xSemaphoreCreateMutex();
17 if (this->mutex_ !=
nullptr) {
18 vSemaphoreDelete(this->mutex_);
19 this->mutex_ =
nullptr;
23size_t TaskLogBuffer::available_contiguous_space()
const {
24 if (this->head_ >= this->tail_) {
28 size_t space_to_end = ESPHOME_TASK_LOG_BUFFER_SIZE - this->head_;
29 if (this->tail_ == 0) {
31 return space_to_end > 0 ? space_to_end - 1 : 0;
37 return this->tail_ - this->head_ - 1;
43 if (this->mutex_ ==
nullptr) {
48 if (xSemaphoreTake(this->mutex_, 0) != pdTRUE) {
52 if (this->head_ == this->tail_) {
53 xSemaphoreGive(this->mutex_);
58 LogMessage *msg =
reinterpret_cast<LogMessage *
>(this->storage_ + this->tail_);
65 msg =
reinterpret_cast<LogMessage *
>(this->storage_);
68 text_length = msg->text_length;
69 this->current_message_size_ = message_total_size(msg->text_length);
77 this->tail_ += this->current_message_size_;
80 if (this->tail_ >= ESPHOME_TASK_LOG_BUFFER_SIZE) {
84 this->message_count_--;
85 this->current_message_size_ = 0;
87 xSemaphoreGive(this->mutex_);
94 va_copy(args_copy,
args);
95 int ret = vsnprintf(
nullptr, 0,
format, args_copy);
103 static constexpr size_t MAX_TEXT_SIZE = 255;
104 size_t text_length = (
static_cast<size_t>(ret) > MAX_TEXT_SIZE) ? MAX_TEXT_SIZE : ret;
107 size_t total_size = message_total_size(text_length);
110 if (this->mutex_ ==
nullptr) {
115 if (xSemaphoreTake(this->mutex_, 0) != pdTRUE) {
120 size_t contiguous = this->available_contiguous_space();
122 if (contiguous < total_size) {
125 size_t space_at_start = (this->head_ >= this->tail_) ? this->tail_ : 0;
126 if (space_at_start > 0) {
131 constexpr size_t PADDING_MARKER_MIN_SPACE = offsetof(LogMessage, level) + 1;
133 if (space_at_start >= total_size && this->head_ > 0 && contiguous >= PADDING_MARKER_MIN_SPACE) {
135 LogMessage *padding =
reinterpret_cast<LogMessage *
>(this->storage_ + this->head_);
140 xSemaphoreGive(this->mutex_);
146 LogMessage *msg =
reinterpret_cast<LogMessage *
>(this->storage_ + this->head_);
152 if (thread_name !=
nullptr) {
153 strncpy(msg->thread_name, thread_name,
sizeof(msg->thread_name) - 1);
154 msg->thread_name[
sizeof(msg->thread_name) - 1] =
'\0';
156 msg->thread_name[0] =
'\0';
160 char *text_area = msg->text_data();
161 ret = vsnprintf(text_area, text_length + 1,
format,
args);
164 xSemaphoreGive(this->mutex_);
169 while (text_length > 0 && text_area[text_length - 1] ==
'\n') {
173 msg->text_length = text_length;
176 this->head_ += total_size;
179 if (this->head_ >= ESPHOME_TASK_LOG_BUFFER_SIZE) {
183 this->message_count_++;
185 xSemaphoreGive(this->mutex_);
void release_message_main_loop()
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 char int const __FlashStringHelper * format
const char int const __FlashStringHelper va_list args