ESPHome 2026.4.0-dev
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 ESPHOME_TASK_LOG_BUFFER_SIZE; }
81
82 private:
83 // Calculate total size needed for a message (header + text + null terminator)
84 static inline size_t message_total_size(size_t text_length) { return sizeof(LogMessage) + text_length + 1; }
85
86 // Calculate available contiguous space at write position
87 size_t available_contiguous_space() const;
88
89 uint8_t storage_[ESPHOME_TASK_LOG_BUFFER_SIZE]; // Embedded in Logger (no separate heap allocation)
90 size_t head_{0}; // Write position
91 size_t tail_{0}; // Read position
92
93 SemaphoreHandle_t mutex_{nullptr}; // FreeRTOS mutex for thread safety
94 volatile uint16_t message_count_{0}; // Fast check counter (dirty read OK)
95 size_t current_message_size_{0}; // Size of currently borrowed message
96};
97
98} // namespace esphome::logger
99
100#endif // USE_ESPHOME_TASK_LOG_BUFFER
101#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 char * message
Definition component.cpp:35
const char int line
Definition log.h:74
const char * tag
Definition log.h:74
const char int const __FlashStringHelper * format
Definition log.h:74
const char int const __FlashStringHelper va_list args
Definition log.h:74