ESPHome 2026.3.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 };
36 // Constructor that takes a total buffer size
37 explicit TaskLogBuffer(size_t total_buffer_size);
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 inline size_t size() const { return this->mpsc_config_.size * 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 mpsc_pbuf_buffer_config mpsc_config_{};
58 mpsc_pbuf_buffer log_buffer_{};
59 const mpsc_pbuf_generic *current_token_{};
60};
61
62#endif // USE_ESPHOME_TASK_LOG_BUFFER
63
64} // namespace esphome::logger
65
66#endif // USE_ZEPHYR
Task log buffer for ESP32 platform using FreeRTOS ring buffer.
TaskLogBuffer(size_t total_buffer_size)
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)
const char * message
Definition component.cpp:38
__thread bool non_main_task_recursion_guard_