ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
logger_esp32.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
5#include <driver/uart.h>
6
7namespace esphome::logger {
8
9#ifdef USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
10void write_usb_serial_jtag_line(const char *msg, size_t len);
11#endif
12
13inline void HOT Logger::write_msg_(const char *msg, uint16_t len) {
14#if defined(USE_LOGGER_UART_SELECTION_USB_CDC) || defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
15 // USB CDC/JTAG - single write including newline (already in buffer)
16 // Use fwrite to stdout which goes through VFS to USB console
17 //
18 // Note: These defines indicate the user's YAML configuration choice (hardware_uart: USB_CDC/USB_SERIAL_JTAG).
19 // They are ONLY defined when the user explicitly selects USB as the logger output in their config.
20 // This is compile-time selection, not runtime detection - if USB is configured, it's always used.
21 // There is no fallback to regular UART if "USB isn't connected" - that's the user's responsibility
22 // to configure correctly for their hardware. This approach eliminates runtime overhead.
23#ifdef USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
24 // Not through stdout: its VFS hands the driver one character per call
26#else
27 fwrite(msg, 1, len, stdout);
28#endif
29#else
30 // Regular UART - single write including newline (already in buffer)
31 uart_write_bytes(this->get_uart_num(), msg, len);
32#endif
33}
34
35} // namespace esphome::logger
36
37#endif
void write_msg_(const char *msg, uint16_t len)
constexpr uart_port_t get_uart_num() const
UART_NUM_0 when logging to USB.
Definition logger.h:178
void write_usb_serial_jtag_line(const char *msg, size_t len)
const void size_t len
Definition hal.h:68