ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdarg>
4#include <map>
5#include <span>
6#include <type_traits>
7#if defined(USE_ESP32) || defined(USE_HOST)
8#include <pthread.h>
9#endif
14#include "esphome/core/log.h"
16
17#include "log_buffer.h"
22
23#ifdef USE_ARDUINO
24#if defined(USE_ESP8266)
25#include <HardwareSerial.h>
26#endif // USE_ESP8266
27#ifdef USE_RP2
28#include <HardwareSerial.h>
29#include <SerialUSB.h>
30#endif // USE_RP2
31#endif // USE_ARDUINO
32
33#ifdef USE_ESP32
34#include <driver/uart.h>
35#endif // USE_ESP32
36
37#ifdef USE_ZEPHYR
38#include <zephyr/kernel.h>
39struct device;
40#endif
41
42namespace esphome::logger {
43
58 void *instance;
59 void (*fn)(void *, uint8_t, const char *, const char *, size_t);
60 void invoke(uint8_t level, const char *tag, const char *message, size_t message_len) const {
61 this->fn(this->instance, level, tag, message, message_len);
62 }
63};
64
65#ifdef USE_LOGGER_LEVEL_LISTENERS
84 public:
85 virtual void on_log_level_change(uint8_t level) = 0;
86};
87#endif
88
89// RAM map keys compared with a tag that may be in PROGMEM, without copying it
90struct FlashTag {
91 const char *tag;
92};
94 using is_transparent = void;
95 bool operator()(const char *a, const char *b) const { return strcmp(a, b) < 0; }
96 bool operator()(const char *key, FlashTag t) const { return ESPHOME_strcmp_P(key, t.tag) < 0; }
97 bool operator()(FlashTag t, const char *key) const { return ESPHOME_strcmp_P(key, t.tag) > 0; }
98};
99
100// Stack buffer size for retrieving thread/task names from the OS
101// macOS allows up to 64 bytes, Linux up to 16
102static constexpr size_t THREAD_NAME_BUF_SIZE = 64;
103
104#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
109enum UARTSelection : uint8_t {
110#ifdef USE_LIBRETINY
113#else
115#endif
117#if defined(USE_LIBRETINY) || defined(USE_ESP32_VARIANT_ESP32)
119#endif
120#ifdef USE_LOGGER_USB_CDC
122#endif
123#ifdef USE_LOGGER_USB_SERIAL_JTAG
125#endif
126#ifdef USE_ESP8266
128#endif // USE_ESP8266
129};
130#endif // USE_ESP32 || USE_ESP8266 || USE_RP2 || USE_LIBRETINY || USE_ZEPHYR
131
149class Logger final : public Component {
150 public:
151 explicit Logger(uint32_t baud_rate);
152#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || (defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
153 void loop() override;
154#endif
155#if defined(USE_RP2_CRASH_HANDLER) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
156 void setup() override;
157#endif
159 void set_baud_rate(uint32_t baud_rate) { this->baud_rate_ = baud_rate; }
161#if defined(USE_ARDUINO) && !defined(USE_ESP32)
162 Stream *get_hw_serial() const { return hw_serial_; }
163#endif
164#ifdef USE_ESP32
165 uart_port_t get_uart_num() const { return uart_num_; }
166 void create_pthread_key() { pthread_key_create(&log_recursion_key_, nullptr); }
167#endif
168#ifdef USE_HOST
169 void create_pthread_key() { pthread_key_create(&log_recursion_key_, nullptr); }
170#endif
171#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
172 void set_uart_selection(UARTSelection uart_selection) { uart_ = uart_selection; }
174 UARTSelection get_uart() const { return this->uart_; }
175#endif
176
178 void set_log_level(uint8_t level);
179#ifdef USE_LOGGER_RUNTIME_TAG_LEVELS
181 void set_log_level(const char *tag, uint8_t log_level);
182#endif
183#ifdef USE_LOGGER_BOOT_LOG_PER_TAG_LEVELS
185 void set_boot_log_level(const char *tag, uint8_t log_level);
186#endif
187 uint8_t get_log_level() { return this->current_level_; }
189 uint8_t get_log_level(const char *tag);
190
192 void replay_boot_logs();
193
194 // ========== INTERNAL METHODS ==========
195 // (In most use cases you won't need these)
197 void pre_setup();
198 void dump_config() override;
199
200 inline uint8_t level_for(const char *tag);
201#ifdef USE_LOG_LISTENERS
203 void add_log_callback(void *instance, void (*fn)(void *, uint8_t, const char *, const char *, size_t)) {
204 this->log_callbacks_.push_back(LogCallback{instance, fn});
205 }
206#else
208 void add_log_callback(void *instance, void (*fn)(void *, uint8_t, const char *, const char *, size_t)) {}
209#endif
210
211#ifdef USE_LOGGER_LEVEL_LISTENERS
213 void add_level_listener(LoggerLevelListener *listener) { this->level_listeners_.push_back(listener); }
214#endif
215
216 float get_setup_priority() const override { return setup_priority::BUS + 500.0f; }
217
218 void log_vprintf_(uint8_t level, const char *tag, int line, const char *format, va_list args); // NOLINT
219#ifdef USE_ESP8266
220 // NOLINTNEXTLINE(readability-identifier-naming)
221 void log_vprintf_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format, va_list args);
222#endif
223
224 protected:
225 // RAII guard for recursion flags - sets flag on construction, clears on destruction
227 public:
228 explicit RecursionGuard(bool &flag) : flag_(flag) { flag_ = true; }
229 ~RecursionGuard() { flag_ = false; }
234
235 private:
236 bool &flag_;
237 };
238
239#if defined(USE_ESP32) || defined(USE_HOST) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
240 // Handles non-main thread logging only (~0.1% of calls)
241 // thread_name is resolved by the caller from the task handle, avoiding redundant lookups
242 void log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args,
243 const char *thread_name);
244#endif
245#if defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
246 void cdc_loop_();
247#endif
248 void process_messages_();
249#if defined(USE_HOST) || defined(USE_ZEPHYR)
250 void write_msg_(const char *msg, uint16_t len);
251#else
252 inline void write_msg_(const char *msg, uint16_t len); // Defined in platform-specific logger_*.h
253#endif
254
255 // Format a log message with printf-style arguments and write it to a buffer with header, footer, and null terminator
256 // thread_name: name of the calling thread/task, or nullptr for main task (callers already know which task they're on)
257 inline void HOT format_log_to_buffer_with_terminator_(uint8_t level, const char *tag, int line, const char *format,
258 va_list args, LogBuffer &buf, const char *thread_name) {
259 buf.write_header(level, tag, line, thread_name);
260 buf.format_body(format, args);
261 }
262
263#ifdef USE_ESP8266
264 // Format a log message with flash string format and write it to a buffer with header, footer, and null terminator
265 // ESP8266-only (single-task), thread_name is always nullptr
266 inline void HOT format_log_to_buffer_with_terminator_p_(uint8_t level, const char *tag, int line,
267 const __FlashStringHelper *format, va_list args,
268 LogBuffer &buf) {
269 buf.write_header(level, tag, line, nullptr);
270 buf.format_body_p(reinterpret_cast<PGM_P>(format), args);
271 }
272#endif
273
274 // Helper to notify log callbacks
275 inline void HOT notify_listeners_(uint8_t level, const char *tag, const LogBuffer &buf) {
276#ifdef USE_LOG_LISTENERS
277 for (auto &cb : this->log_callbacks_)
278 cb.invoke(level, tag, buf.data, buf.pos);
279#endif
280 }
281
282 // Helper to write log buffer to console (replaces null terminator with newline and writes)
283 inline void HOT write_to_console_(LogBuffer &buf) {
285 this->write_msg_(buf.data, buf.pos);
286 }
287
288 // Helper to write log buffer to console if logging is enabled
290 if (this->baud_rate_ > 0)
291 this->write_to_console_(buf);
292 }
293
294 // Helper to format and send a log message to both console and listeners
295 // Template handles both const char* (RAM) and __FlashStringHelper* (flash) format strings
296 // thread_name: name of the calling thread/task, or nullptr for main task
297 template<typename FormatType>
298 inline void HOT log_message_to_buffer_and_send_(bool &recursion_guard, uint8_t level, const char *tag, int line,
299 FormatType format, va_list args, const char *thread_name) {
300 RecursionGuard guard(recursion_guard);
301 LogBuffer buf{this->tx_buffer_, ESPHOME_LOGGER_TX_BUFFER_SIZE};
302#ifdef USE_ESP8266
303 if constexpr (std::is_same_v<FormatType, const __FlashStringHelper *>) {
304 this->format_log_to_buffer_with_terminator_p_(level, tag, line, format, args, buf);
305 } else
306#endif
307 {
308 this->format_log_to_buffer_with_terminator_(level, tag, line, format, args, buf, thread_name);
309 }
310#ifdef USE_LOGGER_BOOT_LOG_BUFFER
311 if (!this->boot_log_done_)
312 this->boot_log_capture_(level, tag, buf);
313#endif
314 this->notify_listeners_(level, tag, buf);
316 }
317
318#ifdef USE_ESPHOME_TASK_LOG_BUFFER
319 // Helper to format a pre-formatted message from the task log buffer and notify listeners
320 // Used by process_messages_ to avoid code duplication between ESP32 and host platforms
321 inline void HOT format_buffered_message_and_notify_(uint8_t level, const char *tag, uint16_t line,
322 const char *thread_name, const char *text, uint16_t text_length,
323 LogBuffer &buf) {
324 buf.write_header(level, tag, line, thread_name);
325 buf.write_body(text, text_length);
326#ifdef USE_LOGGER_BOOT_LOG_BUFFER
327 if (!this->boot_log_done_)
328 this->boot_log_capture_(level, tag, buf);
329#endif
330 this->notify_listeners_(level, tag, buf);
331 }
332#endif
333
334#ifndef USE_HOST
335 const LogString *get_uart_selection_();
336#endif
337
338#ifdef USE_LOGGER_BOOT_LOG_BUFFER
340 void boot_log_capture_(uint8_t level, const char *tag, const LogBuffer &buf);
341 inline uint8_t boot_level_for_(const char *tag);
342#endif
343
344 // Group 4-byte aligned members first
346#if defined(USE_ARDUINO) && !defined(USE_ESP32)
347 Stream *hw_serial_{nullptr};
348#endif
349#if defined(USE_ZEPHYR)
350 void dump_crash_();
351#ifdef USE_BOOTLOADER_ADAFRUIT
352 void dump_bootloader_();
353#endif
354 const device *uart_dev_{nullptr};
355#endif
356#if defined(USE_ESP32) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
357 void *main_task_{nullptr}; // Main thread/task for fast path comparison
358#endif
359#ifdef USE_HOST
360 pthread_t main_thread_{}; // Main thread for pthread_equal() comparison
361#endif
362#ifdef USE_ESP32
363 // Task-specific recursion guards:
364 // - Main task uses a dedicated member variable for efficiency
365 // - Other tasks use pthread TLS with a dynamically created key via pthread_key_create
366 pthread_key_t log_recursion_key_; // 4 bytes
367 uart_port_t uart_num_; // 4 bytes (enum defaults to int size)
368#endif
369#ifdef USE_HOST
370 // Thread-specific recursion guards using pthread TLS
371 pthread_key_t log_recursion_key_;
372#endif
373
374 // Large objects (internally aligned)
375#ifdef USE_LOGGER_BOOT_LOG_PER_TAG_LEVELS
376 std::map<const char *, uint8_t, CStrCompare> boot_log_levels_{};
377#endif
378#ifdef USE_LOGGER_RUNTIME_TAG_LEVELS
379 std::map<const char *, uint8_t, CStrCompare> log_levels_{};
380#endif
381#ifdef USE_LOG_LISTENERS
383 log_callbacks_; // Log message callbacks (API, MQTT, syslog, etc.)
384#endif
385#ifdef USE_LOGGER_LEVEL_LISTENERS
386 std::vector<LoggerLevelListener *> level_listeners_; // Log level change listeners
387#endif
388 // Group smaller types together at the end
389#ifdef USE_LOGGER_BOOT_LOG_BUFFER
390 uint16_t boot_log_pos_{0}; // Bytes used in boot_log_buffer_
391 uint16_t boot_log_dropped_{0}; // Lines that did not fit
392 bool boot_log_done_{false}; // Set on first replay; stops further capture
393#endif
394 uint8_t current_level_{ESPHOME_LOG_LEVEL_VERY_VERBOSE};
395#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_ZEPHYR)
396 UARTSelection uart_{UART_SELECTION_UART0}; // Must match cpp_default_uart in __init__.py
397#endif
398#ifdef USE_LIBRETINY
399 UARTSelection uart_{UART_SELECTION_DEFAULT}; // Must match cpp_default_uart in __init__.py
400#endif
401#if defined(USE_ESP32) || defined(USE_HOST) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
403#ifdef USE_LIBRETINY
404 bool non_main_task_recursion_guard_{false}; // Shared guard for all non-main tasks on LibreTiny
405#endif
406 // The guard that protects tx_buffer_ on the main loop, for callers that run on any platform
408#else
409 bool global_recursion_guard_{false}; // Simple global recursion guard for single-task platforms
411#endif
412
413 // Large buffers placed last to keep frequently-accessed member offsets small
414 char tx_buffer_[ESPHOME_LOGGER_TX_BUFFER_SIZE + 1]; // +1 for null terminator
415#ifdef USE_ESPHOME_TASK_LOG_BUFFER
416 logger::TaskLogBuffer log_buffer_; // Embedded in Logger (no separate heap allocation)
417#endif
418#ifdef USE_LOGGER_BOOT_LOG_BUFFER
419 char boot_log_buffer_[ESPHOME_LOGGER_BOOT_LOG_BUFFER_SIZE]; // BootLogRecord headers each followed by their text
420#endif
421
422 // --- get_thread_name_ overloads (per-platform) ---
423
424#if defined(USE_ESP32) || defined(USE_LIBRETINY)
425 // Primary overload - takes a task handle directly to avoid redundant xTaskGetCurrentTaskHandle() calls
426 // when the caller already has the handle (e.g. from the main task check in log_vprintf_)
427 const char *get_thread_name_(TaskHandle_t task) {
428 if (task == this->main_task_) {
429 return nullptr; // Main task
430 }
431#if defined(USE_ESP32)
432 return pcTaskGetName(task);
433#elif defined(USE_LIBRETINY)
434 return pcTaskGetTaskName(task);
435#endif
436 }
437
438 // Convenience overload - gets the current task handle and delegates
439 const char *HOT get_thread_name_() { return this->get_thread_name_(xTaskGetCurrentTaskHandle()); }
440
441#elif defined(USE_HOST)
442 // Takes a caller-provided buffer for the thread name (stack-allocated for thread safety)
443 const char *HOT get_thread_name_(std::span<char> buff) {
444 pthread_t current_thread = pthread_self();
445 if (pthread_equal(current_thread, main_thread_)) {
446 return nullptr; // Main thread
447 }
448 // For non-main threads, get the thread name into the caller-provided buffer
449 if (pthread_getname_np(current_thread, buff.data(), buff.size()) == 0) {
450 return buff.data();
451 }
452 return nullptr;
453 }
454
455#elif defined(USE_ZEPHYR)
456 const char *HOT get_thread_name_(std::span<char> buff, k_tid_t current_task = nullptr) {
457 if (current_task == nullptr) {
458 current_task = k_current_get();
459 }
460 if (current_task == main_task_) {
461 return nullptr; // Main task
462 }
463 const char *name = k_thread_name_get(current_task);
464 if (name) {
465 // zephyr print task names only if debug component is present
466 return name;
467 }
468 std::snprintf(buff.data(), buff.size(), "%p", current_task);
469 return buff.data();
470 }
471#endif
472
473 // --- Non-main task recursion guards (per-platform) ---
474
475#if defined(USE_ESP32) || defined(USE_HOST)
476 // RAII guard for non-main task recursion using pthread TLS
478 public:
479 explicit NonMainTaskRecursionGuard(pthread_key_t key) : key_(key) {
480 pthread_setspecific(key_, reinterpret_cast<void *>(1));
481 }
482 ~NonMainTaskRecursionGuard() { pthread_setspecific(key_, nullptr); }
487
488 private:
489 pthread_key_t key_;
490 };
491
492 // Check if non-main task is already in recursion (via TLS)
493 inline bool HOT is_non_main_task_recursive_() const { return pthread_getspecific(log_recursion_key_) != nullptr; }
494
495 // Create RAII guard for non-main task recursion
497
498#elif defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
499 // LibreTiny doesn't have FreeRTOS TLS, so use a simple approach:
500 // - Main task uses dedicated boolean (same as ESP32)
501 // - Non-main tasks share a single recursion guard
502 // This is safe because:
503 // - Recursion from logging within logging is the main concern
504 // - Cross-task "recursion" is prevented by the buffer mutex anyway
505 // - Missing a recursive call from another task is acceptable (falls back to direct output)
506 //
507 // Zephyr use __thread as TLS
508
509 // Check if non-main task is already in recursion
511
512 // Create RAII guard for non-main task recursion (uses shared boolean for all non-main tasks)
514#endif
515
516#if defined(USE_ESPHOME_TASK_LOG_BUFFER) && !(defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
517 // Disable loop when task buffer is empty
518 // Zephyr with USB CDC needs loop active to poll port readiness via cdc_loop_()
520 // Thread safety note: This is safe even if another task calls enable_loop_soon_any_context()
521 // concurrently. If that happens between our check and disable_loop(), the enable request
522 // will be processed on the next main loop iteration since:
523 // - disable_loop() takes effect immediately
524 // - enable_loop_soon_any_context() sets a pending flag that's checked at loop start
525 this->disable_loop();
526 }
527#endif
528};
529extern Logger *global_logger; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
530
531class LoggerMessageTrigger final : public Trigger<uint8_t, const char *, const char *> {
532 public:
533 explicit LoggerMessageTrigger(Logger *parent, uint8_t level) : level_(level) {
534 parent->add_log_callback(this,
535 [](void *self, uint8_t level, const char *tag, const char *message, size_t message_len) {
536 auto *trigger = static_cast<LoggerMessageTrigger *>(self);
537 if (level <= trigger->level_) {
538#ifdef USE_ESP8266
539 // User lambdas may strcmp the tag, which may be in PROGMEM. The copy lives in
540 // the trigger so an automation that suspends (delay) still sees a valid tag.
541 ESPHOME_strncpy_P(trigger->ram_tag_, tag, MAX_TAG_LENGTH);
542 tag = trigger->ram_tag_;
543#endif
544 trigger->trigger(level, tag, message);
545 }
546 });
547 }
548
549 protected:
550 uint8_t level_;
551#ifdef USE_ESP8266
552 char ram_tag_[MAX_TAG_LENGTH + 1]{};
553#endif
554};
555
556#ifdef USE_CRASH_HANDLER
558bool crash_log_pending();
562#endif
563
564} // namespace esphome::logger
565
566// Platform-specific inline implementations of write_msg_()
567// Must be included after the Logger class definition is complete
568#if defined(USE_ESP32)
569#include "logger_esp32.h"
570#elif defined(USE_ESP8266)
571#include "logger_esp8266.h"
572#elif defined(USE_RP2)
573#include "logger_rp2.h"
574#elif defined(USE_LIBRETINY)
575#include "logger_libretiny.h"
576#endif
void disable_loop()
Disable this component's loop.
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:304
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:557
NonMainTaskRecursionGuard & operator=(const NonMainTaskRecursionGuard &)=delete
NonMainTaskRecursionGuard & operator=(NonMainTaskRecursionGuard &&)=delete
NonMainTaskRecursionGuard(const NonMainTaskRecursionGuard &)=delete
NonMainTaskRecursionGuard(NonMainTaskRecursionGuard &&)=delete
RecursionGuard & operator=(const RecursionGuard &)=delete
RecursionGuard(const RecursionGuard &)=delete
RecursionGuard & operator=(RecursionGuard &&)=delete
RecursionGuard(RecursionGuard &&)=delete
Logger component for all ESPHome logging.
Definition logger.h:149
void add_level_listener(LoggerLevelListener *listener)
Register a listener for log level changes.
Definition logger.h:213
void HOT notify_listeners_(uint8_t level, const char *tag, const LogBuffer &buf)
Definition logger.h:275
UARTSelection uart_
Definition logger.h:396
char boot_log_buffer_[ESPHOME_LOGGER_BOOT_LOG_BUFFER_SIZE]
Definition logger.h:419
void HOT format_log_to_buffer_with_terminator_p_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format, va_list args, LogBuffer &buf)
Definition logger.h:266
void write_msg_(const char *msg, uint16_t len)
Definition logger_esp32.h:9
const char *HOT get_thread_name_()
Definition logger.h:439
void dump_config() override
Definition logger.cpp:328
const char * get_thread_name_(TaskHandle_t task)
Definition logger.h:427
bool HOT is_non_main_task_recursive_() const
Definition logger.h:493
uint32_t get_baud_rate() const
Definition logger.h:160
const char *HOT get_thread_name_(std::span< char > buff, k_tid_t current_task=nullptr)
Definition logger.h:456
NonMainTaskRecursionGuard make_non_main_task_guard_()
Definition logger.h:496
const LogString * get_uart_selection_()
uint8_t level_for(const char *tag)
Definition logger.cpp:177
StaticVector< LogCallback, ESPHOME_LOG_MAX_LISTENERS > log_callbacks_
Definition logger.h:383
void loop() override
Definition logger.cpp:206
uint8_t get_log_level()
Definition logger.h:187
Stream * get_hw_serial() const
Definition logger.h:162
void log_vprintf_(uint8_t level, const char *tag, int line, const char *format, va_list args)
Definition logger.cpp:35
void HOT log_message_to_buffer_and_send_(bool &recursion_guard, uint8_t level, const char *tag, int line, FormatType format, va_list args, const char *thread_name)
Definition logger.h:298
void pre_setup()
Set up this component.
void replay_boot_logs()
Send the lines captured during boot to the listeners and stop capturing; safe to call repeatedly.
Definition logger.cpp:284
char tx_buffer_[ESPHOME_LOGGER_TX_BUFFER_SIZE+1]
Definition logger.h:414
void HOT format_buffered_message_and_notify_(uint8_t level, const char *tag, uint16_t line, const char *thread_name, const char *text, uint16_t text_length, LogBuffer &buf)
Definition logger.h:321
void write_msg_(const char *msg, uint16_t len)
float get_setup_priority() const override
Definition logger.h:216
std::map< const char *, uint8_t, CStrCompare > log_levels_
Definition logger.h:379
const char *HOT get_thread_name_(std::span< char > buff)
Definition logger.h:443
UARTSelection get_uart() const
Get the UART used by the logger.
Definition logger.h:174
pthread_t main_thread_
Definition logger.h:360
std::vector< LoggerLevelListener * > level_listeners_
Definition logger.h:386
bool non_main_task_recursion_guard_
Definition logger.h:404
void disable_loop_when_buffer_empty_()
Definition logger.h:519
uart_port_t uart_num_
Definition logger.h:367
void HOT format_log_to_buffer_with_terminator_(uint8_t level, const char *tag, int line, const char *format, va_list args, LogBuffer &buf, const char *thread_name)
Definition logger.h:257
pthread_key_t log_recursion_key_
Definition logger.h:366
RecursionGuard make_non_main_task_guard_()
Definition logger.h:513
void HOT write_log_buffer_to_console_(LogBuffer &buf)
Definition logger.h:289
uart_port_t get_uart_num() const
Definition logger.h:165
uint8_t boot_level_for_(const char *tag)
Definition logger.cpp:186
void set_log_level(uint8_t level)
Set the default log level for this logger.
Definition logger.cpp:383
void set_baud_rate(uint32_t baud_rate)
Manually set the baud rate for serial, set to 0 to disable.
Definition logger.h:159
void set_uart_selection(UARTSelection uart_selection)
Definition logger.h:172
void boot_log_capture_(uint8_t level, const char *tag, const LogBuffer &buf)
Copy a formatted line at or above the boot log level (outer ANSI codes stripped) into the boot log bu...
Definition logger.cpp:256
void setup() override
void add_log_callback(void *instance, void(*fn)(void *, uint8_t, const char *, const char *, size_t))
Register a log callback to receive log messages.
Definition logger.h:203
bool & main_loop_recursion_guard_()
Definition logger.h:407
void set_boot_log_level(const char *tag, uint8_t log_level)
Set the boot log level of the specified tag.
Definition logger.cpp:318
void HOT write_to_console_(LogBuffer &buf)
Definition logger.h:283
void log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args, const char *thread_name)
Definition logger.cpp:91
logger::TaskLogBuffer log_buffer_
Definition logger.h:416
uint16_t boot_log_dropped_
Definition logger.h:391
bool main_task_recursion_guard_
Definition logger.h:402
const device * uart_dev_
Definition logger.h:354
std::map< const char *, uint8_t, CStrCompare > boot_log_levels_
Definition logger.h:376
Logger(uint32_t baud_rate)
Definition logger.cpp:195
Interface for receiving log level changes without std::function overhead.
Definition logger.h:83
virtual void on_log_level_change(uint8_t level)=0
char ram_tag_[MAX_TAG_LENGTH+1]
Definition logger.h:552
LoggerMessageTrigger(Logger *parent, uint8_t level)
Definition logger.h:533
Task log buffer for ESP32 platform using FreeRTOS ring buffer.
const LogString * message
Definition component.cpp:35
const char * key
const char * format
bool crash_log_pending()
Whether a crash record from before this boot is waiting to be reported.
UARTSelection
Enum for logging UART selection.
Definition logger.h:109
@ UART_SELECTION_UART0_SWAP
Definition logger.h:127
@ UART_SELECTION_UART2
Definition logger.h:118
@ UART_SELECTION_USB_SERIAL_JTAG
Definition logger.h:124
@ UART_SELECTION_DEFAULT
Definition logger.h:111
@ UART_SELECTION_USB_CDC
Definition logger.h:121
@ UART_SELECTION_UART0
Definition logger.h:112
@ UART_SELECTION_UART1
Definition logger.h:116
Logger * global_logger
Definition logger.cpp:396
void log_crash_to_client()
Log the previous crash for a client that just subscribed to the logs.
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:39
const void size_t len
Definition hal.h:68
static void uint32_t
bool operator()(const char *a, const char *b) const
Definition logger.h:95
bool operator()(FlashTag t, const char *key) const
Definition logger.h:97
bool operator()(const char *key, FlashTag t) const
Definition logger.h:96
void HOT format_body_p(PGM_P format, va_list args)
Definition log_buffer.h:112
void write_body(const char *text, uint16_t text_length)
Definition log_buffer.h:134
void HOT write_header(uint8_t level, const char *tag, int line, const char *thread_name)
Definition log_buffer.h:54
void HOT format_body(const char *format, va_list args)
Definition log_buffer.h:107
Lightweight callback for receiving log messages without virtual dispatch overhead.
Definition logger.h:57
void invoke(uint8_t level, const char *tag, const char *message, size_t message_len) const
Definition logger.h:60
void(* fn)(void *, uint8_t, const char *, const char *, size_t)
Definition logger.h:59