ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
task_log_buffer_zephyr.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ZEPHYR
4
7#include <zephyr/sys/mpsc_pbuf.h>
8
9#include "task_log_message.h"
10
11namespace esphome::logger {
12
13// "0x" + 2 hex digits per byte + '\0'
14static constexpr size_t MAX_POINTER_REPRESENTATION = 2 + sizeof(void *) * 2 + 1;
15
16extern __thread bool non_main_task_recursion_guard; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
17
18#ifdef USE_ESPHOME_TASK_LOG_BUFFER
19
21 public:
22 // Structure for a log message header (text data follows immediately after)
23 struct LogMessage {
24 MPSC_PBUF_HDR; // this is only 2 bits but no more than 30 bits directly after
25 uint16_t line; // Source code line number
26 uint8_t level; // Log level (0-7)
27#if defined(CONFIG_THREAD_NAME)
28 char thread_name[CONFIG_THREAD_MAX_NAME_LEN]; // Store thread name directly (only used for non-main threads)
29#else
30 char thread_name[MAX_POINTER_REPRESENTATION]; // Store thread name directly (only used for non-main threads)
31#endif
32 const char *tag; // We store the pointer, assuming tags are static
33 uint16_t text_length; // Length of the message text (up to ~64KB)
34
35 // Methods for accessing message contents
36 inline char *text_data() { return reinterpret_cast<char *>(this) + sizeof(LogMessage); }
37 };
39
40 // Check if there are messages ready to be processed using an atomic counter for performance
41 inline bool HOT has_messages() { return mpsc_pbuf_is_pending(&this->log_buffer_); }
42
43 // Get the total buffer size in bytes
44 static constexpr size_t size() { return BUF_WORD_COUNT * sizeof(uint32_t); }
45
46 // NOT thread-safe - borrow a message from the ring buffer, only call from main loop
47 bool borrow_message_main_loop(LogMessage *&message, uint16_t &text_length);
48
49 // NOT thread-safe - release a message buffer and update the counter, only call from main loop
51
52 // Thread-safe - send a message to the ring buffer from any thread
53 bool send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
54 const char *format, va_list args);
55
56 protected:
57 // Round up byte size to 32-bit word count for mpsc_pbuf alignment requirement
58 static constexpr size_t BUF_WORD_COUNT = (ESPHOME_TASK_LOG_BUFFER_SIZE + 3) / sizeof(uint32_t);
59 uint32_t buf_storage_[BUF_WORD_COUNT]; // Embedded in Logger (no separate heap allocation)
60 mpsc_pbuf_buffer_config mpsc_config_{};
61 mpsc_pbuf_buffer log_buffer_{};
62 const mpsc_pbuf_generic *current_token_{};
63};
64
65#endif // USE_ESPHOME_TASK_LOG_BUFFER
66
67} // namespace esphome::logger
68
69#endif // USE_ZEPHYR
Task log buffer for ESP32 platform using FreeRTOS ring buffer.
static constexpr size_t BUF_WORD_COUNT
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)
uint32_t buf_storage_[BUF_WORD_COUNT]
const LogString * message
Definition component.cpp:35
__thread bool non_main_task_recursion_guard
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
static void uint32_t