ESPHome 2026.12.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
16#include "task_log_message.h"
17
18namespace esphome::logger {
19
45class TaskLogBuffer {
46 public:
47 using LogMessage = TaskLogMessage;
48
49 // Padding marker level to indicate wrap-around point (stored in LogMessage.level field)
50 // Valid log levels are 0-7, so 0xFF cannot be a real message
51 static constexpr uint8_t PADDING_MARKER_LEVEL = 0xFF;
52
54 // No destructor: Logger is never destroyed
55
56 // NOT thread-safe - borrow a message from the buffer, only call from main loop
57 bool borrow_message_main_loop(LogMessage *&message, uint16_t &text_length);
58
59 // NOT thread-safe - release a message buffer, only call from main loop
61
62 // Thread-safe - send a message to the buffer from any thread
63 bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
64 const char *format, va_list args);
65
66 // Fast check using volatile counter - no lock needed
67 // Worst case: miss a message for one loop iteration (~8ms at 7000 loops/min)
68 inline bool HOT has_messages() const { return this->message_count_ != 0; }
69
70 // Get the total buffer size in bytes
71 static constexpr size_t size() { return CAPACITY; }
72
73 private:
74 // Keep headers aligned: BK72xx silently misreads unaligned words instead of faulting
75 static constexpr size_t ALIGNMENT = alignof(LogMessage);
76 static constexpr size_t CAPACITY = ESPHOME_TASK_LOG_BUFFER_SIZE & ~(ALIGNMENT - 1);
77
78 // Header + text + null, rounded up to keep the next header aligned
79 static constexpr size_t message_total_size(size_t text_length) {
80 return (sizeof(LogMessage) + text_length + 1 + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
81 }
82
83 // Calculate available contiguous space at write position
84 size_t available_contiguous_space_() const;
85
86 alignas(LogMessage) uint8_t storage_[CAPACITY]; // Embedded in Logger (no separate heap allocation)
87 size_t head_{0}; // Write position
88 size_t tail_{0}; // Read position
89
90 SemaphoreHandle_t mutex_{nullptr}; // FreeRTOS mutex for thread safety
91 volatile uint16_t message_count_{0}; // Fast check counter (dirty read OK)
92 size_t current_message_size_{0}; // Size of currently borrowed message
93};
94
95} // namespace esphome::logger
96
97#endif // USE_ESPHOME_TASK_LOG_BUFFER
98#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
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