ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
task_log_buffer_host.cpp
Go to the documentation of this file.
1#ifdef USE_HOST
2
4
5#ifdef USE_ESPHOME_TASK_LOG_BUFFER
6
7#include "esphome/core/log.h"
8#include <algorithm>
9#include <cstdio>
10
11namespace esphome::logger {
12
13int TaskLogBuffer::acquire_write_slot_() {
14 // Try to reserve a slot using compare-and-swap
15 size_t current_reserve = this->reserve_index_.load(std::memory_order_relaxed);
16
17 while (true) {
18 // Calculate next index (with wrap-around)
19 size_t next_reserve = (current_reserve + 1) % ESPHOME_TASK_LOG_BUFFER_SIZE;
20
21 // Check if buffer would be full
22 // Buffer is full when next write position equals read position
23 size_t current_read = this->read_index_.load(std::memory_order_acquire);
24 if (next_reserve == current_read) {
25 return -1; // Buffer full
26 }
27
28 // Try to claim this slot
29 if (this->reserve_index_.compare_exchange_weak(current_reserve, next_reserve, std::memory_order_acq_rel,
30 std::memory_order_relaxed)) {
31 return static_cast<int>(current_reserve);
32 }
33 // If CAS failed, current_reserve was updated, retry with new value
34 }
35}
36
37void TaskLogBuffer::commit_write_slot_(int slot_index) {
38 // Mark the slot as ready for reading
39 this->slots_[slot_index].ready.store(true, std::memory_order_release);
40
41 // Try to advance the write_index if we're the next expected commit
42 // This ensures messages are read in order
43 size_t expected = slot_index;
44 size_t next = (slot_index + 1) % ESPHOME_TASK_LOG_BUFFER_SIZE;
45
46 // We only advance write_index if this slot is the next one expected
47 // This handles out-of-order commits correctly
48 while (true) {
49 if (!this->write_index_.compare_exchange_weak(expected, next, std::memory_order_release,
50 std::memory_order_relaxed)) {
51 // Someone else advanced it or we're not next in line, that's fine
52 break;
53 }
54
55 // Successfully advanced, check if next slot is also ready
56 expected = next;
57 next = (next + 1) % ESPHOME_TASK_LOG_BUFFER_SIZE;
58 if (!this->slots_[expected].ready.load(std::memory_order_acquire)) {
59 break;
60 }
61 }
62}
63
64bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
65 const char *format, va_list args) {
66 // Acquire a slot
67 int slot_index = this->acquire_write_slot_();
68 if (slot_index < 0) {
69 return false; // Buffer full
70 }
71
72 LogMessage &msg = this->slots_[slot_index];
73
74 // Copied now; the thread may exit before processing
75 fill_task_log_header(&msg, level, tag, line, thread_name);
76
77 // Format the message text
78 int ret = vsnprintf(msg.text, sizeof(msg.text), format, args);
79 if (ret < 0) {
80 // Formatting error - still commit the slot but with empty text
81 msg.text[0] = '\0';
82 msg.text_length = 0;
83 } else {
84 msg.text_length = static_cast<uint16_t>(std::min(static_cast<size_t>(ret), sizeof(msg.text) - 1));
85 }
86
87 msg.text_length = trim_trailing_newlines(msg.text, msg.text_length);
88 msg.text[msg.text_length] = '\0';
89
90 // Commit the slot
91 this->commit_write_slot_(slot_index);
92
93 return true;
94}
95
96bool TaskLogBuffer::borrow_message_main_loop(LogMessage *&message, uint16_t &text_length) {
97 size_t current_read = this->read_index_.load(std::memory_order_relaxed);
98 size_t current_write = this->write_index_.load(std::memory_order_acquire);
99
100 // Check if buffer is empty
101 if (current_read == current_write) {
102 return false;
103 }
104
105 // Check if the slot is ready (should always be true if write_index advanced)
106 LogMessage &msg = this->slots_[current_read];
107 if (!msg.ready.load(std::memory_order_acquire)) {
108 return false;
109 }
110
111 message = &msg;
112 text_length = msg.text_length;
113 return true;
114}
115
117 size_t current_read = this->read_index_.load(std::memory_order_relaxed);
118
119 // Clear the ready flag
120 this->slots_[current_read].ready.store(false, std::memory_order_release);
121
122 // Advance read index
123 size_t next_read = (current_read + 1) % ESPHOME_TASK_LOG_BUFFER_SIZE;
124 this->read_index_.store(next_read, std::memory_order_release);
125}
126
127} // namespace esphome::logger
128
129#endif // USE_ESPHOME_TASK_LOG_BUFFER
130#endif // USE_HOST
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)
const LogString * message
Definition component.cpp:35
NameCopy * next
int ret
void fill_task_log_header(Message *msg, uint8_t level, const char *tag, uint16_t line, const char *thread_name)
uint16_t trim_trailing_newlines(const char *text, uint16_t length)
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