[logger] Zephyr: Use k_str_out() with known length instead of printk() (#12619)

This commit is contained in:
J. Nick Koston
2025-12-22 14:29:47 -10:00
committed by GitHub
parent 1b31253287
commit b4c92dd8cb
2 changed files with 10 additions and 10 deletions

View File

@@ -118,11 +118,11 @@ static constexpr uint16_t MAX_HEADER_SIZE = 128;
static constexpr size_t MAX_POINTER_REPRESENTATION = 2 + sizeof(void *) * 2 + 1;
// Platform-specific: does write_msg_ add its own newline?
// false: Caller must add newline to buffer before calling write_msg_ (ESP32, ESP8266, RP2040, LibreTiny)
// false: Caller must add newline to buffer before calling write_msg_ (ESP32, ESP8266, RP2040, LibreTiny, Zephyr)
// Allows single write call with newline included for efficiency
// true: write_msg_ adds newline itself via puts()/println() (other platforms)
// Newline should NOT be added to buffer
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY)
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
static constexpr bool WRITE_MSG_ADDS_NEWLINE = false;
#else
static constexpr bool WRITE_MSG_ADDS_NEWLINE = true;

View File

@@ -6,6 +6,7 @@
#include <zephyr/device.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/sys/printk.h>
#include <zephyr/usb/usb_device.h>
namespace esphome::logger {
@@ -14,7 +15,7 @@ static const char *const TAG = "logger";
#ifdef USE_LOGGER_USB_CDC
void Logger::loop() {
if (this->uart_ != UART_SELECTION_USB_CDC || nullptr == this->uart_dev_) {
if (this->uart_ != UART_SELECTION_USB_CDC || this->uart_dev_ == nullptr) {
return;
}
static bool opened = false;
@@ -62,18 +63,17 @@ void Logger::pre_setup() {
ESP_LOGI(TAG, "Log initialized");
}
void HOT Logger::write_msg_(const char *msg, size_t) {
void HOT Logger::write_msg_(const char *msg, size_t len) {
// Single write with newline already in buffer (added by caller)
#ifdef CONFIG_PRINTK
printk("%s\n", msg);
k_str_out(const_cast<char *>(msg), len);
#endif
if (nullptr == this->uart_dev_) {
if (this->uart_dev_ == nullptr) {
return;
}
while (*msg) {
uart_poll_out(this->uart_dev_, *msg);
++msg;
for (size_t i = 0; i < len; ++i) {
uart_poll_out(this->uart_dev_, msg[i]);
}
uart_poll_out(this->uart_dev_, '\n');
}
const LogString *Logger::get_uart_selection_() {