[uart][usb_uart] Add debug_prefix option to distinguish multiple defined uarts in log (#14525)

This commit is contained in:
Oliver Kleinecke authored and GitHub committed 2026-03-08 10:16:12 +11:00
1 parent 3f700bac1c
commit f2dfb5e1dc
12 files changed
+138 -48

No files matched your search

+12 -5
View File
@@ -5,6 +5,7 @@
#include <vector>
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/core/string_ref.h"
#include "uart.h"
#include "uart_component.h"
@@ -17,7 +18,7 @@ namespace esphome::uart {
/// 'appropriate time' means exactly, is determined by a number of
/// configurable constraints. E.g. when a given number of bytes is gathered
/// and/or when no more data has been seen for a given time interval.
class UARTDebugger : public Component, public Trigger<UARTDirection, std::vector<uint8_t>> {
class UARTDebugger : public Component, public Trigger<UARTDirection, std::vector<uint8_t>, StringRef> {
public:
explicit UARTDebugger(UARTComponent *parent);
void loop() override;
@@ -41,6 +42,8 @@ class UARTDebugger : public Component, public Trigger<UARTDirection, std::vector
/// logging will be triggered.
void add_delimiter_byte(uint8_t byte) { this->after_delimiter_.push_back(byte); }
void set_debug_prefix(const char *prefix) { this->debug_prefix_ = StringRef(prefix); }
protected:
UARTDirection for_direction_;
UARTDirection last_direction_{};
@@ -51,6 +54,7 @@ class UARTDebugger : public Component, public Trigger<UARTDirection, std::vector
std::vector<uint8_t> after_delimiter_{};
size_t after_delimiter_pos_{};
bool is_triggering_{false};
StringRef debug_prefix_{};
bool is_my_direction_(UARTDirection direction);
bool is_recursive_();
@@ -81,18 +85,21 @@ class UARTDebug {
public:
/// Log the bytes as hex values, separated by the provided separator
/// character.
static void log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator);
static void log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
StringRef prefix = StringRef());
/// Log the bytes as string values, escaping unprintable characters.
static void log_string(UARTDirection direction, std::vector<uint8_t> bytes);
static void log_string(UARTDirection direction, std::vector<uint8_t> bytes, StringRef prefix = StringRef());
/// Log the bytes as integer values, separated by the provided separator
/// character.
static void log_int(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator);
static void log_int(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
StringRef prefix = StringRef());
/// Log the bytes as '<binary> (<hex>)' values, separated by the provided
/// separator.
static void log_binary(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator);
static void log_binary(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
StringRef prefix = StringRef());
};
} // namespace esphome::uart