ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
task_log_buffer_host.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_HOST
4
7
8#ifdef USE_ESPHOME_TASK_LOG_BUFFER
9
10#include <atomic>
11#include <cstdarg>
12#include <cstddef>
13#include <cstring>
14#include <pthread.h>
15
16#include "task_log_message.h"
17
18namespace esphome::logger {
19
52class TaskLogBuffer {
53 public:
54 // Structure for a log message (fixed size for lock-free operation)
55 struct LogMessage {
56 // Size constants
57 static constexpr size_t MAX_THREAD_NAME_SIZE = 32;
58 static constexpr size_t MAX_TEXT_SIZE = 512;
59
60 const char *tag; // Pointer to static tag string
61 char thread_name[MAX_THREAD_NAME_SIZE]; // Thread name (copied)
62 char text[MAX_TEXT_SIZE + 1]; // Message text with null terminator
63 uint16_t text_length; // Actual length of text
64 uint16_t line; // Source line number
65 uint8_t level; // Log level
66 std::atomic<bool> ready; // Message is ready to be consumed
67
68 LogMessage() : tag(nullptr), text_length(0), line(0), level(0), ready(false) {
69 thread_name[0] = '\0';
70 text[0] = '\0';
71 }
72 inline char *text_data() { return this->text; }
73 };
74
75 TaskLogBuffer() = default;
76
77 // NOT thread-safe - get next message from buffer, only call from main loop
78 // Returns true if a message was retrieved, false if buffer is empty
79 bool borrow_message_main_loop(LogMessage *&message, uint16_t &text_length);
80
81 // NOT thread-safe - release the message after processing, only call from main loop
83
84 // Thread-safe - send a message to the buffer from any thread
85 // Returns true if message was queued, false if buffer is full
86 bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
87 const char *format, va_list args);
88
89 // Check if there are messages ready to be processed
90 inline bool HOT has_messages() const {
91 return read_index_.load(std::memory_order_acquire) != write_index_.load(std::memory_order_acquire);
92 }
93
94 // Get the buffer size (number of slots)
95 static constexpr size_t size() { return ESPHOME_TASK_LOG_BUFFER_SIZE; }
96
97 private:
98 // Acquire a slot for writing (thread-safe)
99 // Returns slot index or -1 if buffer is full
100 int acquire_write_slot_();
101
102 // Commit a slot after writing (thread-safe)
103 void commit_write_slot_(int slot_index);
104
105 LogMessage slots_[ESPHOME_TASK_LOG_BUFFER_SIZE]; // Embedded in Logger (no separate heap allocation)
106
107 // Lock-free indices using atomics
108 // - reserve_index_: Next slot to reserve (producers CAS this to claim slots)
109 // - write_index_: Boundary of committed/ready slots (consumer reads up to this)
110 // - read_index_: Next slot to read (only consumer modifies this)
111 std::atomic<size_t> reserve_index_{0}; // Next slot to reserve for writing
112 std::atomic<size_t> write_index_{0}; // Last committed slot boundary
113 std::atomic<size_t> read_index_{0}; // Next slot to read from
114};
115
116} // namespace esphome::logger
117
118#endif // USE_ESPHOME_TASK_LOG_BUFFER
119#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)
static constexpr size_t size()
const LogString * message
Definition component.cpp:35
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