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