ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
logger_rp2.cpp
Go to the documentation of this file.
1#ifdef USE_RP2
2#include "logger.h"
4#ifdef USE_RP2_CRASH_HANDLER
6#endif
7#include "esphome/core/log.h"
8
9namespace esphome::logger {
10
11ESPHOME_LOG_TAG(TAG, "logger");
12
13#if defined(USE_RP2_CRASH_HANDLER) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
15 if (this->hw_serial_ == nullptr || !crash_log_pending())
16 return;
17 // Report the crash each time a host opens the USB port (sets DTR)
18 this->set_interval(500, [opened_before = false]() mutable {
19 const bool opened = Serial.dtr();
20 if (opened && !opened_before)
22 opened_before = opened;
23 });
24}
25#endif
26
27void Logger::pre_setup() {
28 if (this->baud_rate_ > 0) {
29 switch (this->uart_) {
31 this->hw_serial_ = &Serial1;
32 Serial1.begin(this->baud_rate_);
33 break;
35 this->hw_serial_ = &Serial2;
36 Serial2.begin(this->baud_rate_);
37 break;
39 this->hw_serial_ = &Serial;
40 Serial.begin(this->baud_rate_);
41 break;
42 }
43 }
44 global_logger = this;
45 ESP_LOGI(TAG, "Log initialized");
46 // Nobody has a USB port open this early; setup() reports the crash once a host opens it
47#if defined(USE_RP2_CRASH_HANDLER) && !defined(USE_LOGGER_UART_SELECTION_USB_CDC)
49#endif
50}
51
52const LogString *Logger::get_uart_selection_() {
53 switch (this->uart_) {
55 return LOG_STR("UART0");
57 return LOG_STR("UART1");
58#ifdef USE_LOGGER_USB_CDC
60 return LOG_STR("USB_CDC");
61#endif
62 default:
63 return LOG_STR("UNKNOWN");
64 }
65}
66
67#ifdef USE_CRASH_HANDLER
70#endif
71
72} // namespace esphome::logger
73#endif // USE_RP2
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
Definition component.cpp:88
UARTSelection uart_
Definition logger.h:396
const LogString * get_uart_selection_()
void pre_setup()
Set up this component.
void setup() override
bool crash_log_pending()
Whether a crash record from before this boot is waiting to be reported.
@ 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.
ESPHOME_LOG_TAG(TAG, "logger")
bool crash_handler_has_data()
Returns true if crash data was found this boot, reading it first if needed.
void crash_handler_log()
Log crash data if a crash was detected on previous boot.