ESPHome 2026.11.0b6
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
43class TaskLogBuffer {
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
64
65 // NOT thread-safe - borrow a message from the buffer, only call from main loop
66 bool borrow_message_main_loop(LogMessage *&message, uint16_t &text_length);
67
68 // NOT thread-safe - release a message buffer, only call from main loop
70
71 // Thread-safe - send a message to the buffer from any thread
72 bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
73 const char *format, va_list args);
74
75 // Fast check using volatile counter - no lock needed
76 // Worst case: miss a message for one loop iteration (~8ms at 7000 loops/min)
77 inline bool HOT has_messages() const { return this->message_count_ != 0; }
78
79 // Get the total buffer size in bytes
80 static constexpr size_t size() { return CAPACITY; }
81
82 private:
83 // Keep headers aligned: BK72xx silently misreads unaligned words instead of faulting
84 static constexpr size_t ALIGNMENT = alignof(LogMessage);
85 static constexpr size_t CAPACITY = ESPHOME_TASK_LOG_BUFFER_SIZE & ~(ALIGNMENT - 1);
86
87 // Header + text + null, rounded up to keep the next header aligned
88 static constexpr size_t message_total_size(size_t text_length) {
89 return (sizeof(LogMessage) + text_length + 1 + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
90 }
91
92 // Calculate available contiguous space at write position
93 size_t available_contiguous_space_() const;
94
95 alignas(LogMessage) uint8_t storage_[CAPACITY]; // Embedded in Logger (no separate heap allocation)
96 size_t head_{0}; // Write position
97 size_t tail_{0}; // Read position
98
99 SemaphoreHandle_t mutex_{nullptr}; // FreeRTOS mutex for thread safety
100 volatile uint16_t message_count_{0}; // Fast check counter (dirty read OK)
101 size_t current_message_size_{0}; // Size of currently borrowed message
102};
103
104} // namespace esphome::logger
105
106#endif // USE_ESPHOME_TASK_LOG_BUFFER
107#endif // USE_LIBRETINY
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 uint8_t PADDING_MARKER_LEVEL
const LogString * message
Definition component.cpp:35
const char * format