ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
task_log_buffer_zephyr.cpp
Go to the documentation of this file.
1#ifdef USE_ZEPHYR
2
4
5namespace esphome::logger {
6
7__thread bool non_main_task_recursion_guard; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
8
9#ifdef USE_ESPHOME_TASK_LOG_BUFFER
10
11static inline uint32_t total_size_in_32bit_words(uint16_t text_length) {
12 // Calculate total size in 32-bit words needed (header + text length + null terminator + 3(4 bytes alignment)
13 return (sizeof(TaskLogBuffer::LogMessage) + text_length + 1 + 3) / sizeof(uint32_t);
14}
15
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);
18}
19
21 // Storage is a member array (embedded in Logger), no heap allocation needed
22 this->mpsc_config_.buf = this->buf_storage_;
23 this->mpsc_config_.size = BUF_WORD_COUNT;
24 this->mpsc_config_.flags = MPSC_PBUF_MODE_OVERWRITE;
25 this->mpsc_config_.get_wlen = get_wlen;
26
27 mpsc_pbuf_init(&this->log_buffer_, &this->mpsc_config_);
28}
29
30bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
31 const char *format, va_list args) {
32 // Measure on a copy; the caller's va_list formats the text later
33 va_list args_copy;
34 va_copy(args_copy, args);
35 int ret = task_log_cap_length(vsnprintf(nullptr, 0, format, args_copy));
36 va_end(args_copy);
37 if (ret <= 0) {
38 return false; // Formatting error or empty message
39 }
40 uint16_t text_length = ret;
41 size_t total_size = total_size_in_32bit_words(text_length);
42 auto *msg = reinterpret_cast<LogMessage *>(mpsc_pbuf_alloc(&this->log_buffer_, total_size, K_NO_WAIT));
43 if (msg == nullptr) {
44 return false;
45 }
46 fill_task_log_header(msg, level, tag, line, thread_name);
47
48 // Format the message text directly into the acquired memory
49 // We add 1 to text_length to ensure space for null terminator during formatting
50 char *text_area = msg->text_data();
51 ret = vsnprintf(text_area, text_length + 1, format, args);
52
53 // Handle unexpected formatting error (ret < 0 is encoding error; ret == 0 is valid empty output)
54 if (ret < 0) {
55 // this should not happen, vsnprintf was called already once
56 // fill with '\n' to not call mpsc_pbuf_free from producer
57 // it will be trimmed anyway
58 for (size_t i = 0; i < text_length; ++i) {
59 text_area[i] = '\n';
60 }
61 text_area[text_length] = 0;
62 // do not return false to free the buffer from main thread
63 }
64
65 msg->text_length = text_length;
66
67 mpsc_pbuf_commit(&this->log_buffer_, reinterpret_cast<mpsc_pbuf_generic *>(msg));
68 return true;
69}
70
71bool TaskLogBuffer::borrow_message_main_loop(LogMessage *&message, uint16_t &text_length) {
72 if (this->current_token_) {
73 return false;
74 }
75
76 this->current_token_ = mpsc_pbuf_claim(&this->log_buffer_);
77
78 if (this->current_token_ == nullptr) {
79 return false;
80 }
81
82 // we claimed buffer already, const_cast is safe here
83 message = const_cast<LogMessage *>(reinterpret_cast<const LogMessage *>(this->current_token_));
84
85 text_length = trim_trailing_newlines(message->text_data(), message->text_length);
86
87 return true;
88}
89
91 if (this->current_token_ == nullptr) {
92 return;
93 }
94 mpsc_pbuf_free(&this->log_buffer_, this->current_token_);
95 this->current_token_ = nullptr;
96}
97#endif // USE_ESPHOME_TASK_LOG_BUFFER
98
99} // namespace esphome::logger
100
101#endif // USE_ZEPHYR
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]
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)
__thread bool non_main_task_recursion_guard
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
static void uint32_t