ESPHome 2026.1.4
Loading...
Searching...
No Matches
task_log_buffer_libretiny.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_LIBRETINY
4
7
8#ifdef USE_ESPHOME_TASK_LOG_BUFFER
9#include <cstdarg>
10#include <cstddef>
11#include <cstring>
12#include <FreeRTOS.h>
13#include <semphr.h>
14#include <task.h>
15
16namespace esphome::logger {
17
44 public:
45 // Structure for a log message header (text data follows immediately after)
46 struct LogMessage {
47 const char *tag; // We store the pointer, assuming tags are static
48 char thread_name[16]; // Store thread name directly (only used for non-main threads)
49 uint16_t text_length; // Length of the message text (up to ~64KB)
50 uint16_t line; // Source code line number
51 uint8_t level; // Log level (0-7)
52
53 // Methods for accessing message contents
54 inline char *text_data() { return reinterpret_cast<char *>(this) + sizeof(LogMessage); }
55 inline const char *text_data() const { return reinterpret_cast<const char *>(this) + sizeof(LogMessage); }
56 };
57
58 // Padding marker level to indicate wrap-around point (stored in LogMessage.level field)
59 // Valid log levels are 0-7, so 0xFF cannot be a real message
60 static constexpr uint8_t PADDING_MARKER_LEVEL = 0xFF;
61
62 // Constructor that takes a total buffer size
63 explicit TaskLogBufferLibreTiny(size_t total_buffer_size);
65
66 // NOT thread-safe - borrow a message from the buffer, only call from main loop
67 bool borrow_message_main_loop(LogMessage **message, const char **text);
68
69 // NOT thread-safe - release a message buffer, only call from main loop
71
72 // Thread-safe - send a message to the buffer from any thread
73 bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, TaskHandle_t task_handle,
74 const char *format, va_list args);
75
76 // Fast check using volatile counter - no lock needed
77 // Worst case: miss a message for one loop iteration (~8ms at 7000 loops/min)
78 inline bool HOT has_messages() const { return this->message_count_ != 0; }
79
80 // Get the total buffer size in bytes
81 inline size_t size() const { return this->size_; }
82
83 private:
84 // Calculate total size needed for a message (header + text + null terminator)
85 static inline size_t message_total_size(size_t text_length) { return sizeof(LogMessage) + text_length + 1; }
86
87 // Calculate available contiguous space at write position
88 size_t available_contiguous_space() const;
89
90 uint8_t *storage_{nullptr}; // Pointer to allocated memory
91 size_t size_{0}; // Size of allocated memory
92 size_t head_{0}; // Write position
93 size_t tail_{0}; // Read position
94
95 SemaphoreHandle_t mutex_{nullptr}; // FreeRTOS mutex for thread safety
96 volatile uint16_t message_count_{0}; // Fast check counter (dirty read OK)
97 size_t current_message_size_{0}; // Size of currently borrowed message
98};
99
100} // namespace esphome::logger
101
102#endif // USE_ESPHOME_TASK_LOG_BUFFER
103#endif // USE_LIBRETINY
Task log buffer for LibreTiny platform using mutex-protected circular buffer.
bool borrow_message_main_loop(LogMessage **message, const char **text)
bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, TaskHandle_t task_handle, const char *format, va_list args)
const char * message
Definition component.cpp:38