9#ifdef USE_ESPHOME_TASK_LOG_BUFFER
11static inline uint32_t total_size_in_32bit_words(uint16_t text_length) {
16static inline uint32_t get_wlen(
const mpsc_pbuf_generic *item) {
17 return total_size_in_32bit_words(
reinterpret_cast<const TaskLogBuffer::LogMessage *
>(item)->text_length);
34 va_copy(args_copy,
args);
35 int ret = vsnprintf(
nullptr, 0,
format, args_copy);
43 static constexpr size_t MAX_TEXT_SIZE = 255;
44 size_t text_length = (
static_cast<size_t>(ret) > MAX_TEXT_SIZE) ? MAX_TEXT_SIZE : ret;
45 size_t total_size = total_size_in_32bit_words(text_length);
46 auto *msg =
reinterpret_cast<LogMessage *
>(mpsc_pbuf_alloc(&this->
log_buffer_, total_size, K_NO_WAIT));
53 strncpy(msg->thread_name, thread_name,
sizeof(msg->thread_name) - 1);
54 msg->thread_name[
sizeof(msg->thread_name) - 1] =
'\0';
58 char *text_area = msg->text_data();
59 ret = vsnprintf(text_area, text_length + 1,
format,
args);
66 for (
size_t i = 0; i < text_length; ++i) {
69 text_area[text_length] = 0;
73 msg->text_length = text_length;
75 mpsc_pbuf_commit(&this->
log_buffer_,
reinterpret_cast<mpsc_pbuf_generic *
>(msg));
80 if (this->current_token_) {
84 this->current_token_ = mpsc_pbuf_claim(&this->
log_buffer_);
86 if (this->current_token_ ==
nullptr) {
91 message =
const_cast<LogMessage *
>(
reinterpret_cast<const LogMessage *
>(this->current_token_));
93 text_length =
message->text_length;
95 while (text_length > 0 &&
message->text_data()[text_length - 1] ==
'\n') {
103 if (this->current_token_ ==
nullptr) {
106 mpsc_pbuf_free(&this->
log_buffer_, this->current_token_);
107 this->current_token_ =
nullptr;
void release_message_main_loop()
mpsc_pbuf_buffer log_buffer_
mpsc_pbuf_buffer_config mpsc_config_
static constexpr size_t BUF_WORD_COUNT
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)
uint32_t buf_storage_[BUF_WORD_COUNT]
__thread bool non_main_task_recursion_guard_
const char int const __FlashStringHelper * format
const char int const __FlashStringHelper va_list args