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 = CAPACITY - 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 if (CAPACITY - this->tail_ <
sizeof(LogMessage) ||
62 LogMessage *msg =
reinterpret_cast<LogMessage *
>(this->storage_ + this->tail_);
64 this->current_message_size_ = message_total_size(msg->text_length);
65 text_length = msg->text_length;
73 this->tail_ += this->current_message_size_;
76 if (this->tail_ >= CAPACITY) {
80 this->message_count_ = this->message_count_ - 1;
81 this->current_message_size_ = 0;
83 xSemaphoreGive(this->mutex_);
87 const char *
format, va_list args) {
90 va_copy(args_copy, args);
91 int ret = vsnprintf(
nullptr, 0,
format, args_copy);
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;
103 size_t total_size = message_total_size(text_length);
106 if (this->mutex_ ==
nullptr) {
111 if (xSemaphoreTake(this->mutex_, 0) != pdTRUE) {
115 size_t write_pos = this->head_;
116 if (this->available_contiguous_space_() < total_size) {
118 if (this->head_ < this->tail_ || this->head_ == 0 || this->tail_ <= total_size) {
119 xSemaphoreGive(this->mutex_);
123 if (CAPACITY - this->head_ >=
sizeof(LogMessage)) {
130 LogMessage *msg =
reinterpret_cast<LogMessage *
>(this->storage_ + write_pos);
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';
140 msg->thread_name[0] =
'\0';
144 char *text_area = msg->text_data();
145 ret = vsnprintf(text_area, text_length + 1,
format, args);
148 xSemaphoreGive(this->mutex_);
153 while (text_length > 0 && text_area[text_length - 1] ==
'\n') {
156 text_area[text_length] =
'\0';
157 msg->text_length = text_length;
159 this->head_ = write_pos + message_total_size(text_length);
160 if (this->head_ >= CAPACITY) {
164 this->message_count_ = this->message_count_ + 1;
166 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 LogString * message