mirror of
https://github.com/esphome/esphome.git
synced 2026-11-15 01:25:57 +00:00
Compare commits
17
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b310c985e0 | ||
|
|
7bc87b9b00 | ||
|
|
a5c1b4f532 | ||
|
|
8fc6fcf724 | ||
|
|
22a2efd8fd | ||
|
|
e0354ae794 | ||
|
|
f9299c1c58 | ||
|
|
09c40438e2 | ||
|
|
63df68acc4 | ||
|
|
8936e4f54b | ||
|
|
1c1cf21b16 | ||
|
|
71a0b9cfca | ||
|
|
ebc4dd2cc9 | ||
|
|
8f705e7cdb | ||
|
|
c2067f4452 | ||
|
|
e1b38f5171 | ||
|
|
ef10ea99b9 |
No files matched your search
@@ -37,6 +37,7 @@ from esphome.types import ConfigType
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
DOMAIN = "logger"
|
||||
logger_ns = cg.esphome_ns.namespace("logger")
|
||||
LOG_LEVELS = {
|
||||
"NONE": cg.global_ns.ESPHOME_LOG_LEVEL_NONE,
|
||||
@@ -93,6 +94,7 @@ UART_SELECTION_RP2040 = [USB_CDC, UART0, UART1]
|
||||
|
||||
UART_SELECTION_NRF52 = [USB_CDC, UART0]
|
||||
|
||||
# Read by device-builder
|
||||
HARDWARE_UART_TO_UART_SELECTION = {
|
||||
UART0: logger_ns.UART_SELECTION_UART0,
|
||||
UART0_SWAP: logger_ns.UART_SELECTION_UART0_SWAP,
|
||||
@@ -108,8 +110,6 @@ HARDWARE_UART_TO_SERIAL = {
|
||||
UART0: cg.global_ns.Serial,
|
||||
UART0_SWAP: cg.global_ns.Serial,
|
||||
UART1: cg.global_ns.Serial1,
|
||||
UART2: cg.global_ns.Serial2,
|
||||
DEFAULT: cg.global_ns.Serial,
|
||||
},
|
||||
PLATFORM_RP2: {
|
||||
UART0: cg.global_ns.Serial1,
|
||||
@@ -503,13 +503,10 @@ async def to_code(config: ConfigType) -> None:
|
||||
CORE.data.setdefault(DOMAIN, {})[CONF_LEVEL] = level
|
||||
tx_buffer_size = config[CONF_TX_BUFFER_SIZE]
|
||||
cg.add_define("ESPHOME_LOGGER_TX_BUFFER_SIZE", tx_buffer_size)
|
||||
# Determine task log buffer size. The buffer is a direct member of Logger
|
||||
# (no separate heap allocation).
|
||||
task_log_buffer_size = 0
|
||||
if CORE.is_esp32 or CORE.is_libretiny or CORE.is_nrf52:
|
||||
task_log_buffer_size = config[CONF_TASK_LOG_BUFFER_SIZE]
|
||||
elif CORE.is_host:
|
||||
task_log_buffer_size = 64 # Fixed 64 slots for host
|
||||
# The host has a fixed 64 slots
|
||||
task_log_buffer_size = (
|
||||
64 if CORE.is_host else config.get(CONF_TASK_LOG_BUFFER_SIZE, 0)
|
||||
)
|
||||
if task_log_buffer_size > 0:
|
||||
cg.add_define("USE_ESPHOME_TASK_LOG_BUFFER")
|
||||
cg.add_define("ESPHOME_TASK_LOG_BUFFER_SIZE", task_log_buffer_size)
|
||||
@@ -542,17 +539,17 @@ async def to_code(config: ConfigType) -> None:
|
||||
cg.add(log.set_boot_log_level(tag, LOG_LEVELS[log_level]))
|
||||
if CORE.is_esp32 or CORE.is_host:
|
||||
cg.add(log.create_pthread_key())
|
||||
# set_uart_selection() must be called before pre_setup() because
|
||||
# pre_setup() switches on uart_ to decide which hardware to initialize
|
||||
# (e.g. UART0 vs USB_SERIAL_JTAG). Without this, uart_ is still the
|
||||
# default UART_SELECTION_UART0 and the wrong hardware gets initialized.
|
||||
# uart_ is UART0 in C++ except on LibreTiny where it is DEFAULT; skip the
|
||||
# setter when the config matches it.
|
||||
cpp_default_uart = DEFAULT if CORE.is_libretiny else UART0
|
||||
if (
|
||||
hardware_uart := config.get(CONF_HARDWARE_UART)
|
||||
) is not None and hardware_uart != cpp_default_uart:
|
||||
cg.add(log.set_uart_selection(HARDWARE_UART_TO_UART_SELECTION[hardware_uart]))
|
||||
if (hardware_uart := config.get(CONF_HARDWARE_UART)) is not None:
|
||||
cg.add_define(f"USE_LOGGER_UART_SELECTION_{hardware_uart}")
|
||||
cg.add_define(
|
||||
"ESPHOME_LOGGER_UART_SELECTION",
|
||||
cg.RawExpression(f"logger::UART_SELECTION_{hardware_uart}"),
|
||||
)
|
||||
# ESP8266 without serial logging never opens a port
|
||||
cg.add_define(
|
||||
"ESPHOME_LOGGER_UART_NAME",
|
||||
"NONE" if CORE.is_esp8266 and baud_rate == 0 else hardware_uart,
|
||||
)
|
||||
# pre_setup() sets global_logger and must run before any other code
|
||||
# that may call ESP_LOG* (e.g. setup_preferences contains ESP_LOGVV).
|
||||
cg.add(log.pre_setup())
|
||||
@@ -647,10 +644,8 @@ async def _late_logger_init(config: ConfigType) -> None:
|
||||
|
||||
if config[CONF_HARDWARE_UART] == USB_CDC:
|
||||
add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_CDC", True)
|
||||
cg.add_define("USE_LOGGER_UART_SELECTION_USB_CDC")
|
||||
elif config[CONF_HARDWARE_UART] == USB_SERIAL_JTAG:
|
||||
add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG", True)
|
||||
cg.add_define("USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG")
|
||||
# Log V2 formats centrally in esp_log() (~4KB flash, ~180B RAM). 6.1
|
||||
# adds CONSTRAINED_ENV_SAFE=n, keeping early/DRAM logs on
|
||||
# esp_rom_printf instead of pulling ~1.2KB esp_rom_vprintf into IRAM.
|
||||
@@ -667,8 +662,13 @@ async def _late_logger_init(config: ConfigType) -> None:
|
||||
cg.add_define("USE_ESP32_LOG_V2")
|
||||
# --wrap: a strong definition collides; see logger_esp32.cpp
|
||||
cg.add_build_flag("-Wl,--wrap=esp_log_format")
|
||||
if CORE.is_rp2 and config[CONF_HARDWARE_UART] == USB_CDC:
|
||||
cg.add_define("USE_LOGGER_UART_SELECTION_USB_CDC")
|
||||
# Zephyr polls the USB port only when it logs there
|
||||
if (
|
||||
CORE.using_zephyr
|
||||
and has_serial_logging
|
||||
and config[CONF_HARDWARE_UART] == USB_CDC
|
||||
):
|
||||
cg.add_define("USE_LOGGER_ZEPHYR_CDC_LOOP")
|
||||
# Define platform support flags for components that need auto-detection
|
||||
try:
|
||||
uart_selection(USB_SERIAL_JTAG)
|
||||
@@ -718,7 +718,6 @@ async def _late_logger_init(config: ConfigType) -> None:
|
||||
if config[CONF_HARDWARE_UART] == UART1:
|
||||
zephyr_add_overlay("""&uart1 { status = "okay";};""")
|
||||
if config[CONF_HARDWARE_UART] == USB_CDC:
|
||||
cg.add_define("USE_LOGGER_UART_SELECTION_USB_CDC")
|
||||
zephyr_add_prj_conf("UART_LINE_CTRL", True)
|
||||
|
||||
# Register at end for safe mode
|
||||
@@ -739,8 +738,6 @@ async def _late_logger_init(config: ConfigType) -> None:
|
||||
conf,
|
||||
)
|
||||
|
||||
CORE.add_job(final_step)
|
||||
|
||||
|
||||
def validate_printf(value: ConfigType) -> ConfigType:
|
||||
# https://stackoverflow.com/questions/30011379/how-can-i-parse-a-c-format-string-in-python
|
||||
@@ -875,11 +872,6 @@ FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
}
|
||||
)
|
||||
|
||||
# Keys for CORE.data storage
|
||||
DOMAIN = "logger"
|
||||
KEY_LEVEL_LISTENERS = "level_listeners"
|
||||
KEY_LOG_LISTENERS = "log_listeners"
|
||||
|
||||
|
||||
def request_logger_level_listeners() -> None:
|
||||
"""Request that logger level listeners be compiled in.
|
||||
@@ -888,29 +880,18 @@ def request_logger_level_listeners() -> None:
|
||||
function during their code generation. This enables the add_level_listener()
|
||||
method and compiles in the listener vector.
|
||||
"""
|
||||
CORE.data.setdefault(DOMAIN, {})[KEY_LEVEL_LISTENERS] = True
|
||||
cg.add_define("USE_LOGGER_LEVEL_LISTENERS")
|
||||
|
||||
|
||||
_request_log_listener_slot = cg.slot_counter("ESPHOME_LOG_MAX_LISTENERS")
|
||||
|
||||
|
||||
def request_console() -> None:
|
||||
"""Compile in Logger::console_read() and console_write(); call from to_code."""
|
||||
cg.add_define("USE_LOGGER_CONSOLE")
|
||||
|
||||
|
||||
def request_log_listener() -> None:
|
||||
"""Request a log listener slot.
|
||||
|
||||
Components that need to receive log messages should call this function
|
||||
during their code generation. This increments the listener count used
|
||||
to size the StaticVector.
|
||||
"""
|
||||
data = CORE.data.setdefault(DOMAIN, {})
|
||||
data[KEY_LOG_LISTENERS] = data.get(KEY_LOG_LISTENERS, 0) + 1
|
||||
|
||||
|
||||
@coroutine_with_priority(CoroPriority.FINAL)
|
||||
async def final_step() -> None:
|
||||
"""Final code generation step to configure optional logger features."""
|
||||
domain_data = CORE.data.get(DOMAIN, {})
|
||||
if domain_data.get(KEY_LEVEL_LISTENERS, False):
|
||||
cg.add_define("USE_LOGGER_LEVEL_LISTENERS")
|
||||
|
||||
# Only generate log listener code if any component needs it
|
||||
log_listener_count = domain_data.get(KEY_LOG_LISTENERS, 0)
|
||||
if log_listener_count > 0:
|
||||
cg.add_define("USE_LOG_LISTENERS")
|
||||
cg.add_define("ESPHOME_LOG_MAX_LISTENERS", log_listener_count)
|
||||
"""Reserve a log listener slot; call once per listener from to_code."""
|
||||
cg.add_define("USE_LOG_LISTENERS")
|
||||
_request_log_listener_slot()
|
||||
@@ -32,7 +32,7 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
|
||||
// Get task handle once - used for both main task check and passing to non-main thread handler
|
||||
TaskHandle_t current_task = xTaskGetCurrentTaskHandle();
|
||||
const bool is_main_task = (current_task == this->main_task_);
|
||||
#elif (USE_ZEPHYR)
|
||||
#elif defined(USE_ZEPHYR)
|
||||
k_tid_t current_task = k_current_get();
|
||||
const bool is_main_task = (current_task == this->main_task_);
|
||||
#else // USE_HOST
|
||||
@@ -57,10 +57,10 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
|
||||
// (we already have the handle from the main task check above).
|
||||
// Host: pass a stack buffer for pthread_getname_np to write into.
|
||||
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||
const char *thread_name = get_thread_name_(current_task);
|
||||
const char *thread_name = this->get_thread_name_(current_task);
|
||||
#elif defined(USE_ZEPHYR)
|
||||
char thread_name_buf[MAX_POINTER_REPRESENTATION];
|
||||
const char *thread_name = get_thread_name_(thread_name_buf, current_task);
|
||||
const char *thread_name = this->get_thread_name_(thread_name_buf, current_task);
|
||||
#else // USE_HOST
|
||||
char thread_name_buf[THREAD_NAME_BUF_SIZE];
|
||||
const char *thread_name = this->get_thread_name_(thread_name_buf);
|
||||
@@ -103,10 +103,10 @@ void Logger::log_vprintf_non_main_thread_(uint8_t level, const char *tag, int li
|
||||
{
|
||||
#ifdef USE_HOST
|
||||
// Host always has console output - no baud_rate check needed
|
||||
static const size_t MAX_CONSOLE_LOG_MSG_SIZE = 512;
|
||||
static constexpr size_t MAX_CONSOLE_LOG_MSG_SIZE = 512;
|
||||
#else
|
||||
// Maximum size for console log messages (includes null terminator)
|
||||
static const size_t MAX_CONSOLE_LOG_MSG_SIZE = 144;
|
||||
static constexpr size_t MAX_CONSOLE_LOG_MSG_SIZE = 144;
|
||||
#endif
|
||||
char console_buffer[MAX_CONSOLE_LOG_MSG_SIZE]; // MUST be stack allocated for thread safety
|
||||
LogBuffer buf{console_buffer, MAX_CONSOLE_LOG_MSG_SIZE};
|
||||
@@ -118,13 +118,11 @@ void Logger::log_vprintf_non_main_thread_(uint8_t level, const char *tag, int li
|
||||
}
|
||||
#else
|
||||
// Implementation for single-task platforms (ESP8266, RP2040)
|
||||
// Logging calls are NOT thread-safe: global_recursion_guard_ is a plain bool and tx_buffer_ has no locking.
|
||||
// Not a problem in practice yet since Zephyr has no API support (logs are console-only).
|
||||
// Logging calls are NOT thread-safe: the recursion guard is a plain bool and tx_buffer_ has no locking.
|
||||
void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const char *format, va_list args) { // NOLINT
|
||||
if (level > this->level_for(tag) || global_recursion_guard_)
|
||||
if (level > this->level_for(tag) || this->main_task_recursion_guard_)
|
||||
return;
|
||||
// Other single-task platforms don't have thread names, so pass nullptr
|
||||
this->log_message_to_buffer_and_send_(global_recursion_guard_, level, tag, line, format, args, nullptr);
|
||||
this->log_message_to_buffer_and_send_(this->main_task_recursion_guard_, level, tag, line, format, args, nullptr);
|
||||
}
|
||||
#endif // USE_ESP32 || USE_HOST || USE_LIBRETINY || USE_ZEPHYR
|
||||
|
||||
@@ -136,10 +134,10 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
|
||||
//
|
||||
void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format,
|
||||
va_list args) { // NOLINT
|
||||
if (level > this->level_for(tag) || global_recursion_guard_)
|
||||
if (level > this->level_for(tag) || this->main_task_recursion_guard_)
|
||||
return;
|
||||
|
||||
this->log_message_to_buffer_and_send_(global_recursion_guard_, level, tag, line, format, args, nullptr);
|
||||
this->log_message_to_buffer_and_send_(this->main_task_recursion_guard_, level, tag, line, format, args, nullptr);
|
||||
}
|
||||
#endif // USE_ESP8266
|
||||
|
||||
@@ -184,10 +182,10 @@ Logger::Logger(uint32_t baud_rate) : baud_rate_(baud_rate) {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || (defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
|
||||
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
|
||||
void Logger::loop() {
|
||||
this->process_messages_();
|
||||
#if defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
|
||||
this->cdc_loop_();
|
||||
#endif
|
||||
}
|
||||
@@ -213,11 +211,11 @@ void Logger::process_messages_() {
|
||||
this->write_log_buffer_to_console_(buf);
|
||||
}
|
||||
}
|
||||
#if !(defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
|
||||
// Zephyr with USB CDC keeps the loop running for cdc_loop_()
|
||||
#ifndef USE_LOGGER_ZEPHYR_CDC_LOOP
|
||||
else {
|
||||
// No messages to process, disable loop if appropriate
|
||||
// This reduces overhead when there's no async logging activity
|
||||
this->disable_loop_when_buffer_empty_();
|
||||
// A concurrent enable_loop_soon_any_context() still wakes the next iteration
|
||||
this->disable_loop();
|
||||
}
|
||||
#endif
|
||||
#endif // USE_ESPHOME_TASK_LOG_BUFFER
|
||||
@@ -265,7 +263,7 @@ void Logger::boot_log_capture_(uint8_t level, const char *tag, const LogBuffer &
|
||||
|
||||
void Logger::replay_boot_logs() {
|
||||
// The replay shares tx_buffer_ with normal logging, so it is dropped like any other re-entrant log
|
||||
bool &recursion_guard = this->main_loop_recursion_guard_();
|
||||
bool &recursion_guard = this->main_task_recursion_guard_;
|
||||
if (recursion_guard)
|
||||
return;
|
||||
this->boot_log_done_ = true; // Stops capture first, so the replay output is not buffered again
|
||||
@@ -307,6 +305,25 @@ static const LogString *get_log_level_str(uint8_t level) {
|
||||
return LogLevelStrings::get_log_str(level, LogLevelStrings::LAST_INDEX);
|
||||
}
|
||||
|
||||
#ifndef USE_HOST
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
#ifdef USE_LOGGER_UART_SELECTION_DEFAULT
|
||||
// LibreTiny resolves DEFAULT to the board's port only when serial logging is on
|
||||
if (this->baud_rate_ > 0) {
|
||||
switch (this->get_uart()) {
|
||||
case UART_SELECTION_UART0:
|
||||
return LOG_STR("UART0");
|
||||
case UART_SELECTION_UART1:
|
||||
return LOG_STR("UART1");
|
||||
default:
|
||||
return LOG_STR("UART2");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return LOG_STR(ESPHOME_LOGGER_UART_NAME);
|
||||
}
|
||||
#endif
|
||||
|
||||
void Logger::dump_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"Logger:\n"
|
||||
@@ -342,7 +359,7 @@ void Logger::dump_config() {
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_ZEPHYR
|
||||
dump_crash_();
|
||||
this->dump_crash_();
|
||||
#ifdef USE_BOOTLOADER_ADAFRUIT
|
||||
this->dump_bootloader_();
|
||||
#endif
|
||||
|
||||
@@ -42,9 +42,6 @@ struct device;
|
||||
namespace esphome::logger {
|
||||
|
||||
/** Lightweight callback for receiving log messages without virtual dispatch overhead.
|
||||
*
|
||||
* Replaces the former LogListener virtual interface to eliminate per-implementer
|
||||
* vtable sub-tables and thunk code (~39 bytes saved per class that used LogListener).
|
||||
*
|
||||
* Usage:
|
||||
* // In your component's setup():
|
||||
@@ -73,7 +70,7 @@ struct LogCallback {
|
||||
* public:
|
||||
* void setup() override {
|
||||
* if (logger::global_logger != nullptr)
|
||||
* logger::global_logger->add_logger_level_listener(this);
|
||||
* logger::global_logger->add_level_listener(this);
|
||||
* }
|
||||
* void on_log_level_change(uint8_t level) override {
|
||||
* // Handle log level change
|
||||
@@ -127,6 +124,9 @@ enum UARTSelection : uint8_t {
|
||||
UART_SELECTION_UART0_SWAP,
|
||||
#endif // USE_ESP8266
|
||||
};
|
||||
|
||||
// The port chosen by hardware_uart, fixed at build time
|
||||
static constexpr UARTSelection CONFIGURED_UART_SELECTION = ESPHOME_LOGGER_UART_SELECTION;
|
||||
#endif // USE_ESP32 || USE_ESP8266 || USE_RP2 || USE_LIBRETINY || USE_ZEPHYR
|
||||
|
||||
/**
|
||||
@@ -138,7 +138,7 @@ enum UARTSelection : uint8_t {
|
||||
* - On ESP32: Uses task-specific recursion guards
|
||||
* * Main task: Uses a dedicated boolean member variable for efficiency
|
||||
* * Other tasks: Uses pthread TLS with a dynamically allocated key for task-specific state
|
||||
* - On other platforms: Uses a simple global recursion guard
|
||||
* - On single-task platforms: Uses the main task guard alone
|
||||
*
|
||||
* We use pthread TLS via pthread_key_create to create a unique key for storing
|
||||
* task-specific recursion state, which:
|
||||
@@ -149,7 +149,7 @@ enum UARTSelection : uint8_t {
|
||||
class Logger final : public Component {
|
||||
public:
|
||||
explicit Logger(uint32_t baud_rate);
|
||||
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || (defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
|
||||
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
|
||||
void loop() override;
|
||||
#endif
|
||||
#if defined(USE_RP2_CRASH_HANDLER) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
@@ -157,21 +157,52 @@ class Logger final : public Component {
|
||||
#endif
|
||||
/// Manually set the baud rate for serial, set to 0 to disable.
|
||||
void set_baud_rate(uint32_t baud_rate) { this->baud_rate_ = baud_rate; }
|
||||
uint32_t get_baud_rate() const { return baud_rate_; }
|
||||
uint32_t get_baud_rate() const { return this->baud_rate_; }
|
||||
#if defined(USE_ARDUINO) && !defined(USE_ESP32)
|
||||
Stream *get_hw_serial() const { return hw_serial_; }
|
||||
Stream *get_hw_serial() const { return this->hw_serial_; }
|
||||
#endif
|
||||
#ifdef USE_ESP32
|
||||
uart_port_t get_uart_num() const { return uart_num_; }
|
||||
void create_pthread_key() { pthread_key_create(&log_recursion_key_, nullptr); }
|
||||
/// UART_NUM_0 when logging to USB
|
||||
constexpr uart_port_t get_uart_num() const {
|
||||
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC) || defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
|
||||
return UART_NUM_0;
|
||||
#else
|
||||
return static_cast<uart_port_t>(CONFIGURED_UART_SELECTION);
|
||||
#endif
|
||||
#ifdef USE_HOST
|
||||
void create_pthread_key() { pthread_key_create(&log_recursion_key_, nullptr); }
|
||||
}
|
||||
#endif
|
||||
#if defined(USE_ESP32) || defined(USE_HOST)
|
||||
void create_pthread_key() { pthread_key_create(&this->log_recursion_key_, nullptr); }
|
||||
#endif
|
||||
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
|
||||
void set_uart_selection(UARTSelection uart_selection) { uart_ = uart_selection; }
|
||||
/// Get the UART used by the logger.
|
||||
UARTSelection get_uart() const { return this->uart_; }
|
||||
#ifdef USE_LIBRETINY
|
||||
// DEFAULT resolves to the board's default port
|
||||
constexpr UARTSelection get_uart() const {
|
||||
return CONFIGURED_UART_SELECTION == UART_SELECTION_DEFAULT ? static_cast<UARTSelection>(LT_UART_DEFAULT_SERIAL + 1)
|
||||
: CONFIGURED_UART_SELECTION;
|
||||
}
|
||||
#else
|
||||
constexpr UARTSelection get_uart() const { return CONFIGURED_UART_SELECTION; }
|
||||
#endif
|
||||
#endif
|
||||
#ifdef USE_LOGGER_CONSOLE
|
||||
// Raw bytes on the console port, without log framing; request with logger.request_console()
|
||||
#if defined(USE_ARDUINO) && !defined(USE_ESP32)
|
||||
size_t console_write(const uint8_t *data, size_t len) { return this->hw_serial_->write(data, len); }
|
||||
bool console_read(uint8_t &byte) {
|
||||
int c = this->hw_serial_->read();
|
||||
if (c < 0)
|
||||
return false;
|
||||
byte = c;
|
||||
return true;
|
||||
}
|
||||
#elif defined(USE_ESP32)
|
||||
/// Blocking write; returns the bytes written.
|
||||
size_t console_write(const uint8_t *data, size_t len);
|
||||
/// Non-blocking read of one byte; false if none is waiting.
|
||||
bool console_read(uint8_t &byte);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/// Set the default log level for this logger.
|
||||
@@ -242,14 +273,17 @@ class Logger final : public Component {
|
||||
void log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args,
|
||||
const char *thread_name);
|
||||
#endif
|
||||
#if defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
|
||||
void cdc_loop_();
|
||||
#endif
|
||||
void process_messages_();
|
||||
#if defined(USE_HOST) || defined(USE_ZEPHYR)
|
||||
void write_msg_(const char *msg, uint16_t len);
|
||||
// msg already ends in a newline
|
||||
#if defined(USE_ARDUINO) && !defined(USE_ESP32)
|
||||
inline void HOT write_msg_(const char *msg, uint16_t len) { this->hw_serial_->write(msg, len); }
|
||||
#elif defined(USE_ESP32)
|
||||
inline void write_msg_(const char *msg, uint16_t len); // Defined in logger_esp32.h
|
||||
#else
|
||||
inline void write_msg_(const char *msg, uint16_t len); // Defined in platform-specific logger_*.h
|
||||
void write_msg_(const char *msg, uint16_t len);
|
||||
#endif
|
||||
|
||||
// Format a log message with printf-style arguments and write it to a buffer with header, footer, and null terminator
|
||||
@@ -317,7 +351,6 @@ class Logger final : public Component {
|
||||
|
||||
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
|
||||
// Helper to format a pre-formatted message from the task log buffer and notify listeners
|
||||
// Used by process_messages_ to avoid code duplication between ESP32 and host platforms
|
||||
inline 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) {
|
||||
@@ -359,15 +392,8 @@ class Logger final : public Component {
|
||||
#ifdef USE_HOST
|
||||
pthread_t main_thread_{}; // Main thread for pthread_equal() comparison
|
||||
#endif
|
||||
#ifdef USE_ESP32
|
||||
// Task-specific recursion guards:
|
||||
// - Main task uses a dedicated member variable for efficiency
|
||||
// - Other tasks use pthread TLS with a dynamically created key via pthread_key_create
|
||||
pthread_key_t log_recursion_key_; // 4 bytes
|
||||
uart_port_t uart_num_; // 4 bytes (enum defaults to int size)
|
||||
#endif
|
||||
#ifdef USE_HOST
|
||||
// Thread-specific recursion guards using pthread TLS
|
||||
#if defined(USE_ESP32) || defined(USE_HOST)
|
||||
// Non-main task recursion guard (pthread TLS)
|
||||
pthread_key_t log_recursion_key_;
|
||||
#endif
|
||||
|
||||
@@ -392,23 +418,10 @@ class Logger final : public Component {
|
||||
bool boot_log_done_{false}; // Set on first replay; stops further capture
|
||||
#endif
|
||||
uint8_t current_level_{ESPHOME_LOG_LEVEL_VERY_VERBOSE};
|
||||
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_ZEPHYR)
|
||||
UARTSelection uart_{UART_SELECTION_UART0}; // Must match cpp_default_uart in __init__.py
|
||||
#endif
|
||||
#ifdef USE_LIBRETINY
|
||||
UARTSelection uart_{UART_SELECTION_DEFAULT}; // Must match cpp_default_uart in __init__.py
|
||||
#endif
|
||||
#if defined(USE_ESP32) || defined(USE_HOST) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
|
||||
bool main_task_recursion_guard_{false};
|
||||
bool main_task_recursion_guard_{false}; // Guards tx_buffer_ on the main loop
|
||||
#ifdef USE_LIBRETINY
|
||||
bool non_main_task_recursion_guard_{false}; // Shared guard for all non-main tasks on LibreTiny
|
||||
#endif
|
||||
// The guard that protects tx_buffer_ on the main loop, for callers that run on any platform
|
||||
bool &main_loop_recursion_guard_() { return this->main_task_recursion_guard_; }
|
||||
#else
|
||||
bool global_recursion_guard_{false}; // Simple global recursion guard for single-task platforms
|
||||
bool &main_loop_recursion_guard_() { return this->global_recursion_guard_; }
|
||||
#endif
|
||||
|
||||
// Large buffers placed last to keep frequently-accessed member offsets small
|
||||
char tx_buffer_[ESPHOME_LOGGER_TX_BUFFER_SIZE + 1]; // +1 for null terminator
|
||||
@@ -477,9 +490,9 @@ class Logger final : public Component {
|
||||
class NonMainTaskRecursionGuard {
|
||||
public:
|
||||
explicit NonMainTaskRecursionGuard(pthread_key_t key) : key_(key) {
|
||||
pthread_setspecific(key_, reinterpret_cast<void *>(1));
|
||||
pthread_setspecific(this->key_, reinterpret_cast<void *>(1));
|
||||
}
|
||||
~NonMainTaskRecursionGuard() { pthread_setspecific(key_, nullptr); }
|
||||
~NonMainTaskRecursionGuard() { pthread_setspecific(this->key_, nullptr); }
|
||||
NonMainTaskRecursionGuard(const NonMainTaskRecursionGuard &) = delete;
|
||||
NonMainTaskRecursionGuard &operator=(const NonMainTaskRecursionGuard &) = delete;
|
||||
NonMainTaskRecursionGuard(NonMainTaskRecursionGuard &&) = delete;
|
||||
@@ -490,40 +503,24 @@ class Logger final : public Component {
|
||||
};
|
||||
|
||||
// Check if non-main task is already in recursion (via TLS)
|
||||
inline bool HOT is_non_main_task_recursive_() const { return pthread_getspecific(log_recursion_key_) != nullptr; }
|
||||
inline bool HOT is_non_main_task_recursive_() const {
|
||||
return pthread_getspecific(this->log_recursion_key_) != nullptr;
|
||||
}
|
||||
|
||||
// Create RAII guard for non-main task recursion
|
||||
inline NonMainTaskRecursionGuard make_non_main_task_guard_() { return NonMainTaskRecursionGuard(log_recursion_key_); }
|
||||
|
||||
#elif defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
|
||||
// LibreTiny doesn't have FreeRTOS TLS, so use a simple approach:
|
||||
// - Main task uses dedicated boolean (same as ESP32)
|
||||
// - Non-main tasks share a single recursion guard
|
||||
// This is safe because:
|
||||
// - Recursion from logging within logging is the main concern
|
||||
// - Cross-task "recursion" is prevented by the buffer mutex anyway
|
||||
// - Missing a recursive call from another task is acceptable (falls back to direct output)
|
||||
//
|
||||
// Zephyr use __thread as TLS
|
||||
|
||||
// Check if non-main task is already in recursion
|
||||
inline bool HOT is_non_main_task_recursive_() const { return non_main_task_recursion_guard_; }
|
||||
|
||||
// Create RAII guard for non-main task recursion (uses shared boolean for all non-main tasks)
|
||||
inline RecursionGuard make_non_main_task_guard_() { return RecursionGuard(non_main_task_recursion_guard_); }
|
||||
#endif
|
||||
|
||||
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) && !(defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
|
||||
// Disable loop when task buffer is empty
|
||||
// Zephyr with USB CDC needs loop active to poll port readiness via cdc_loop_()
|
||||
inline void disable_loop_when_buffer_empty_() {
|
||||
// Thread safety note: This is safe even if another task calls enable_loop_soon_any_context()
|
||||
// concurrently. If that happens between our check and disable_loop(), the enable request
|
||||
// will be processed on the next main loop iteration since:
|
||||
// - disable_loop() takes effect immediately
|
||||
// - enable_loop_soon_any_context() sets a pending flag that's checked at loop start
|
||||
this->disable_loop();
|
||||
inline NonMainTaskRecursionGuard make_non_main_task_guard_() {
|
||||
return NonMainTaskRecursionGuard(this->log_recursion_key_);
|
||||
}
|
||||
|
||||
#elif defined(USE_LIBRETINY)
|
||||
// No FreeRTOS TLS on LibreTiny: all non-main tasks share one guard
|
||||
inline bool HOT is_non_main_task_recursive_() const { return this->non_main_task_recursion_guard_; }
|
||||
inline RecursionGuard make_non_main_task_guard_() { return RecursionGuard(this->non_main_task_recursion_guard_); }
|
||||
|
||||
#elif defined(USE_ZEPHYR)
|
||||
// Thread-local guard (__thread)
|
||||
inline bool HOT is_non_main_task_recursive_() const { return non_main_task_recursion_guard; }
|
||||
inline RecursionGuard make_non_main_task_guard_() { return RecursionGuard(non_main_task_recursion_guard); }
|
||||
#endif
|
||||
};
|
||||
extern Logger *global_logger; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
@@ -563,14 +560,7 @@ void log_crash_to_client();
|
||||
|
||||
} // namespace esphome::logger
|
||||
|
||||
// Platform-specific inline implementations of write_msg_()
|
||||
// Must be included after the Logger class definition is complete
|
||||
#if defined(USE_ESP32)
|
||||
// ESP32 inline write_msg_(), included after the Logger class definition is complete
|
||||
#ifdef USE_ESP32
|
||||
#include "logger_esp32.h"
|
||||
#elif defined(USE_ESP8266)
|
||||
#include "logger_esp8266.h"
|
||||
#elif defined(USE_RP2)
|
||||
#include "logger_rp2.h"
|
||||
#elif defined(USE_LIBRETINY)
|
||||
#include "logger_libretiny.h"
|
||||
#endif
|
||||
@@ -17,6 +17,9 @@
|
||||
#include <driver/usb_serial_jtag_vfs.h>
|
||||
#endif
|
||||
#endif
|
||||
#if defined(USE_LOGGER_CONSOLE) && defined(USE_LOGGER_UART_SELECTION_USB_CDC) && defined(CONFIG_ESP_CONSOLE_USB_CDC)
|
||||
#include <esp_private/usb_console.h>
|
||||
#endif
|
||||
#if defined(CONFIG_PM_ENABLE) && defined(CONFIG_FREERTOS_USE_TICKLESS_IDLE) && \
|
||||
(ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
|
||||
#include "esp_sleep.h"
|
||||
@@ -73,7 +76,8 @@ static void init_usb_serial_jtag() {
|
||||
}
|
||||
#endif
|
||||
|
||||
void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_size) {
|
||||
#if !defined(USE_LOGGER_UART_SELECTION_USB_CDC) && !defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
|
||||
static void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_size) {
|
||||
uart_config_t uart_config{};
|
||||
uart_config.baud_rate = (int) baud_rate;
|
||||
uart_config.data_bits = UART_DATA_8_BITS;
|
||||
@@ -97,37 +101,22 @@ void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_size) {
|
||||
esp_sleep_set_console_uart_handling_mode(ESP_SLEEP_ALWAYS_FLUSH_UART);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
// get_uart_num() and pre_setup() cast the selection straight to a port
|
||||
static_assert(static_cast<int>(UART_SELECTION_UART0) == static_cast<int>(UART_NUM_0) &&
|
||||
static_cast<int>(UART_SELECTION_UART1) == static_cast<int>(UART_NUM_1));
|
||||
#ifdef USE_ESP32_VARIANT_ESP32
|
||||
static_assert(static_cast<int>(UART_SELECTION_UART2) == static_cast<int>(UART_NUM_2));
|
||||
#endif
|
||||
|
||||
void Logger::pre_setup() {
|
||||
if (this->baud_rate_ > 0) {
|
||||
this->uart_num_ = UART_NUM_0;
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
this->uart_num_ = UART_NUM_0;
|
||||
init_uart(this->uart_num_, baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
|
||||
break;
|
||||
case UART_SELECTION_UART1:
|
||||
this->uart_num_ = UART_NUM_1;
|
||||
init_uart(this->uart_num_, baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
|
||||
break;
|
||||
#ifdef USE_ESP32_VARIANT_ESP32
|
||||
case UART_SELECTION_UART2:
|
||||
this->uart_num_ = UART_NUM_2;
|
||||
init_uart(this->uart_num_, baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
|
||||
break;
|
||||
#endif
|
||||
#ifdef USE_LOGGER_USB_CDC
|
||||
case UART_SELECTION_USB_CDC:
|
||||
break;
|
||||
#endif
|
||||
#ifdef USE_LOGGER_USB_SERIAL_JTAG
|
||||
case UART_SELECTION_USB_SERIAL_JTAG:
|
||||
#ifdef USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
|
||||
init_usb_serial_jtag();
|
||||
init_usb_serial_jtag();
|
||||
#elif !defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
init_uart(this->get_uart_num(), this->baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
|
||||
#endif
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
global_logger = this;
|
||||
@@ -139,29 +128,36 @@ void Logger::pre_setup() {
|
||||
#endif
|
||||
}
|
||||
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
return LOG_STR("UART0");
|
||||
case UART_SELECTION_UART1:
|
||||
return LOG_STR("UART1");
|
||||
#ifdef USE_ESP32_VARIANT_ESP32
|
||||
case UART_SELECTION_UART2:
|
||||
return LOG_STR("UART2");
|
||||
#ifdef USE_LOGGER_CONSOLE
|
||||
size_t Logger::console_write(const uint8_t *data, size_t len) {
|
||||
int written = 0;
|
||||
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
#ifdef CONFIG_ESP_CONSOLE_USB_CDC
|
||||
written = esp_usb_console_write_buf(reinterpret_cast<const char *>(data), len);
|
||||
#endif
|
||||
#ifdef USE_LOGGER_USB_CDC
|
||||
case UART_SELECTION_USB_CDC:
|
||||
return LOG_STR("USB_CDC");
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
|
||||
written = usb_serial_jtag_write_bytes(data, len, 20 / portTICK_PERIOD_MS);
|
||||
#else
|
||||
written = uart_write_bytes(this->get_uart_num(), data, len);
|
||||
#endif
|
||||
#ifdef USE_LOGGER_USB_SERIAL_JTAG
|
||||
case UART_SELECTION_USB_SERIAL_JTAG:
|
||||
return LOG_STR("USB_SERIAL_JTAG");
|
||||
#endif
|
||||
default:
|
||||
return LOG_STR("UNKNOWN");
|
||||
}
|
||||
return written > 0 ? written : 0;
|
||||
}
|
||||
|
||||
bool Logger::console_read(uint8_t &byte) {
|
||||
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
#ifdef CONFIG_ESP_CONSOLE_USB_CDC
|
||||
return esp_usb_console_available_for_read() && esp_usb_console_read_buf(reinterpret_cast<char *>(&byte), 1) == 1;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
|
||||
return usb_serial_jtag_read_bytes(&byte, 1, 0) > 0;
|
||||
#else
|
||||
return uart_read_bytes(this->get_uart_num(), &byte, 1, 0) > 0;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_CRASH_HANDLER
|
||||
bool crash_log_pending() { return esp32::crash_handler_has_data(); }
|
||||
void log_crash_to_client() {
|
||||
|
||||
@@ -19,7 +19,7 @@ inline void HOT Logger::write_msg_(const char *msg, uint16_t len) {
|
||||
fwrite(msg, 1, len, stdout);
|
||||
#else
|
||||
// Regular UART - single write including newline (already in buffer)
|
||||
uart_write_bytes(this->uart_num_, msg, len);
|
||||
uart_write_bytes(this->get_uart_num(), msg, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -14,9 +14,9 @@ void Logger::pre_setup() {
|
||||
#if defined(USE_ESP8266_LOGGER_SERIAL)
|
||||
this->hw_serial_ = &Serial;
|
||||
Serial.begin(this->baud_rate_);
|
||||
if (this->uart_ == UART_SELECTION_UART0_SWAP) {
|
||||
Serial.swap();
|
||||
}
|
||||
#ifdef USE_LOGGER_UART_SELECTION_UART0_SWAP
|
||||
Serial.swap();
|
||||
#endif
|
||||
Serial.setDebugOutput(ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE);
|
||||
#elif defined(USE_ESP8266_LOGGER_SERIAL1)
|
||||
this->hw_serial_ = &Serial1;
|
||||
@@ -35,19 +35,6 @@ void Logger::pre_setup() {
|
||||
#endif
|
||||
}
|
||||
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
#if defined(USE_ESP8266_LOGGER_SERIAL)
|
||||
if (this->uart_ == UART_SELECTION_UART0_SWAP) {
|
||||
return LOG_STR("UART0_SWAP");
|
||||
}
|
||||
return LOG_STR("UART0");
|
||||
#elif defined(USE_ESP8266_LOGGER_SERIAL1)
|
||||
return LOG_STR("UART1");
|
||||
#else
|
||||
return LOG_STR("NONE");
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_CRASH_HANDLER
|
||||
bool crash_log_pending() { return esp8266::crash_handler_has_data(); }
|
||||
void log_crash_to_client() { esp8266::crash_handler_log(); }
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome::logger {
|
||||
|
||||
// Single write with newline already in buffer (added by caller)
|
||||
inline void HOT Logger::write_msg_(const char *msg, uint16_t len) { this->hw_serial_->write(msg, len); }
|
||||
|
||||
} // namespace esphome::logger
|
||||
|
||||
#endif
|
||||
@@ -12,45 +12,28 @@ ESPHOME_LOG_TAG(TAG, "logger");
|
||||
|
||||
void Logger::pre_setup() {
|
||||
if (this->baud_rate_ > 0) {
|
||||
switch (this->uart_) {
|
||||
#if LT_HW_UART0
|
||||
case UART_SELECTION_UART0:
|
||||
this->hw_serial_ = &Serial0;
|
||||
libretiny::ensure_serial_handle(0);
|
||||
Serial0.begin(this->baud_rate_);
|
||||
break;
|
||||
#endif
|
||||
#if LT_HW_UART1
|
||||
case UART_SELECTION_UART1:
|
||||
this->hw_serial_ = &Serial1;
|
||||
libretiny::ensure_serial_handle(1);
|
||||
Serial1.begin(this->baud_rate_);
|
||||
break;
|
||||
#endif
|
||||
#if LT_HW_UART2
|
||||
case UART_SELECTION_UART2:
|
||||
this->hw_serial_ = &Serial2;
|
||||
libretiny::ensure_serial_handle(2);
|
||||
Serial2.begin(this->baud_rate_);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
this->hw_serial_ = &Serial;
|
||||
Serial.begin(this->baud_rate_);
|
||||
if (this->uart_ != UART_SELECTION_DEFAULT) {
|
||||
ESP_LOGW(TAG, " The chosen logger UART port is not available on this board."
|
||||
"The default port was used instead.");
|
||||
}
|
||||
break;
|
||||
#if defined(USE_LOGGER_UART_SELECTION_UART0) && LT_HW_UART0
|
||||
this->hw_serial_ = &Serial0;
|
||||
libretiny::ensure_serial_handle(0);
|
||||
Serial0.begin(this->baud_rate_);
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_UART1) && LT_HW_UART1
|
||||
this->hw_serial_ = &Serial1;
|
||||
libretiny::ensure_serial_handle(1);
|
||||
Serial1.begin(this->baud_rate_);
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_UART2) && LT_HW_UART2
|
||||
this->hw_serial_ = &Serial2;
|
||||
libretiny::ensure_serial_handle(2);
|
||||
Serial2.begin(this->baud_rate_);
|
||||
#else
|
||||
this->hw_serial_ = &Serial;
|
||||
Serial.begin(this->baud_rate_);
|
||||
if (CONFIGURED_UART_SELECTION != UART_SELECTION_DEFAULT) {
|
||||
ESP_LOGW(TAG, " The chosen logger UART port is not available on this board."
|
||||
"The default port was used instead.");
|
||||
}
|
||||
|
||||
#endif
|
||||
// change lt_log() port to match default Serial
|
||||
if (this->uart_ == UART_SELECTION_DEFAULT) {
|
||||
this->uart_ = (UARTSelection) (LT_UART_DEFAULT_SERIAL + 1);
|
||||
lt_log_set_port(LT_UART_DEFAULT_SERIAL);
|
||||
} else {
|
||||
lt_log_set_port(this->uart_ - 1);
|
||||
}
|
||||
lt_log_set_port(this->get_uart() - 1);
|
||||
}
|
||||
|
||||
global_logger = this;
|
||||
@@ -60,20 +43,6 @@ void Logger::pre_setup() {
|
||||
#endif
|
||||
}
|
||||
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_DEFAULT:
|
||||
return LOG_STR("DEFAULT");
|
||||
case UART_SELECTION_UART0:
|
||||
return LOG_STR("UART0");
|
||||
case UART_SELECTION_UART1:
|
||||
return LOG_STR("UART1");
|
||||
case UART_SELECTION_UART2:
|
||||
default:
|
||||
return LOG_STR("UART2");
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_LIBRETINY_CRASH_HANDLER
|
||||
bool crash_log_pending() { return libretiny::crash_handler_has_data(); }
|
||||
void log_crash_to_client() { libretiny::crash_handler_log(); }
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_LIBRETINY
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome::logger {
|
||||
|
||||
// Single write with newline already in buffer (added by caller)
|
||||
inline void HOT Logger::write_msg_(const char *msg, uint16_t len) { this->hw_serial_->write(msg, len); }
|
||||
|
||||
} // namespace esphome::logger
|
||||
|
||||
#endif
|
||||
@@ -26,20 +26,16 @@ void Logger::setup() {
|
||||
|
||||
void Logger::pre_setup() {
|
||||
if (this->baud_rate_ > 0) {
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
this->hw_serial_ = &Serial1;
|
||||
Serial1.begin(this->baud_rate_);
|
||||
break;
|
||||
case UART_SELECTION_UART1:
|
||||
this->hw_serial_ = &Serial2;
|
||||
Serial2.begin(this->baud_rate_);
|
||||
break;
|
||||
case UART_SELECTION_USB_CDC:
|
||||
this->hw_serial_ = &Serial;
|
||||
Serial.begin(this->baud_rate_);
|
||||
break;
|
||||
}
|
||||
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
this->hw_serial_ = &Serial;
|
||||
Serial.begin(this->baud_rate_);
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_UART1)
|
||||
this->hw_serial_ = &Serial2;
|
||||
Serial2.begin(this->baud_rate_);
|
||||
#else
|
||||
this->hw_serial_ = &Serial1;
|
||||
Serial1.begin(this->baud_rate_);
|
||||
#endif
|
||||
}
|
||||
global_logger = this;
|
||||
ESP_LOGI(TAG, "Log initialized");
|
||||
@@ -49,21 +45,6 @@ void Logger::pre_setup() {
|
||||
#endif
|
||||
}
|
||||
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
return LOG_STR("UART0");
|
||||
case UART_SELECTION_UART1:
|
||||
return LOG_STR("UART1");
|
||||
#ifdef USE_LOGGER_USB_CDC
|
||||
case UART_SELECTION_USB_CDC:
|
||||
return LOG_STR("USB_CDC");
|
||||
#endif
|
||||
default:
|
||||
return LOG_STR("UNKNOWN");
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_CRASH_HANDLER
|
||||
bool crash_log_pending() { return rp2::crash_handler_has_data(); }
|
||||
void log_crash_to_client() { rp2::crash_handler_log(); }
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_RP2
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome::logger {
|
||||
|
||||
// Single write with newline already in buffer (added by caller)
|
||||
inline void HOT Logger::write_msg_(const char *msg, uint16_t len) { this->hw_serial_->write(msg, len); }
|
||||
|
||||
} // namespace esphome::logger
|
||||
|
||||
#endif
|
||||
@@ -42,9 +42,9 @@ __attribute__((section(".noinit"))) struct {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "logger");
|
||||
|
||||
#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
|
||||
void Logger::cdc_loop_() {
|
||||
if (this->uart_ != UART_SELECTION_USB_CDC || this->uart_dev_ == nullptr) {
|
||||
if (this->uart_dev_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
static bool opened = false;
|
||||
@@ -74,26 +74,20 @@ void Logger::cdc_loop_() {
|
||||
void Logger::pre_setup() {
|
||||
#ifdef CONFIG_SERIAL
|
||||
if (this->baud_rate_ > 0) {
|
||||
static const struct device *uart_dev = nullptr;
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0: // NOLINT(bugprone-branch-clone)
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(ESPHOME_LOGGER_UART0_NODE));
|
||||
break;
|
||||
case UART_SELECTION_UART1:
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
|
||||
break;
|
||||
#ifdef USE_LOGGER_USB_CDC
|
||||
case UART_SELECTION_USB_CDC:
|
||||
const struct device *uart_dev = nullptr;
|
||||
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
#if defined(CONFIG_USB_DEVICE_STACK) || defined(CONFIG_USB_DEVICE_STACK_NEXT)
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
|
||||
if (device_is_ready(uart_dev)) {
|
||||
// Nothing can log this early, and the logger has no output without USB anyway
|
||||
zephyr::usb_device_enable();
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
#endif
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
|
||||
if (device_is_ready(uart_dev)) {
|
||||
// Nothing can log this early, and the logger has no output without USB anyway
|
||||
zephyr::usb_device_enable();
|
||||
}
|
||||
#endif
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_UART1)
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
|
||||
#else
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(ESPHOME_LOGGER_UART0_NODE));
|
||||
#endif
|
||||
if (device_is_ready(uart_dev)) {
|
||||
this->uart_dev_ = uart_dev;
|
||||
#if defined(USE_LOGGER_WAIT_FOR_CDC) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
@@ -114,14 +108,10 @@ void Logger::pre_setup() {
|
||||
char reason_buffer[zephyr::RESET_REASON_BUFFER_SIZE];
|
||||
const char *reset_reason = zephyr::get_reset_reason(std::span<char, zephyr::RESET_REASON_BUFFER_SIZE>(reason_buffer));
|
||||
ESP_LOGI(TAG, "Reset reason: %s", reset_reason);
|
||||
dump_crash_();
|
||||
this->dump_crash_();
|
||||
#endif
|
||||
#ifdef USE_CRASH_HANDLER
|
||||
#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
// Nobody reads a USB port this early; cdc_loop_() reports the dump once the port is opened
|
||||
if (this->uart_ == UART_SELECTION_USB_CDC)
|
||||
return;
|
||||
#endif
|
||||
#if defined(USE_CRASH_HANDLER) && !defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
|
||||
// Without a UART (baud_rate: 0, or one that did not come up) nothing reaches the console
|
||||
if (this->uart_dev_ != nullptr)
|
||||
log_crash_to_client();
|
||||
@@ -143,22 +133,7 @@ void HOT Logger::write_msg_(const char *msg, uint16_t len) {
|
||||
}
|
||||
}
|
||||
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
return LOG_STR("UART0");
|
||||
case UART_SELECTION_UART1:
|
||||
return LOG_STR("UART1");
|
||||
#ifdef USE_LOGGER_USB_CDC
|
||||
case UART_SELECTION_USB_CDC:
|
||||
return LOG_STR("USB_CDC");
|
||||
#endif
|
||||
default:
|
||||
return LOG_STR("UNKNOWN");
|
||||
}
|
||||
}
|
||||
|
||||
static const uint8_t REASON_BUF_SIZE = 32;
|
||||
static constexpr uint8_t REASON_BUF_SIZE = 32;
|
||||
|
||||
static const char *reason_to_str(unsigned int reason, char *buf) {
|
||||
switch (reason) {
|
||||
|
||||
@@ -44,19 +44,15 @@ void TaskLogBuffer::release_message_main_loop() {
|
||||
|
||||
bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
|
||||
const char *format, va_list args) {
|
||||
// First, calculate the exact length needed using a null buffer (no actual writing)
|
||||
// Measure on a copy; the caller's va_list formats the text later
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
int ret = vsnprintf(nullptr, 0, format, args_copy);
|
||||
int ret = task_log_cap_length(vsnprintf(nullptr, 0, format, args_copy));
|
||||
va_end(args_copy);
|
||||
|
||||
if (ret <= 0) {
|
||||
return false; // Formatting error or empty message
|
||||
}
|
||||
|
||||
// Calculate actual text length (capped to maximum size)
|
||||
static constexpr size_t MAX_TEXT_SIZE = 255;
|
||||
size_t text_length = (static_cast<size_t>(ret) > MAX_TEXT_SIZE) ? MAX_TEXT_SIZE : ret;
|
||||
uint16_t text_length = ret;
|
||||
|
||||
// Calculate total size needed (header + text length + null terminator)
|
||||
size_t total_size = sizeof(LogMessage) + text_length + 1;
|
||||
@@ -69,24 +65,10 @@ bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uin
|
||||
return false; // Failed to acquire memory
|
||||
}
|
||||
|
||||
// Set up the message header in the acquired memory
|
||||
LogMessage *msg = static_cast<LogMessage *>(acquired_memory);
|
||||
msg->level = level;
|
||||
msg->tag = tag;
|
||||
msg->line = line;
|
||||
|
||||
// Store the thread name now instead of waiting until main loop processing
|
||||
// This avoids crashes if the task completes or is deleted between when this message
|
||||
// is enqueued and when it's processed by the main loop
|
||||
if (thread_name != nullptr) {
|
||||
strncpy(msg->thread_name, thread_name, sizeof(msg->thread_name) - 1);
|
||||
msg->thread_name[sizeof(msg->thread_name) - 1] = '\0'; // Ensure null termination
|
||||
} else {
|
||||
msg->thread_name[0] = '\0'; // Empty string if no thread name
|
||||
}
|
||||
fill_task_log_header(msg, level, tag, line, thread_name);
|
||||
|
||||
// Format the message text directly into the acquired memory
|
||||
// We add 1 to text_length to ensure space for null terminator during formatting
|
||||
char *text_area = msg->text_data();
|
||||
ret = vsnprintf(text_area, text_length + 1, format, args);
|
||||
|
||||
@@ -96,12 +78,7 @@ bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uin
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove trailing newlines
|
||||
while (text_length > 0 && text_area[text_length - 1] == '\n') {
|
||||
text_length--;
|
||||
}
|
||||
|
||||
msg->text_length = text_length;
|
||||
msg->text_length = trim_trailing_newlines(text_area, text_length);
|
||||
// Complete the send operation with the acquired memory
|
||||
result = xRingbufferSendComplete(this->handle_(), acquired_memory);
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/ringbuf.h>
|
||||
|
||||
#include "task_log_message.h"
|
||||
|
||||
namespace esphome::logger {
|
||||
|
||||
/**
|
||||
@@ -32,19 +34,7 @@ namespace esphome::logger {
|
||||
*/
|
||||
class TaskLogBuffer {
|
||||
public:
|
||||
// Structure for a log message header (text data follows immediately after)
|
||||
struct LogMessage {
|
||||
const char *tag; // We store the pointer, assuming tags are static
|
||||
char thread_name[16]; // Store thread name directly (only used for non-main threads)
|
||||
uint16_t text_length; // Length of the message text (up to ~64KB)
|
||||
uint16_t line; // Source code line number
|
||||
uint8_t level; // Log level (0-7)
|
||||
|
||||
// Methods for accessing message contents
|
||||
inline char *text_data() { return reinterpret_cast<char *>(this) + sizeof(LogMessage); }
|
||||
|
||||
inline const char *text_data() const { return reinterpret_cast<const char *>(this) + sizeof(LogMessage); }
|
||||
};
|
||||
using LogMessage = TaskLogMessage;
|
||||
|
||||
TaskLogBuffer();
|
||||
// No destructor: Logger is never destroyed
|
||||
|
||||
@@ -71,18 +71,8 @@ bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uin
|
||||
|
||||
LogMessage &msg = this->slots_[slot_index];
|
||||
|
||||
// Fill in the message header
|
||||
msg.level = level;
|
||||
msg.tag = tag;
|
||||
msg.line = line;
|
||||
|
||||
// Store the thread name now to avoid crashes if thread exits before processing
|
||||
if (thread_name != nullptr) {
|
||||
strncpy(msg.thread_name, thread_name, sizeof(msg.thread_name) - 1);
|
||||
msg.thread_name[sizeof(msg.thread_name) - 1] = '\0';
|
||||
} else {
|
||||
msg.thread_name[0] = '\0';
|
||||
}
|
||||
// Copied now; the thread may exit before processing
|
||||
fill_task_log_header(&msg, level, tag, line, thread_name);
|
||||
|
||||
// Format the message text
|
||||
int ret = vsnprintf(msg.text, sizeof(msg.text), format, args);
|
||||
@@ -94,10 +84,7 @@ bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uin
|
||||
msg.text_length = static_cast<uint16_t>(std::min(static_cast<size_t>(ret), sizeof(msg.text) - 1));
|
||||
}
|
||||
|
||||
// Remove trailing newlines
|
||||
while (msg.text_length > 0 && msg.text[msg.text_length - 1] == '\n') {
|
||||
msg.text_length--;
|
||||
}
|
||||
msg.text_length = trim_trailing_newlines(msg.text, msg.text_length);
|
||||
msg.text[msg.text_length] = '\0';
|
||||
|
||||
// Commit the slot
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
#include <cstring>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "task_log_message.h"
|
||||
|
||||
namespace esphome::logger {
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,13 +13,6 @@ TaskLogBuffer::TaskLogBuffer() {
|
||||
this->mutex_ = xSemaphoreCreateMutex();
|
||||
}
|
||||
|
||||
TaskLogBuffer::~TaskLogBuffer() {
|
||||
if (this->mutex_ != nullptr) {
|
||||
vSemaphoreDelete(this->mutex_);
|
||||
this->mutex_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
size_t TaskLogBuffer::available_contiguous_space_() const {
|
||||
if (this->head_ >= this->tail_) {
|
||||
// head is ahead of or equal to tail
|
||||
@@ -85,19 +78,15 @@ void TaskLogBuffer::release_message_main_loop() {
|
||||
|
||||
bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
|
||||
const char *format, va_list args) {
|
||||
// First, calculate the exact length needed using a null buffer (no actual writing)
|
||||
// Measure on a copy; the caller's va_list formats the text later
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
int ret = vsnprintf(nullptr, 0, format, args_copy);
|
||||
int ret = task_log_cap_length(vsnprintf(nullptr, 0, format, args_copy));
|
||||
va_end(args_copy);
|
||||
|
||||
if (ret <= 0) {
|
||||
return false; // Formatting error or empty message
|
||||
}
|
||||
|
||||
// Calculate actual text length (capped to maximum size)
|
||||
static constexpr size_t MAX_TEXT_SIZE = 255;
|
||||
size_t text_length = (static_cast<size_t>(ret) > MAX_TEXT_SIZE) ? MAX_TEXT_SIZE : ret;
|
||||
uint16_t text_length = ret;
|
||||
|
||||
// Calculate total size needed (header + text length + null terminator)
|
||||
size_t total_size = message_total_size(text_length);
|
||||
@@ -128,17 +117,7 @@ bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uin
|
||||
|
||||
// Write message header
|
||||
LogMessage *msg = reinterpret_cast<LogMessage *>(this->storage_ + write_pos);
|
||||
msg->level = level;
|
||||
msg->tag = tag;
|
||||
msg->line = line;
|
||||
|
||||
// Store the thread name now to avoid crashes if task is deleted before processing
|
||||
if (thread_name != nullptr) {
|
||||
strncpy(msg->thread_name, thread_name, sizeof(msg->thread_name) - 1);
|
||||
msg->thread_name[sizeof(msg->thread_name) - 1] = '\0';
|
||||
} else {
|
||||
msg->thread_name[0] = '\0';
|
||||
}
|
||||
fill_task_log_header(msg, level, tag, line, thread_name);
|
||||
|
||||
// Format the message text directly into the buffer
|
||||
char *text_area = msg->text_data();
|
||||
@@ -150,9 +129,7 @@ bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uin
|
||||
}
|
||||
|
||||
// Trim before storing so the next message reuses the freed bytes
|
||||
while (text_length > 0 && text_area[text_length - 1] == '\n') {
|
||||
text_length--;
|
||||
}
|
||||
text_length = trim_trailing_newlines(text_area, text_length);
|
||||
text_area[text_length] = '\0';
|
||||
msg->text_length = text_length;
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
#include <semphr.h>
|
||||
#include <task.h>
|
||||
|
||||
#include "task_log_message.h"
|
||||
|
||||
namespace esphome::logger {
|
||||
|
||||
/**
|
||||
@@ -42,25 +44,14 @@ namespace esphome::logger {
|
||||
*/
|
||||
class TaskLogBuffer {
|
||||
public:
|
||||
// Structure for a log message header (text data follows immediately after)
|
||||
struct LogMessage {
|
||||
const char *tag; // We store the pointer, assuming tags are static
|
||||
char thread_name[16]; // Store thread name directly (only used for non-main threads)
|
||||
uint16_t text_length; // Length of the message text (up to ~64KB)
|
||||
uint16_t line; // Source code line number
|
||||
uint8_t level; // Log level (0-7)
|
||||
|
||||
// Methods for accessing message contents
|
||||
inline char *text_data() { return reinterpret_cast<char *>(this) + sizeof(LogMessage); }
|
||||
inline const char *text_data() const { return reinterpret_cast<const char *>(this) + sizeof(LogMessage); }
|
||||
};
|
||||
using LogMessage = TaskLogMessage;
|
||||
|
||||
// Padding marker level to indicate wrap-around point (stored in LogMessage.level field)
|
||||
// Valid log levels are 0-7, so 0xFF cannot be a real message
|
||||
static constexpr uint8_t PADDING_MARKER_LEVEL = 0xFF;
|
||||
|
||||
TaskLogBuffer();
|
||||
~TaskLogBuffer();
|
||||
// No destructor: Logger is never destroyed
|
||||
|
||||
// NOT thread-safe - borrow a message from the buffer, only call from main loop
|
||||
bool borrow_message_main_loop(LogMessage *&message, uint16_t &text_length);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
namespace esphome::logger {
|
||||
|
||||
__thread bool non_main_task_recursion_guard_; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
__thread bool non_main_task_recursion_guard; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
|
||||
|
||||
@@ -29,29 +29,21 @@ TaskLogBuffer::TaskLogBuffer() {
|
||||
|
||||
bool TaskLogBuffer::send_message_thread_safe(uint8_t level, const char *tag, uint16_t line, const char *thread_name,
|
||||
const char *format, va_list args) {
|
||||
// First, calculate the exact length needed using a null buffer (no actual writing)
|
||||
// Measure on a copy; the caller's va_list formats the text later
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
int ret = vsnprintf(nullptr, 0, format, args_copy);
|
||||
int ret = task_log_cap_length(vsnprintf(nullptr, 0, format, args_copy));
|
||||
va_end(args_copy);
|
||||
|
||||
if (ret <= 0) {
|
||||
return false; // Formatting error or empty message
|
||||
}
|
||||
|
||||
// Calculate actual text length (capped to maximum size)
|
||||
static constexpr size_t MAX_TEXT_SIZE = 255;
|
||||
size_t text_length = (static_cast<size_t>(ret) > MAX_TEXT_SIZE) ? MAX_TEXT_SIZE : ret;
|
||||
uint16_t text_length = ret;
|
||||
size_t total_size = total_size_in_32bit_words(text_length);
|
||||
auto *msg = reinterpret_cast<LogMessage *>(mpsc_pbuf_alloc(&this->log_buffer_, total_size, K_NO_WAIT));
|
||||
if (msg == nullptr) {
|
||||
return false;
|
||||
}
|
||||
msg->level = level;
|
||||
msg->tag = tag;
|
||||
msg->line = line;
|
||||
strncpy(msg->thread_name, thread_name, sizeof(msg->thread_name) - 1);
|
||||
msg->thread_name[sizeof(msg->thread_name) - 1] = '\0'; // Ensure null termination
|
||||
fill_task_log_header(msg, level, tag, line, thread_name);
|
||||
|
||||
// Format the message text directly into the acquired memory
|
||||
// We add 1 to text_length to ensure space for null terminator during formatting
|
||||
@@ -90,11 +82,7 @@ bool TaskLogBuffer::borrow_message_main_loop(LogMessage *&message, uint16_t &tex
|
||||
// we claimed buffer already, const_cast is safe here
|
||||
message = const_cast<LogMessage *>(reinterpret_cast<const LogMessage *>(this->current_token_));
|
||||
|
||||
text_length = message->text_length;
|
||||
// Remove trailing newlines
|
||||
while (text_length > 0 && message->text_data()[text_length - 1] == '\n') {
|
||||
text_length--;
|
||||
}
|
||||
text_length = trim_trailing_newlines(message->text_data(), message->text_length);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
#include "esphome/core/helpers.h"
|
||||
#include <zephyr/sys/mpsc_pbuf.h>
|
||||
|
||||
#include "task_log_message.h"
|
||||
|
||||
namespace esphome::logger {
|
||||
|
||||
// "0x" + 2 hex digits per byte + '\0'
|
||||
static constexpr size_t MAX_POINTER_REPRESENTATION = 2 + sizeof(void *) * 2 + 1;
|
||||
|
||||
extern __thread bool non_main_task_recursion_guard_; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
extern __thread bool non_main_task_recursion_guard; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
|
||||
|
||||
@@ -34,7 +36,6 @@ class TaskLogBuffer {
|
||||
inline char *text_data() { return reinterpret_cast<char *>(this) + sizeof(LogMessage); }
|
||||
};
|
||||
TaskLogBuffer();
|
||||
~TaskLogBuffer() = default;
|
||||
|
||||
// Check if there are messages ready to be processed using an atomic counter for performance
|
||||
inline bool HOT has_messages() { return mpsc_pbuf_is_pending(&this->log_buffer_); }
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
namespace esphome::logger {
|
||||
|
||||
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||
// Header of a variable-size task log message; the text follows it
|
||||
struct TaskLogMessage {
|
||||
const char *tag; // We store the pointer, assuming tags are static
|
||||
char thread_name[16]; // Copied, the task may be gone before the main loop reads it
|
||||
uint16_t text_length;
|
||||
uint16_t line;
|
||||
uint8_t level;
|
||||
|
||||
inline char *text_data() { return reinterpret_cast<char *>(this) + sizeof(TaskLogMessage); }
|
||||
};
|
||||
#endif
|
||||
|
||||
#if defined(USE_ESP32) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
|
||||
static constexpr int TASK_LOG_MAX_TEXT_SIZE = 255;
|
||||
|
||||
inline int task_log_cap_length(int ret) { return ret > TASK_LOG_MAX_TEXT_SIZE ? TASK_LOG_MAX_TEXT_SIZE : ret; }
|
||||
#endif
|
||||
|
||||
template<typename Message>
|
||||
inline void fill_task_log_header(Message *msg, uint8_t level, const char *tag, uint16_t line, const char *thread_name) {
|
||||
msg->level = level;
|
||||
msg->tag = tag;
|
||||
msg->line = line;
|
||||
if (thread_name != nullptr) {
|
||||
strncpy(msg->thread_name, thread_name, sizeof(msg->thread_name) - 1);
|
||||
msg->thread_name[sizeof(msg->thread_name) - 1] = '\0';
|
||||
} else {
|
||||
msg->thread_name[0] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
inline uint16_t trim_trailing_newlines(const char *text, uint16_t length) {
|
||||
while (length > 0 && text[length - 1] == '\n')
|
||||
length--;
|
||||
return length;
|
||||
}
|
||||
|
||||
} // namespace esphome::logger
|
||||
|
||||
#endif // USE_ESPHOME_TASK_LOG_BUFFER
|
||||
+25
-3
@@ -151,6 +151,7 @@
|
||||
#define USE_LINE_READER
|
||||
#define USE_LOCK
|
||||
#define USE_LOGGER
|
||||
#define USE_LOGGER_CONSOLE
|
||||
#define USE_LOGGER_LEVEL_LISTENERS
|
||||
#define USE_LOGGER_RUNTIME_TAG_LEVELS
|
||||
#define USE_LVGL
|
||||
@@ -758,16 +759,26 @@
|
||||
#if defined(USE_ESP32_VARIANT_ESP32S2)
|
||||
#define USE_LOGGER_USB_CDC
|
||||
#define USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
|
||||
#elif defined(USE_ESP32_VARIANT_ESP32H21)
|
||||
#define USE_LOGGER_USB_SERIAL_JTAG
|
||||
#define USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
|
||||
#define ESPHOME_LOGGER_UART_NAME "USB_SERIAL_JTAG"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_SERIAL_JTAG
|
||||
#elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C5) || \
|
||||
defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32C61) || defined(USE_ESP32_VARIANT_ESP32H2) || \
|
||||
defined(USE_ESP32_VARIANT_ESP32H21) || defined(USE_ESP32_VARIANT_ESP32H4) || defined(USE_ESP32_VARIANT_ESP32P4) || \
|
||||
defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32S31)
|
||||
defined(USE_ESP32_VARIANT_ESP32H4) || defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S3) || \
|
||||
defined(USE_ESP32_VARIANT_ESP32S31)
|
||||
#define USE_LOGGER_USB_CDC
|
||||
#define USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
#define USE_LOGGER_USB_SERIAL_JTAG
|
||||
#define USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
|
||||
#define ESPHOME_LOGGER_UART_NAME "USB_SERIAL_JTAG"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_SERIAL_JTAG
|
||||
#else
|
||||
#define USE_LOGGER_UART_SELECTION_UART0
|
||||
#define ESPHOME_LOGGER_UART_NAME "UART0"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_UART0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -781,6 +792,9 @@
|
||||
#define USE_WIFI_SCAN_RESULTS_LOCK
|
||||
#define USE_ESP8266_LOGGER_SERIAL
|
||||
#define USE_ESP8266_LOGGER_SERIAL1
|
||||
#define USE_LOGGER_UART_SELECTION_UART0
|
||||
#define ESPHOME_LOGGER_UART_NAME "UART0"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_UART0
|
||||
#define USE_ESP8266_PREFERENCES_FLASH
|
||||
#define ESP8266_PREFERENCES_FLASH_WORDS 128
|
||||
#define ESP8266_PREFERENCES_RTC_EBOOT_WORDS 32
|
||||
@@ -826,6 +840,8 @@
|
||||
#define USE_I2C
|
||||
#define USE_LOGGER_USB_CDC
|
||||
#define USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
|
||||
#define USE_SOCKET_IMPL_LWIP_TCP
|
||||
#define USE_RP2040_BLE
|
||||
#define USE_RP2_BLE_TRACKER
|
||||
@@ -856,6 +872,9 @@
|
||||
#if defined(USE_BK72XX) || defined(USE_RTL87XX) || defined(USE_LN882X)
|
||||
#define USE_LIBRETINY_CRASH_HANDLER
|
||||
#endif
|
||||
#define USE_LOGGER_UART_SELECTION_DEFAULT
|
||||
#define ESPHOME_LOGGER_UART_NAME "DEFAULT"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_DEFAULT
|
||||
#ifdef USE_BK72XX
|
||||
#define USE_REMOTE_RECEIVER_TIMER_SAMPLING
|
||||
#endif
|
||||
@@ -910,8 +929,11 @@
|
||||
#define ESPHOME_TASK_LOG_BUFFER_SIZE 768
|
||||
#define USE_LOGGER_EARLY_MESSAGE
|
||||
#define USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
|
||||
#define USE_LOGGER_USB_CDC
|
||||
#define USE_LOGGER_WAIT_FOR_CDC
|
||||
#define USE_LOGGER_ZEPHYR_CDC_LOOP
|
||||
#define USE_NRF52_DFU
|
||||
#define USE_NRF52_REG0_VOUT 5
|
||||
#define USE_NRF52_UICR_ERASE
|
||||
|
||||
@@ -337,6 +337,57 @@ size_t truncate_name_to(char *buffer, size_t buffer_size, const char *name, size
|
||||
|
||||
// Parsing & formatting
|
||||
|
||||
// A plain decimal converts exactly with one division: the digits accumulate
|
||||
// into an integer that stays below 2^24 and the fraction's scale is a power of
|
||||
// ten up to 1e9 (2^9 * 5^9), so both convert to float exactly and IEEE division
|
||||
// rounds the quotient correctly, the same result strtof produces.
|
||||
static constexpr uint32_t PARSE_FLOAT_MANTISSA_LIMIT = 1000000; // one more digit stays below 2^24
|
||||
static constexpr uint32_t PARSE_FLOAT_MAX_FRACTION_DIGITS = 9;
|
||||
|
||||
optional<float> parse_float(const char *str) {
|
||||
const char *p = str;
|
||||
const bool negative = *p == '-';
|
||||
if (negative || *p == '+')
|
||||
p++;
|
||||
const char *digits = p;
|
||||
const char *dot = nullptr;
|
||||
uint32_t mantissa = 0;
|
||||
for (;; p++) {
|
||||
const uint32_t d = static_cast<unsigned char>(*p) - '0';
|
||||
if (d > 9) {
|
||||
if (*p == '.' && dot == nullptr) {
|
||||
dot = p;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (mantissa >= PARSE_FLOAT_MANTISSA_LIMIT)
|
||||
break;
|
||||
mantissa = mantissa * 10 + d;
|
||||
}
|
||||
const uint32_t fraction_digits = dot != nullptr ? p - dot - 1 : 0;
|
||||
const bool any_digit = p - digits > (dot != nullptr ? 1 : 0);
|
||||
if (*p == '\0' && any_digit && fraction_digits <= PARSE_FLOAT_MAX_FRACTION_DIGITS) {
|
||||
// The mantissa is exact in a float, so the sign goes on before the one rounding step
|
||||
float value = static_cast<float>(mantissa);
|
||||
if (negative)
|
||||
value = -value;
|
||||
if (fraction_digits != 0) {
|
||||
uint32_t scale = 10;
|
||||
for (uint32_t i = 1; i < fraction_digits; i++)
|
||||
scale *= 10;
|
||||
value /= static_cast<float>(scale);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
// Exponents, hex, inf, nan, leading whitespace and longer inputs take the libc path.
|
||||
char *end = nullptr;
|
||||
const float value = ::strtof(str, &end);
|
||||
if (end == str || *end != '\0' || value == HUGE_VALF)
|
||||
return {};
|
||||
return value;
|
||||
}
|
||||
|
||||
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count) {
|
||||
size_t chars = std::min(length, 2 * count);
|
||||
for (size_t i = 2 * count - chars; i < 2 * count; i++, str++) {
|
||||
|
||||
+2
-21
@@ -25,7 +25,6 @@
|
||||
#include <strings.h>
|
||||
|
||||
#include "esphome/core/optional.h"
|
||||
#include "esphome/core/parse_float.h"
|
||||
#include "esphome/core/time_conversion.h"
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
@@ -1034,26 +1033,6 @@ inline float pow10_int(int8_t exp) {
|
||||
return result;
|
||||
}
|
||||
|
||||
/// 10^n, exact while it fits in T.
|
||||
template<typename T> constexpr T pow10_exact(uint32_t n) {
|
||||
T result = 1;
|
||||
T base = 10;
|
||||
for (;;) {
|
||||
if ((n & 1) != 0)
|
||||
result *= base;
|
||||
n >>= 1;
|
||||
if (n == 0)
|
||||
return result;
|
||||
base *= base;
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether c is '0' to '9'.
|
||||
constexpr bool is_digit(char c) { return static_cast<uint8_t>(c - '0') <= 9; }
|
||||
|
||||
/// Whether c is C-locale white space, without isspace's ctype table.
|
||||
constexpr bool is_space_c(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); }
|
||||
|
||||
/// Remap \p value from the range (\p min, \p max) to (\p min_out, \p max_out).
|
||||
template<typename T, typename U> T remap(U value, U min, U max, T min_out, T max_out) {
|
||||
return (value - min) * (max_out - min_out) / (max - min) + min_out;
|
||||
@@ -1594,6 +1573,8 @@ template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T
|
||||
optional<T> parse_number(const std::string &str) {
|
||||
return parse_number<T>(str.c_str());
|
||||
}
|
||||
/// Parse a decimal floating-point number; plain decimals skip the libc parser.
|
||||
optional<float> parse_float(const char *str);
|
||||
/// Parse a decimal floating-point number from a null-terminated string.
|
||||
template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0> optional<T> parse_number(const char *str) {
|
||||
return parse_float(str);
|
||||
|
||||
@@ -1,372 +0,0 @@
|
||||
// picolibc 1.8.10's strtof, bit for bit, without libc's strtod: decimals round to 9 digits, then to the nearest float.
|
||||
// Float bits are built from integers, so chips that flush denormals give the same result.
|
||||
|
||||
#include "esphome/core/parse_float.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
|
||||
namespace esphome {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint32_t FLOAT_SIGN_BIT = 0x80000000, FLOAT_INF_BITS = 0x7F800000;
|
||||
constexpr uint32_t FLOAT_NAN_BITS = 0x7FC00000; // nanf(""), which strtof returns for every NaN
|
||||
|
||||
constexpr uint32_t DECIMAL_DIGITS = 9, HEX_DIGITS = 8; // digits kept before the rest only round
|
||||
constexpr int32_t EXPONENT_CAP = 224; // picolibc stops adding exponent digits once the exponent reaches this
|
||||
// 0.<digits> * 10^exp10 is zero at or below MIN_EXP10 and infinity at or above MAX_EXP10, as in picolibc
|
||||
constexpr int32_t MIN_EXP10 = -46, MAX_EXP10 = 40;
|
||||
// Past this, one more digit could pass 2^24
|
||||
constexpr uint32_t PLAIN_MANTISSA_LIMIT = 1677720;
|
||||
|
||||
// Bits of the float nearest to (m + sticky fraction) * 2^e, ties to even; m > 0
|
||||
uint32_t round_to_float_bits(uint64_t m, int32_t e, bool sticky) {
|
||||
const int32_t msb = e + static_cast<int32_t>(std::bit_width(m)) - 1; // the value is in [2^msb, 2^(msb + 1))
|
||||
if (msb >= 128)
|
||||
return FLOAT_INF_BITS;
|
||||
if (msb < -150) // below half the smallest float
|
||||
return 0;
|
||||
// The unit of the float's last bit: 2^(msb - 23), or 2^-149 for a denormal
|
||||
const int32_t shift = std::max<int32_t>(msb - 23, -149) - e; // at most 64
|
||||
uint64_t q;
|
||||
if (shift <= 0) {
|
||||
q = m << -shift;
|
||||
} else {
|
||||
q = shift < 64 ? m >> shift : 0;
|
||||
const uint64_t rem = shift < 64 ? m & ((uint64_t{1} << shift) - 1) : m;
|
||||
const uint64_t half = uint64_t{1} << (shift - 1);
|
||||
if (rem > half || (rem == half && (sticky || (q & 1) != 0)))
|
||||
q++;
|
||||
}
|
||||
if (msb < -126)
|
||||
return static_cast<uint32_t>(q); // 2^23 here is the smallest normal float, as it should be
|
||||
// Rounding up past the largest float gives exactly FLOAT_INF_BITS
|
||||
return (static_cast<uint32_t>(msb + 127) << 23) + static_cast<uint32_t>(q - (uint64_t{1} << 23));
|
||||
}
|
||||
|
||||
// The decimal exponents that reach decimal_to_float_bits: 1 to 9 digits and MIN_EXP10 < digits + e10 < MAX_EXP10
|
||||
constexpr int32_t MIN_E10 = MIN_EXP10 + 1 - static_cast<int32_t>(DECIMAL_DIGITS), MAX_E10 = MAX_EXP10 - 2;
|
||||
// Top 64 bits of 10^(8k), rounded down: 10^(8k) = entry * 2^(floor_log2_pow10(8k) - 63), exact for k = 0..3
|
||||
constexpr int32_t POW10_BY_8_FIRST = MIN_E10 >> 3, POW10_BY_8_LAST = MAX_E10 >> 3;
|
||||
constexpr uint64_t POW10_BY_8[] PROGMEM = {
|
||||
0xFB158592BE068D2E, 0xBB127C53B17EC159, 0x8B61313BBABCE2C6, 0xCFB11EAD453994BA,
|
||||
0x9ABE14CD44753B52, 0xE69594BEC44DE15B, 0xABCC77118461CEFC, 0x8000000000000000,
|
||||
0xBEBC200000000000, 0x8E1BC9BF04000000, 0xD3C21BCECCEDA100, 0x9DC5ADA82B70B59D,
|
||||
};
|
||||
static_assert(POW10_BY_8_FIRST == -7 && POW10_BY_8_LAST == 4 &&
|
||||
sizeof(POW10_BY_8) / sizeof(POW10_BY_8[0]) == POW10_BY_8_LAST - POW10_BY_8_FIRST + 1,
|
||||
"POW10_BY_8 must cover MIN_E10..MAX_E10");
|
||||
|
||||
// floor(q * log2(10)), for |q| < 100
|
||||
constexpr int32_t floor_log2_pow10(int32_t q) { return (q * 217706) >> 16; }
|
||||
|
||||
// The top 64 bits of a * b, rounded down
|
||||
uint64_t mul_high(uint64_t a, uint64_t b) {
|
||||
const uint64_t a_lo = static_cast<uint32_t>(a), a_hi = a >> 32;
|
||||
const uint64_t b_lo = static_cast<uint32_t>(b), b_hi = b >> 32;
|
||||
const uint64_t hi_lo = a_hi * b_lo, lo_hi = a_lo * b_hi;
|
||||
const uint64_t middle = ((a_lo * b_lo) >> 32) + static_cast<uint32_t>(hi_lo) + static_cast<uint32_t>(lo_hi);
|
||||
return a_hi * b_hi + (hi_lo >> 32) + (lo_hi >> 32) + (middle >> 32);
|
||||
}
|
||||
|
||||
// Cold: a decimal within 16 units of a midpoint is an exact tie when m10 * 5^e10 is an integer
|
||||
__attribute__((noinline, cold)) uint32_t tie_float_bits(uint32_t m10, int32_t e10, uint32_t bits) {
|
||||
if (e10 >= 0) {
|
||||
if (e10 <= 14) // m10 * 5^14 stays below 2^64
|
||||
return round_to_float_bits(m10 * (pow10_exact<uint64_t>(e10) >> e10), e10, false);
|
||||
} else if (e10 >= -13) {
|
||||
const auto pow5 = static_cast<uint32_t>(pow10_exact<uint64_t>(-e10) >> -e10);
|
||||
if (m10 % pow5 == 0)
|
||||
return round_to_float_bits(m10 / pow5, e10, false);
|
||||
}
|
||||
return bits;
|
||||
}
|
||||
|
||||
// The float nearest to m10 * 10^e10 (0 < m10 <= 10^9). The product is under 4 units low; checked for all 85 billion
|
||||
// (m10, e10), only exact ties and one non-tie (6 units off, keeps its float) come within 34 units of a midpoint.
|
||||
uint32_t decimal_to_float_bits(uint32_t m10, int32_t e10) {
|
||||
// Both operands are exact floats, so the one float operation gives the nearest float
|
||||
const uint32_t power = e10 < 0 ? -e10 : e10;
|
||||
if (m10 < (1u << 24) && power <= 10) {
|
||||
const auto value = static_cast<float>(m10);
|
||||
// 10^10 overflows uint32_t
|
||||
const float factor = power == 10 ? 1e10f : static_cast<float>(pow10_exact<uint32_t>(power));
|
||||
return bit_cast<uint32_t>(e10 < 0 ? value / factor : value * factor);
|
||||
}
|
||||
const int32_t k8 = e10 >> 3; // rounds down
|
||||
const uint64_t x = uint64_t{m10} * pow10_exact<uint32_t>(e10 & 7);
|
||||
const auto zeros = static_cast<int32_t>(std::countl_zero(x));
|
||||
uint64_t m = x << zeros;
|
||||
int32_t e = -zeros;
|
||||
if (k8 == 0)
|
||||
return round_to_float_bits(m, e, false); // exact
|
||||
m = mul_high(m, POW10_BY_8[k8 - POW10_BY_8_FIRST]);
|
||||
e += floor_log2_pow10(8 * k8) + 1;
|
||||
if ((m >> 63) == 0) {
|
||||
m <<= 1;
|
||||
e--;
|
||||
}
|
||||
const uint32_t bits = round_to_float_bits(m, e, false);
|
||||
// The bits below the float's last one, and the midpoint among them
|
||||
const int32_t shift = std::max<int32_t>(40, -149 - e);
|
||||
if (shift <= 64) {
|
||||
const uint64_t low = shift < 64 ? m & ((uint64_t{1} << shift) - 1) : m;
|
||||
const uint64_t half = uint64_t{1} << (shift - 1);
|
||||
if (low - half + 16 > 32) // |low - half| > 16
|
||||
return bits;
|
||||
}
|
||||
return tie_float_bits(m10, e10, bits);
|
||||
}
|
||||
|
||||
uint32_t no_number(const char *str, const char **end, bool *negative) {
|
||||
*end = str;
|
||||
*negative = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The rest of an "nan" or "inf" after its first letter at p; returns the end, or nullptr for no number
|
||||
const char *read_inf_nan(const char *p) {
|
||||
const auto lower = [](char c) { return static_cast<char>(c | 0x20); };
|
||||
if (lower(*p) == 'n') {
|
||||
if (lower(p[1]) != 'a' || lower(p[2]) != 'n')
|
||||
return nullptr;
|
||||
p += 3;
|
||||
if (*p != '(')
|
||||
return p;
|
||||
// "nan(" must be closed after letters, digits and '_'
|
||||
do {
|
||||
p++;
|
||||
} while (static_cast<uint8_t>(lower(*p) - 'a') < 26 || is_digit(*p) || *p == '_');
|
||||
return *p == ')' ? p + 1 : nullptr;
|
||||
}
|
||||
if (lower(p[1]) != 'n' || lower(p[2]) != 'f')
|
||||
return nullptr;
|
||||
p += 3;
|
||||
if (lower(*p) != 'i')
|
||||
return p;
|
||||
// Past "infi", only "infinity" is a number
|
||||
return lower(p[1]) == 'n' && lower(p[2]) == 'i' && lower(p[3]) == 't' && lower(p[4]) == 'y' ? p + 5 : nullptr;
|
||||
}
|
||||
|
||||
// Reads a binary exponent "p" or decimal exponent "e" at p into *exp; returns the end of the number
|
||||
const char *read_exponent(const char *p, char letter, int32_t *exp) {
|
||||
if ((*p | 0x20) != letter)
|
||||
return p;
|
||||
const char *q = p + 1;
|
||||
const bool negative = *q == '-';
|
||||
if (negative || *q == '+')
|
||||
q++;
|
||||
if (!is_digit(*q))
|
||||
return p;
|
||||
int32_t value = 0;
|
||||
for (; is_digit(*q); q++) {
|
||||
if (value < EXPONENT_CAP)
|
||||
value = value * 10 + (*q - '0');
|
||||
}
|
||||
*exp += negative ? -value : value;
|
||||
return q;
|
||||
}
|
||||
|
||||
// Hex digits after the "x" at p, with the point and decimal exponent read so far (no non-zero digit yet)
|
||||
__attribute__((noinline, cold)) uint32_t read_hex(const char *p, bool dot, int32_t exp, const char **end) {
|
||||
uint32_t m = 0, kept = 0; // kept: digits in m from its first non-zero one
|
||||
bool sticky = false;
|
||||
for (;; p++) {
|
||||
const char lower = static_cast<char>(*p | 0x20);
|
||||
uint32_t digit = static_cast<uint8_t>(*p - '0');
|
||||
if (static_cast<uint8_t>(lower - 'a') <= 5) {
|
||||
digit = static_cast<uint8_t>(lower - 'a') + 10;
|
||||
} else if (digit > 9) {
|
||||
digit = 16; // picolibc takes ':' to '?' as 10 to 15; we stop there like newlib
|
||||
}
|
||||
if (digit < 16) {
|
||||
if (kept < HEX_DIGITS) {
|
||||
if (dot)
|
||||
exp--;
|
||||
m = m * 16 + digit;
|
||||
kept += m != 0 ? 1 : 0;
|
||||
} else {
|
||||
if (!dot)
|
||||
exp++;
|
||||
sticky |= digit != 0;
|
||||
}
|
||||
} else if (*p == '.' && !dot) {
|
||||
dot = true;
|
||||
} else if (lower != 'x' || m != 0) { // picolibc takes another "x" while the value is still zero
|
||||
break;
|
||||
}
|
||||
}
|
||||
exp *= 4;
|
||||
*end = read_exponent(p, 'p', &exp);
|
||||
return m == 0 ? 0 : round_to_float_bits(m, exp, sticky);
|
||||
}
|
||||
|
||||
// picolibc's conv_flt for a float from the first digit or point at p: returns the bits; *end is str for no number
|
||||
__attribute__((noinline)) uint32_t read_decimal(const char *str, const char *p, const char **end) {
|
||||
uint32_t m = 0, kept = 0; // the first (up to 9) significant digits, and how many from the first non-zero one
|
||||
int32_t exp = 0; // the value is m * 10^exp
|
||||
bool dot = false, any = false;
|
||||
uint32_t overflow = 0; // digits past the kept ones: 0 none, 1 below half, 2 half, 3 above half
|
||||
bool full = false; // a digit past the kept ones was read
|
||||
for (;; p++) {
|
||||
const uint32_t digit = static_cast<uint8_t>(*p - '0');
|
||||
if (digit <= 9) {
|
||||
any = true;
|
||||
if (kept < DECIMAL_DIGITS) {
|
||||
if (dot)
|
||||
exp--;
|
||||
m = m * 10 + digit;
|
||||
kept += m != 0 ? 1 : 0;
|
||||
} else {
|
||||
if (!dot)
|
||||
exp++;
|
||||
if (!full) {
|
||||
full = true;
|
||||
overflow = digit > 5 ? 3 : digit == 5 ? 2 : digit != 0 ? 1 : 0; // the first one decides the half
|
||||
} else {
|
||||
overflow |= digit != 0 ? 1 : 0;
|
||||
}
|
||||
}
|
||||
} else if (*p == '.' && !dot) {
|
||||
dot = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ((*p | 0x20) == 'x' && any && m == 0)
|
||||
return read_hex(p + 1, dot, exp, end);
|
||||
if (!any) {
|
||||
*end = str;
|
||||
return 0;
|
||||
}
|
||||
*end = read_exponent(p, 'e', &exp);
|
||||
if (m == 0)
|
||||
return 0;
|
||||
// Ties to even on the decimal digits
|
||||
overflow |= m & 1;
|
||||
m += (overflow + 1) >> 2;
|
||||
const int32_t exp10 = static_cast<int32_t>(kept) + exp;
|
||||
if (exp10 <= MIN_EXP10)
|
||||
return 0;
|
||||
if (exp10 >= MAX_EXP10)
|
||||
return FLOAT_INF_BITS;
|
||||
return decimal_to_float_bits(m, exp);
|
||||
}
|
||||
|
||||
// picolibc's strtof from the start: returns the bits without the sign, which goes in *negative
|
||||
__attribute__((noinline)) uint32_t parse_float_full(const char *str, const char **end, bool *negative) {
|
||||
// No number starts with a letter but i (inf) or n (nan): "unavailable", "on" and "unknown" stop here
|
||||
const char first = static_cast<char>(*str | 0x20);
|
||||
if (first >= 'a' && first <= 'z' && first != 'i' && first != 'n')
|
||||
return no_number(str, end, negative);
|
||||
const char *p = str;
|
||||
while (is_space_c(*p))
|
||||
p++;
|
||||
*negative = *p == '-';
|
||||
if (*negative || *p == '+')
|
||||
p++;
|
||||
const char lower = static_cast<char>(*p | 0x20);
|
||||
if (lower == 'i' || lower == 'n') {
|
||||
const char *stop = read_inf_nan(p);
|
||||
if (stop == nullptr)
|
||||
return no_number(str, end, negative);
|
||||
*end = stop;
|
||||
return lower == 'i' ? FLOAT_INF_BITS : FLOAT_NAN_BITS;
|
||||
}
|
||||
const uint32_t bits = read_decimal(str, p, end);
|
||||
if (*end == str)
|
||||
*negative = false;
|
||||
return bits;
|
||||
}
|
||||
|
||||
// A plain decimal below 2^24 with up to 9 digits after the point, in one float operation; nullptr otherwise
|
||||
ESPHOME_ALWAYS_INLINE inline const char *parse_plain_decimal(const char *str, uint32_t *bits, bool *negative) {
|
||||
const char *p = str;
|
||||
*negative = *p == '-';
|
||||
if (*negative || *p == '+')
|
||||
p++;
|
||||
const char *digits = p;
|
||||
const char *point = nullptr;
|
||||
uint32_t mantissa = 0;
|
||||
for (;; p++) {
|
||||
const uint32_t d = static_cast<uint8_t>(*p) - '0';
|
||||
if (d > 9) {
|
||||
if (*p == '.' && point == nullptr) {
|
||||
point = p;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (mantissa > PLAIN_MANTISSA_LIMIT)
|
||||
return nullptr;
|
||||
mantissa = mantissa * 10 + d;
|
||||
}
|
||||
const uint32_t fraction = point != nullptr ? p - point - 1 : 0;
|
||||
const char c = static_cast<char>(*p | 0x20);
|
||||
if (p - digits == (point != nullptr ? 1 : 0) || fraction > 9 || c == 'e' || c == 'x')
|
||||
return nullptr;
|
||||
float value = static_cast<float>(mantissa);
|
||||
if (fraction != 0)
|
||||
value /= static_cast<float>(pow10_exact<uint32_t>(fraction)); // 10^9 is an exact float
|
||||
*bits = bit_cast<uint32_t>(value);
|
||||
return p;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
optional<float> parse_float(const char *str) {
|
||||
// Own copy of the plain-decimal loop: sharing parse_plain_decimal costs 12 to 18% more instructions
|
||||
const char *p = str;
|
||||
const bool negative = *p == '-';
|
||||
if (negative || *p == '+')
|
||||
p++;
|
||||
const char *digits = p;
|
||||
const char *point = nullptr;
|
||||
uint32_t mantissa = 0;
|
||||
for (;; p++) {
|
||||
const uint32_t d = static_cast<uint8_t>(*p) - '0';
|
||||
if (d > 9) {
|
||||
if (*p == '.' && point == nullptr) {
|
||||
point = p;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (mantissa > PLAIN_MANTISSA_LIMIT)
|
||||
break;
|
||||
mantissa = mantissa * 10 + d;
|
||||
}
|
||||
const uint32_t fraction = point != nullptr ? p - point - 1 : 0;
|
||||
if (*p == '\0' && p - digits > (point != nullptr ? 1 : 0) && fraction <= 9) {
|
||||
float value = negative ? -static_cast<float>(mantissa) : static_cast<float>(mantissa);
|
||||
if (fraction != 0)
|
||||
value /= static_cast<float>(pow10_exact<uint32_t>(fraction));
|
||||
return value;
|
||||
}
|
||||
const char *end;
|
||||
bool full_negative;
|
||||
const uint32_t bits = parse_float_full(str, &end, &full_negative);
|
||||
// HUGE_VALF, positive overflow or "inf", is rejected like before
|
||||
if (end == str || *end != '\0' || (!full_negative && bits == FLOAT_INF_BITS))
|
||||
return {};
|
||||
return bit_cast<float>(full_negative ? bits | FLOAT_SIGN_BIT : bits);
|
||||
}
|
||||
|
||||
float str_to_float(const char *str, char **end) {
|
||||
const char *stop;
|
||||
bool negative;
|
||||
uint32_t bits;
|
||||
if (const char *plain_end = parse_plain_decimal(str, &bits, &negative)) {
|
||||
stop = plain_end;
|
||||
} else {
|
||||
bits = parse_float_full(str, &stop, &negative);
|
||||
}
|
||||
if (end != nullptr)
|
||||
*end = const_cast<char *>(stop); // strtof's signature
|
||||
return bit_cast<float>(negative ? bits | FLOAT_SIGN_BIT : bits);
|
||||
}
|
||||
|
||||
} // namespace esphome
|
||||
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "esphome/core/optional.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
/// The whole string as str_to_float's syntax (leading space, hex, inf, nan); trailing characters, "inf" and positive
|
||||
/// overflow are rejected, while -inf, negative overflow and NaN are accepted.
|
||||
optional<float> parse_float(const char *str);
|
||||
|
||||
/// strtof with picolibc's results on every platform, but it never sets errno (overflow gives +-HUGE_VALF, underflow 0);
|
||||
/// use it instead of strtof, or strtod/atof for a float result.
|
||||
float str_to_float(const char *str, char **end);
|
||||
|
||||
} // namespace esphome
|
||||
@@ -7,7 +7,6 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/parse_float.h"
|
||||
|
||||
#ifdef USE_JSON
|
||||
#include "esphome/components/json/json_util.h"
|
||||
@@ -294,7 +293,7 @@ inline bool operator!=(const std::string &lhs, const ProgmemStringRef &rhs) { re
|
||||
#endif
|
||||
|
||||
// String conversion functions for ADL compatibility (allows stoi(x) where x is StringRef)
|
||||
// Must be in esphome namespace for ADL to find them. Uses strtol, strtod and str_to_float to avoid heap allocation.
|
||||
// Must be in esphome namespace for ADL to find them. Uses strtol/strtod directly to avoid heap allocation.
|
||||
namespace internal {
|
||||
// NOLINTBEGIN(google-runtime-int)
|
||||
template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, F conv) {
|
||||
@@ -335,7 +334,7 @@ inline long stol(const StringRef &str, size_t *pos = nullptr, int base = 10) {
|
||||
return internal::parse_number<long>(str, pos, base, std::strtol);
|
||||
}
|
||||
inline float stof(const StringRef &str, size_t *pos = nullptr) {
|
||||
return internal::parse_number<float>(str, pos, str_to_float);
|
||||
return internal::parse_number<float>(str, pos, std::strtof);
|
||||
}
|
||||
inline double stod(const StringRef &str, size_t *pos = nullptr) {
|
||||
return internal::parse_number<double>(str, pos, std::strtod);
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
#include <benchmark/benchmark.h>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome::benchmarks {
|
||||
|
||||
// Inner iteration count to amortize CodSpeed instrumentation overhead.
|
||||
static constexpr int kInnerIterations = 2000;
|
||||
|
||||
static const char *const PLAIN[] = {"0.5", "255", "-12.75", "21.3"};
|
||||
static const char *const EXPONENT[] = {"1e3", "2.5e-2", "-6.02E5", "1.5e+1"};
|
||||
static const char *const PRECISE[] = {"3.14159265358979", "0.000123456789", "123456789.125", "-0.1000000000000000055"};
|
||||
// Home Assistant states that are not numbers
|
||||
static const char *const REJECT[] = {"unavailable", "unknown", "on", "off"};
|
||||
|
||||
template<size_t N, typename F> static void run(benchmark::State &state, const char *const (&inputs)[N], F parse) {
|
||||
for (auto _ : state) {
|
||||
float sum = 0.0f;
|
||||
for (int i = 0; i < kInnerIterations; i++)
|
||||
sum += parse(inputs[i % N]);
|
||||
benchmark::DoNotOptimize(sum);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
|
||||
static float parse_number_float(const char *str) { return parse_number<float>(str).value_or(0.0f); }
|
||||
|
||||
static void ParseNumberFloat_Plain(benchmark::State &state) { run(state, PLAIN, parse_number_float); }
|
||||
BENCHMARK(ParseNumberFloat_Plain);
|
||||
|
||||
static void ParseNumberFloat_Exponent(benchmark::State &state) { run(state, EXPONENT, parse_number_float); }
|
||||
BENCHMARK(ParseNumberFloat_Exponent);
|
||||
|
||||
static void ParseNumberFloat_Precise(benchmark::State &state) { run(state, PRECISE, parse_number_float); }
|
||||
BENCHMARK(ParseNumberFloat_Precise);
|
||||
|
||||
static void ParseNumberFloat_Reject(benchmark::State &state) { run(state, REJECT, parse_number_float); }
|
||||
BENCHMARK(ParseNumberFloat_Reject);
|
||||
|
||||
} // namespace esphome::benchmarks
|
||||
@@ -68,38 +68,6 @@ def test_logger_pre_setup_before_other_components(generate_main):
|
||||
)
|
||||
|
||||
|
||||
def test_default_uart_selection_is_not_emitted(generate_main):
|
||||
"""UART0 is the C++ initializer on ESP8266, so the setter is skipped."""
|
||||
main_cpp = generate_main("tests/component_tests/logger/test_logger.yaml")
|
||||
|
||||
assert "set_uart_selection(" not in main_cpp
|
||||
|
||||
|
||||
def test_custom_uart_selection_is_emitted(generate_main):
|
||||
"""A non default UART still reaches the setter before pre_setup()."""
|
||||
main_cpp = generate_main("tests/component_tests/logger/test_logger_uart1.yaml")
|
||||
|
||||
assert "set_uart_selection(logger::UART_SELECTION_UART1);" in main_cpp
|
||||
|
||||
|
||||
def test_libretiny_default_uart_selection_is_not_emitted(generate_main):
|
||||
"""DEFAULT is the C++ initializer on LibreTiny, so the setter is skipped."""
|
||||
main_cpp = generate_main(
|
||||
"tests/component_tests/logger/test_logger_libretiny_default.yaml"
|
||||
)
|
||||
|
||||
assert "set_uart_selection(" not in main_cpp
|
||||
|
||||
|
||||
def test_libretiny_uart0_is_emitted(generate_main):
|
||||
"""UART0 is not the LibreTiny initializer, so it must still be set."""
|
||||
main_cpp = generate_main(
|
||||
"tests/component_tests/logger/test_logger_libretiny_uart0.yaml"
|
||||
)
|
||||
|
||||
assert "set_uart_selection(logger::UART_SELECTION_UART0);" in main_cpp
|
||||
|
||||
|
||||
def test_ram_log_strings_is_deprecated(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
@@ -168,23 +136,108 @@ def test_log_v2_only_on_native_toolchain(
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config", "usb_cdc"),
|
||||
("config", "port", "name", "zephyr_cdc_loop"),
|
||||
[
|
||||
pytest.param("test_logger_rp2_usb_cdc.yaml", True, id="usb_cdc"),
|
||||
pytest.param("test_logger_rp2_uart0.yaml", False, id="uart0"),
|
||||
pytest.param("test_logger.yaml", "UART0", "UART0", False, id="esp8266_default"),
|
||||
pytest.param(
|
||||
"test_logger_uart1.yaml", "UART1", "UART1", False, id="esp8266_uart1"
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_uart0_swap.yaml",
|
||||
"UART0_SWAP",
|
||||
"UART0_SWAP",
|
||||
False,
|
||||
id="esp8266_uart0_swap",
|
||||
),
|
||||
# ESP8266 without serial logging never opens a port
|
||||
pytest.param(
|
||||
"test_logger_no_serial.yaml", "UART0", "NONE", False, id="esp8266_no_serial"
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_libretiny_default.yaml",
|
||||
"DEFAULT",
|
||||
"DEFAULT",
|
||||
False,
|
||||
id="libretiny_default",
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_libretiny_uart0.yaml",
|
||||
"UART0",
|
||||
"UART0",
|
||||
False,
|
||||
id="libretiny_uart0",
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_rp2_usb_cdc.yaml",
|
||||
"USB_CDC",
|
||||
"USB_CDC",
|
||||
False,
|
||||
id="rp2_usb_cdc",
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_rp2_uart0.yaml", "UART0", "UART0", False, id="rp2_uart0"
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_esp32_usb_cdc.yaml",
|
||||
"USB_CDC",
|
||||
"USB_CDC",
|
||||
False,
|
||||
id="esp32_usb_cdc",
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_esp32_usb_serial_jtag.yaml",
|
||||
"USB_SERIAL_JTAG",
|
||||
"USB_SERIAL_JTAG",
|
||||
False,
|
||||
id="esp32_usb_serial_jtag",
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_nrf52_usb_cdc.yaml",
|
||||
"USB_CDC",
|
||||
"USB_CDC",
|
||||
True,
|
||||
id="nrf52_usb_cdc",
|
||||
),
|
||||
# Zephyr polls the USB port only when it logs there
|
||||
pytest.param(
|
||||
"test_logger_nrf52_usb_cdc_no_serial.yaml",
|
||||
"USB_CDC",
|
||||
"USB_CDC",
|
||||
False,
|
||||
id="nrf52_no_serial",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_rp2_usb_cdc_selection_define(
|
||||
def test_uart_selection_defines(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
config: str,
|
||||
usb_cdc: bool,
|
||||
port: str,
|
||||
name: str,
|
||||
zephyr_cdc_loop: bool,
|
||||
) -> None:
|
||||
"""The USB CDC crash replay only compiles in when the logger uses USB CDC."""
|
||||
"""The chosen port is fixed at build time: one flag, its enum value and its dump_config name."""
|
||||
generate_main(f"tests/component_tests/logger/{config}")
|
||||
|
||||
defines = {d.name: d.value for d in CORE.defines}
|
||||
prefix = "USE_LOGGER_UART_SELECTION_"
|
||||
assert {d.removeprefix(prefix) for d in defines if d.startswith(prefix)} == {port}
|
||||
assert (
|
||||
"USE_LOGGER_UART_SELECTION_USB_CDC" in {d.name for d in CORE.defines}
|
||||
) is usb_cdc
|
||||
str(defines["ESPHOME_LOGGER_UART_SELECTION"])
|
||||
== f"logger::UART_SELECTION_{port}"
|
||||
)
|
||||
assert str(defines["ESPHOME_LOGGER_UART_NAME"]) == f'"{name}"'
|
||||
assert ("USE_LOGGER_ZEPHYR_CDC_LOOP" in defines) is zephyr_cdc_loop
|
||||
|
||||
|
||||
def test_log_listener_slots_count_every_listener(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
) -> None:
|
||||
"""Two on_message triggers and the API each take a slot."""
|
||||
generate_main("tests/component_tests/logger/test_logger_listeners.yaml")
|
||||
|
||||
assert (
|
||||
str({d.name: d.value for d in CORE.defines}["ESPHOME_LOG_MAX_LISTENERS"]) == "3"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
variant: esp32s3
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
logger:
|
||||
hardware_uart: USB_CDC
|
||||
@@ -0,0 +1,11 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
variant: esp32c3
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
logger:
|
||||
hardware_uart: USB_SERIAL_JTAG
|
||||
@@ -0,0 +1,23 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
variant: esp32
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
wifi:
|
||||
ssid: test
|
||||
password: testtesttest
|
||||
|
||||
api:
|
||||
|
||||
logger:
|
||||
on_message:
|
||||
- level: ERROR
|
||||
then:
|
||||
- lambda: "(void) message;"
|
||||
- level: WARN
|
||||
then:
|
||||
- lambda: "(void) message;"
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp8266:
|
||||
board: d1_mini_lite
|
||||
|
||||
logger:
|
||||
baud_rate: 0
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
nrf52:
|
||||
board: adafruit_itsybitsy_nrf52840
|
||||
bootloader: adafruit_nrf52_sd140_v6
|
||||
|
||||
logger:
|
||||
hardware_uart: USB_CDC
|
||||
@@ -0,0 +1,11 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
nrf52:
|
||||
board: adafruit_itsybitsy_nrf52840
|
||||
bootloader: adafruit_nrf52_sd140_v6
|
||||
|
||||
logger:
|
||||
hardware_uart: USB_CDC
|
||||
baud_rate: 0
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp8266:
|
||||
board: d1_mini_lite
|
||||
|
||||
logger:
|
||||
hardware_uart: UART0_SWAP
|
||||
@@ -1,74 +1,34 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include <string>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/parse_float.h"
|
||||
|
||||
#ifdef USE_HOST
|
||||
namespace esphome::core::testing {
|
||||
|
||||
static uint32_t bits(float value) { return bit_cast<uint32_t>(value); }
|
||||
|
||||
// picolibc rounds a decimal to 9 significant digits and converts those to the nearest float, so up to 9
|
||||
// significant digits its result is the correctly rounded float the host's strtof (glibc, macOS) gives. Longer
|
||||
// input and the syntax where libcs differ are checked against values taken from picolibc 1.8.10's own strtof.
|
||||
static float reference_strtof(const char *str, char **end) {
|
||||
const float value = ::strtof(str, end);
|
||||
if (std::isnan(value))
|
||||
return std::signbit(value) ? -NAN : NAN;
|
||||
return value;
|
||||
static uint32_t bits(float value) {
|
||||
uint32_t out;
|
||||
memcpy(&out, &value, sizeof(out));
|
||||
return out;
|
||||
}
|
||||
|
||||
// Every accepted input must give the reference's bits.
|
||||
// strtof is the reference: every accepted input must give the same bits.
|
||||
static void expect_matches_strtof(const char *str) {
|
||||
const float expected = reference_strtof(str, nullptr);
|
||||
char *end = nullptr;
|
||||
const float expected = ::strtof(str, &end);
|
||||
const auto parsed = parse_number<float>(str);
|
||||
if (!parsed.has_value()) {
|
||||
ADD_FAILURE() << str << " was rejected";
|
||||
return;
|
||||
}
|
||||
EXPECT_EQ(bits(parsed.value()), bits(expected)) << str;
|
||||
const float actual = parsed.value();
|
||||
EXPECT_EQ(bits(actual), bits(expected)) << str << " parsed " << actual << " expected " << expected;
|
||||
}
|
||||
|
||||
// The same bits and end as the host's strtof; for decimals of up to 9 significant digits
|
||||
static void expect_str_to_float_matches(const char *str) {
|
||||
char *expected_end = nullptr;
|
||||
const float expected = reference_strtof(str, &expected_end);
|
||||
char *end = nullptr;
|
||||
const float actual = str_to_float(str, &end);
|
||||
EXPECT_EQ(bits(actual), bits(expected)) << '"' << str << '"';
|
||||
EXPECT_EQ(end - str, expected_end - str) << '"' << str << '"';
|
||||
}
|
||||
|
||||
struct PicolibcCase {
|
||||
const char *str;
|
||||
uint32_t bits;
|
||||
int32_t end;
|
||||
};
|
||||
|
||||
// Bits and end from picolibc 1.8.10's strtof
|
||||
static void expect_picolibc(const PicolibcCase &c) {
|
||||
char *end = nullptr;
|
||||
const float value = str_to_float(c.str, &end);
|
||||
EXPECT_EQ(bits(value), c.bits) << '"' << c.str << '"';
|
||||
EXPECT_EQ(end - c.str, c.end) << '"' << c.str << '"';
|
||||
}
|
||||
|
||||
static void expect_picolibc(const std::string &str, uint32_t expected) {
|
||||
char *end = nullptr;
|
||||
EXPECT_EQ(bits(str_to_float(str.c_str(), &end)), expected) << str.substr(0, 40);
|
||||
EXPECT_EQ(static_cast<size_t>(end - str.c_str()), str.size()) << str.substr(0, 40);
|
||||
}
|
||||
|
||||
static float float_from(uint32_t value) { return bit_cast<float>(value); }
|
||||
|
||||
TEST(ParseFloat, PlainDecimalsMatchStrtof) {
|
||||
for (const char *str :
|
||||
{"0", "-0", "+0", "1", "-1", "100.50", "99.75", "0.1", "0.3",
|
||||
@@ -86,14 +46,6 @@ TEST(ParseFloat, SweepOfTwoDecimalValuesMatchesStrtof) {
|
||||
}
|
||||
}
|
||||
|
||||
// Eight-digit mantissas below 2^24 stay on the one-division path; the next digit past the limit falls back
|
||||
TEST(ParseFloat, EightDigitPlainDecimalsMatchStrtof) {
|
||||
for (const char *str : {"12345678", "-12345678", "1234.5678", "0.12345678", "16777209", "1677720.9", "0.16777209",
|
||||
"16777210", "16777215", "1677721.5", "99999999", "9999999.9", "16777209."}) {
|
||||
expect_matches_strtof(str);
|
||||
}
|
||||
}
|
||||
|
||||
// A fraction longer than the fast path accepts must fall back, however long it is.
|
||||
TEST(ParseFloat, LongFractionsMatchStrtof) {
|
||||
std::string str = "0.";
|
||||
@@ -116,293 +68,10 @@ TEST(ParseFloat, LibcPathInputsMatchStrtof) {
|
||||
// Positive overflow and a positive literal infinity both come back as HUGE_VALF from strtof, and the parser
|
||||
// rejects both; the negative forms pass through, as they did before the fast path.
|
||||
TEST(ParseFloat, RejectsInvalidInput) {
|
||||
for (const char *str : {"", "-", "+", ".", "abc", "12abc", "1.2.3", "1,5", "12 ", "--1", "1e", "1e400", "inf",
|
||||
"infinity", "infi", "nan(12 ab)", "nan(12"}) {
|
||||
for (const char *str :
|
||||
{"", "-", "+", ".", "abc", "12abc", "1.2.3", "1,5", "12 ", "--1", "1e", "1e400", "inf", "infinity"}) {
|
||||
EXPECT_FALSE(parse_number<float>(str).has_value()) << str;
|
||||
}
|
||||
}
|
||||
|
||||
// Above the midpoint 1 + 2^-24 by less than half a double: newlib's strtof rounds to the midpoint and then to
|
||||
// 1.0f; picolibc's 9 digits, 1.00000006, give the next float.
|
||||
TEST(ParseFloat, FullPathRoundsLikePicolibc) {
|
||||
EXPECT_EQ(bits(parse_number<float>("1.0000000596046448").value()), 0x3F800001u);
|
||||
EXPECT_EQ(bits(parse_number<float>("-1.0000000596046448").value()), 0xBF800001u);
|
||||
}
|
||||
|
||||
// picolibc takes "nan(" with letters, digits and '_' up to a ")", and nothing at all otherwise
|
||||
TEST(ParseFloat, NanForms) {
|
||||
EXPECT_TRUE(std::isnan(parse_number<float>("nan(12)").value()));
|
||||
EXPECT_TRUE(std::isnan(parse_number<float>("nan(xyz_1)").value()));
|
||||
EXPECT_FALSE(parse_number<float>("nan(x y)").has_value());
|
||||
}
|
||||
|
||||
// The callers' overflow check: a positive overflow is HUGE_VALF and rejected, a negative one is -inf and accepted
|
||||
TEST(ParseFloat, OverflowIsHugeValf) {
|
||||
EXPECT_FALSE(parse_number<float>("1e39").has_value());
|
||||
EXPECT_FALSE(parse_number<float>("3.40282357e38").has_value());
|
||||
EXPECT_FALSE(parse_number<float>("1e99999").has_value());
|
||||
EXPECT_EQ(bits(parse_number<float>("-1e39").value()), 0xFF800000u);
|
||||
EXPECT_EQ(bits(parse_number<float>("3.4028235e38").value()), 0x7F7FFFFFu);
|
||||
}
|
||||
|
||||
// Up to 9 significant digits, with leading and trailing zeros, a point anywhere and an exponent
|
||||
TEST(StrToFloat, RandomDecimalsMatchStrtof) {
|
||||
std::mt19937_64 rng(1); // NOLINT(cert-msc32-c,cert-msc51-cpp,bugprone-random-generator-seed) reproducible
|
||||
for (int i = 0; i < 50000; i++) {
|
||||
std::string str;
|
||||
if (rng() % 3 == 0)
|
||||
str += (rng() & 1) ? "-" : "+";
|
||||
const int zeros = static_cast<int>(rng() % 4);
|
||||
const int significant = 1 + static_cast<int>(rng() % 9);
|
||||
const int trailing = static_cast<int>(rng() % 4);
|
||||
std::string digits(zeros, '0');
|
||||
digits += static_cast<char>('1' + rng() % 9);
|
||||
for (int j = 1; j < significant; j++)
|
||||
digits += static_cast<char>('0' + rng() % 10);
|
||||
digits.append(trailing, '0');
|
||||
const int point = static_cast<int>(rng() % (digits.size() + 2)) - 1;
|
||||
for (int j = 0; j < static_cast<int>(digits.size()); j++) {
|
||||
if (j == point)
|
||||
str += '.';
|
||||
str += digits[j];
|
||||
}
|
||||
if (rng() & 1) {
|
||||
str += (rng() & 1) ? 'e' : 'E';
|
||||
char exp[8];
|
||||
snprintf(exp, sizeof(exp), "%d", static_cast<int>(rng() % 110) - 60);
|
||||
str += exp;
|
||||
}
|
||||
expect_str_to_float_matches(str.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// Every 16411th float, printed with 6 and 9 digits
|
||||
TEST(StrToFloat, FloatsMatchStrtof) {
|
||||
char buf[64];
|
||||
for (uint32_t u = 0; u < 0x7F800000; u += 16411) {
|
||||
for (int precision : {6, 9}) {
|
||||
snprintf(buf, sizeof(buf), "%.*e", precision - 1, static_cast<double>(float_from(u)));
|
||||
expect_str_to_float_matches(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(StrToFloat, SyntaxMatchesStrtof) {
|
||||
// Tokens whose combinations the libcs read alike: no "x", "nan(" or "infi"
|
||||
static const char *const TOKENS[] = {"0", "1", "9", ".", "e", "E", "+", "-", "a", "N", " ", "\t", "y", "00", "#", ""};
|
||||
for (const char *a : TOKENS) {
|
||||
for (const char *b : TOKENS) {
|
||||
for (const char *c : TOKENS) {
|
||||
for (const char *d : TOKENS) {
|
||||
const std::string str = std::string(a) + b + c + d;
|
||||
expect_str_to_float_matches(str.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Where newlib and glibc read the text differently from picolibc
|
||||
TEST(StrToFloat, SyntaxMatchesPicolibc) {
|
||||
static const PicolibcCase CASES[] = {
|
||||
// "x" after any zero digits starts hex, even after a point or a second "0x"
|
||||
{"000x", 0x00000000, 4},
|
||||
{"0x", 0x00000000, 2},
|
||||
{"-0x", 0x80000000, 3},
|
||||
{"00x1", 0x3F800000, 4},
|
||||
{"0x0x1", 0x3F800000, 5},
|
||||
{"0.0x1", 0x3B800000, 5},
|
||||
{".x1", 0x00000000, 0},
|
||||
{"0x.p1", 0x00000000, 5},
|
||||
{"0x.8", 0x3F000000, 4},
|
||||
{"0X1.8", 0x3FC00000, 5},
|
||||
{"0x1P-1", 0x3F000000, 6},
|
||||
{"0x1p+", 0x3F800000, 3},
|
||||
{"1x5", 0x3F800000, 1},
|
||||
// "inf" stops before anything but "i"; past "infi" only "infinity" is a number
|
||||
{"inf", 0x7F800000, 3},
|
||||
{"-inf", 0xFF800000, 4},
|
||||
{"INFINITY", 0x7F800000, 8},
|
||||
{"+infi0", 0x00000000, 0},
|
||||
{"infi", 0x00000000, 0},
|
||||
{"infinit", 0x00000000, 0},
|
||||
// "nan(" takes letters, digits and '_' up to ")", and is no number without the ")"
|
||||
{"NaN", 0x7FC00000, 3},
|
||||
{"nan()", 0x7FC00000, 5},
|
||||
{"nan(xyz)", 0x7FC00000, 8},
|
||||
{"nan(_a1)", 0x7FC00000, 8},
|
||||
{"-nan(1)", 0xFFC00000, 7},
|
||||
{"nan( )", 0x00000000, 0},
|
||||
{"nan(12 ab)", 0x00000000, 0},
|
||||
{"nan(12", 0x00000000, 0},
|
||||
// An exponent needs a digit
|
||||
{"1e", 0x3F800000, 1},
|
||||
{"1e+", 0x3F800000, 1},
|
||||
{"1e-x", 0x3F800000, 1},
|
||||
{"\t 12", 0x41400000, 4},
|
||||
{"- 1", 0x00000000, 0},
|
||||
{"1.2.3", 0x3F99999A, 3},
|
||||
};
|
||||
for (const PicolibcCase &c : CASES)
|
||||
expect_picolibc(c);
|
||||
}
|
||||
|
||||
// The inputs where newlib's strtof (decimal to double to float) and picolibc's were found to differ; every platform
|
||||
// now gives picolibc's result, which is also the correctly rounded float for these
|
||||
TEST(StrToFloat, RoundsLikePicolibcWhereNewlibDoubleRounds) {
|
||||
expect_picolibc({"1.0000000596046448", 0x3F800001, 18});
|
||||
expect_picolibc({"0x1.100b0d0000000c", 0x3F880587, 18});
|
||||
expect_picolibc("1.000000059604644775390625" + std::string(800, '0') + "1", 0x3F800001);
|
||||
}
|
||||
|
||||
// picolibc rounds a decimal to 9 significant digits, ties to even, before converting it: past 9 digits its float can
|
||||
// be one away from the nearest, and an exact tie between two floats can round up. Values from picolibc.
|
||||
TEST(StrToFloat, NineDigitRoundingLikePicolibc) {
|
||||
static const PicolibcCase CASES[] = {
|
||||
{"1.0856183295439e10", 0x5021C50E, 18}, // nearest 0x5021C50D
|
||||
{"3.97496754550e-4", 0x39D0671D, 16}, // nearest 0x39D0671C
|
||||
{"9.76178673657108930e23", 0x674EB6C1, 22}, // nearest 0x674EB6C0
|
||||
{"156.6562236e-26", 0x17F269ED, 15}, // nearest 0x17F269EC
|
||||
{"1.000000059604644775390625", 0x3F800001, 26}, // the tie 1 + 2^-24: nearest-even 0x3F800000
|
||||
{"1.000000059", 0x3F800001, 11}, // 1.00000006 after rounding
|
||||
{"7.0064923216240854e-46", 0x00000000, 22}, // 7.00649232e-46, below half the smallest float
|
||||
{"21.299999999999997", 0x41AA6666, 18},
|
||||
{"0.30000000000000004", 0x3E99999A, 19},
|
||||
{"0.1000000000000000055511151231257827", 0x3DCCCCCD, 36},
|
||||
};
|
||||
for (const PicolibcCase &c : CASES)
|
||||
expect_picolibc(c);
|
||||
}
|
||||
|
||||
// Nine digits converted exactly: ties to even between floats, where the 64-bit product cannot decide alone
|
||||
TEST(StrToFloat, NineDigitTiesGoToEven) {
|
||||
static const PicolibcCase CASES[] = {
|
||||
{"16777217", 0x4B800000, 8}, {"16777219", 0x4B800002, 8}, {"16777217.0", 0x4B800000, 10},
|
||||
{"8388608.5", 0x4B000000, 9}, {"8388609.5", 0x4B000002, 9}, {"3e10", 0x50DF8476, 4},
|
||||
{"5e10", 0x513A43B7, 4}, {"1.00000006", 0x3F800001, 10},
|
||||
};
|
||||
for (const PicolibcCase &c : CASES)
|
||||
expect_picolibc(c);
|
||||
}
|
||||
|
||||
// RP2350 flushes float denormals in its double to float conversion; #22677 bench. The parser builds the bits
|
||||
// with integer operations, so every denormal parses to its own value.
|
||||
TEST(StrToFloat, DenormalsParseWithoutFlushing) {
|
||||
char buf[64];
|
||||
const auto check = [&buf](uint32_t k) {
|
||||
snprintf(buf, sizeof(buf), "%.8e", std::ldexp(static_cast<double>(k), -149));
|
||||
EXPECT_EQ(bits(str_to_float(buf, nullptr)), k) << buf;
|
||||
};
|
||||
for (uint32_t k = 0; k <= 0x800000; k += 4099)
|
||||
check(k);
|
||||
// The largest denormal and FLT_MIN, where rounding carries into the exponent
|
||||
for (uint32_t k : {1u, 2u, 0x7FFFFEu, 0x7FFFFFu, 0x800000u})
|
||||
check(k);
|
||||
}
|
||||
|
||||
// Pico W and Pico 2 W bench, #22677: the RP2040 and RP2350 libcs flush float denormals to 0 in their double to
|
||||
// float conversion, and the RP2040 one turns a NaN into +infinity
|
||||
TEST(StrToFloat, Rp2040LibcFlushesAndNanToInfinity) {
|
||||
static const PicolibcCase CASES[] = {
|
||||
{"1e-40", 0x000116C2, 5}, {"1.4e-45", 0x00000001, 7}, {"1e-38", 0x006CE3EE, 5},
|
||||
{"1.1754942e-38", 0x007FFFFF, 13}, {"0x1p-149", 0x00000001, 8}, {"1.17549435e-38", 0x00800000, 14},
|
||||
{"nan(12)", 0x7FC00000, 7}, {"nan", 0x7FC00000, 3},
|
||||
};
|
||||
for (const PicolibcCase &c : CASES)
|
||||
expect_picolibc(c);
|
||||
}
|
||||
|
||||
// RP2350 bench: libc returned 0 and the first parser stalled stepping up from a flushed guess; #22677 bench
|
||||
TEST(StrToFloat, DenormalsAndFltMinMatchStrtof) {
|
||||
for (const char *str : {"1.4e-45", "-1.4e-45", "1e-40", "1e-38", "1.17549435e-38", "1.1754942e-38", "1.17549421e-38",
|
||||
"1.1754945e-38", "7.0064923e-46", "7.0064924e-46"}) {
|
||||
expect_str_to_float_matches(str);
|
||||
}
|
||||
}
|
||||
|
||||
// Home Assistant's non-numeric states; CodSpeed HomeassistantSensor_StateResponse_Unavailable on #22677
|
||||
TEST(StrToFloat, NonNumericStatesAreRejected) {
|
||||
for (const char *str : {"unavailable", "unknown", "on", "off", "", "-", ".", "none", "inactive"}) {
|
||||
expect_str_to_float_matches(str);
|
||||
EXPECT_FALSE(parse_number<float>(str).has_value()) << str;
|
||||
}
|
||||
}
|
||||
|
||||
// The one float operation path takes up to 2^24 - 1 and 10^10; #22677 review
|
||||
TEST(StrToFloat, PlainPathBoundaries) {
|
||||
for (const char *top : {"16777215", "16777216", "16777217"}) {
|
||||
for (const char *exp : {"e10", "e-10", "e11", "e-11", "e9", "e-9", ".5", ".25"}) {
|
||||
const std::string str = std::string(top) + exp;
|
||||
expect_str_to_float_matches(str.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The midpoint between FLT_MAX and 2^128 rounds to infinity; review
|
||||
TEST(StrToFloat, OverflowMidpoint) {
|
||||
expect_picolibc({"340282356779733661637539395458142568448", 0x7F800000, 39});
|
||||
expect_picolibc({"3.40282356e38", 0x7F7FFFFF, 13});
|
||||
expect_picolibc({"3.40282357e38", 0x7F800000, 13});
|
||||
EXPECT_EQ(bits(str_to_float("0x1.ffffffp127", nullptr)), 0x7F800000u);
|
||||
}
|
||||
|
||||
// picolibc stops adding exponent digits at 224, so the digits of the mantissa can move it no further
|
||||
TEST(StrToFloat, HugeExponents) {
|
||||
for (const char *str : {"1e-99999", "1e99999", "-1e99999", "0e99999", "1e9999999999999999999",
|
||||
"1e-9999999999999999999", "1e-999999999999", "0.000e99999", "1e224", "1e2240", "1e-2240"}) {
|
||||
expect_str_to_float_matches(str);
|
||||
}
|
||||
// 10^-3000 * 10^3000 is 1, but the exponent counts only as 300
|
||||
expect_picolibc("0." + std::string(2999, '0') + "1e3000", 0x00000000);
|
||||
expect_picolibc("1" + std::string(300, '0') + "e-300", 0x3F800000);
|
||||
}
|
||||
|
||||
TEST(StrToFloat, LongMantissas) {
|
||||
expect_picolibc("1" + std::string(799, '0') + "e-799", 0x3F800000);
|
||||
expect_picolibc("1." + std::string(3000, '0') + "1e-3001", 0x00000000);
|
||||
expect_picolibc("0." + std::string(400, '0') + "34028235" + std::string(500, '7') + "e440", 0x7F800000);
|
||||
expect_picolibc(std::string(300, '9') + "e-261", 0x7F800000);
|
||||
expect_picolibc("3" + std::string(799, '7') + "e-761", 0x7F800000);
|
||||
}
|
||||
|
||||
// picolibc takes ':' to '?' as hex digits 10 to 15 ("0x1:" is 26); these stop at them, as newlib does. #22677 review
|
||||
TEST(StrToFloat, HexStopsAtPunctuationAfterNine) {
|
||||
struct Case {
|
||||
const char *str;
|
||||
uint32_t bits;
|
||||
int32_t end;
|
||||
};
|
||||
const Case cases[] = {
|
||||
{"0x1:", 0x3F800000, 3}, {"0x1;", 0x3F800000, 3}, {"0x1<", 0x3F800000, 3}, {"0x1=", 0x3F800000, 3},
|
||||
{"0x1>", 0x3F800000, 3}, {"0x1?", 0x3F800000, 3}, {"0x1.:", 0x3F800000, 4}, {"0x:", 0x00000000, 2},
|
||||
{"0x9?", 0x41100000, 3}, {"0xA:", 0x41200000, 3},
|
||||
};
|
||||
for (const auto &c : cases) {
|
||||
char *end = nullptr;
|
||||
const float value = str_to_float(c.str, &end);
|
||||
EXPECT_EQ(bits(value), c.bits) << '"' << c.str << '"';
|
||||
EXPECT_EQ(end - c.str, c.end) << '"' << c.str << '"';
|
||||
EXPECT_FALSE(parse_number<float>(c.str).has_value()) << c.str;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(StrToFloat, HexDenormalsAndOverflow) {
|
||||
struct Case {
|
||||
const char *str;
|
||||
uint32_t bits;
|
||||
};
|
||||
static const Case CASES[] = {
|
||||
{"0x1p-149", 0x00000001}, {"0x1p-150", 0x00000000},
|
||||
{"0x1.8p-149", 0x00000002}, {"0x1.fffffcp-127", 0x007FFFFF},
|
||||
{"0x1.fffffep-127", 0x00800000}, {"-0x1p-149", 0x80000001},
|
||||
{"0x1p128", 0x7F800000}, {"0x1.fffffep127", 0x7F7FFFFF},
|
||||
{"0x1.fffffefp127", 0x7F7FFFFF}, {"0x1.0000000000001p-150", 0x00000001},
|
||||
{"0x1.000001p0", 0x3F800000}, {"0x1.000003p0", 0x3F800002},
|
||||
{"0x1p-99999999999", 0x00000000}, {"0x1p99999999999", 0x7F800000},
|
||||
};
|
||||
for (const Case &c : CASES)
|
||||
EXPECT_EQ(bits(str_to_float(c.str, nullptr)), c.bits) << c.str;
|
||||
}
|
||||
|
||||
} // namespace esphome::core::testing
|
||||
#endif // USE_HOST
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstring>
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
// The cap and message header are compiled only for platforms with a task log buffer
|
||||
#define USE_LIBRETINY
|
||||
#ifndef USE_ESPHOME_TASK_LOG_BUFFER
|
||||
#define USE_ESPHOME_TASK_LOG_BUFFER
|
||||
#endif
|
||||
#include "esphome/components/logger/task_log_message.h"
|
||||
#undef USE_LIBRETINY
|
||||
|
||||
namespace esphome::logger::testing {
|
||||
|
||||
TEST(TaskLogMessageTest, CapLengthStopsAt255) {
|
||||
EXPECT_EQ(task_log_cap_length(0), 0);
|
||||
EXPECT_EQ(task_log_cap_length(255), 255);
|
||||
EXPECT_EQ(task_log_cap_length(256), 255);
|
||||
EXPECT_EQ(task_log_cap_length(1000), 255);
|
||||
}
|
||||
|
||||
TEST(TaskLogMessageTest, HeaderCopiesFieldsAndThreadName) {
|
||||
TaskLogMessage msg{};
|
||||
fill_task_log_header(&msg, 3, "tag", 42, "worker");
|
||||
EXPECT_EQ(msg.level, 3);
|
||||
EXPECT_STREQ(msg.tag, "tag");
|
||||
EXPECT_EQ(msg.line, 42);
|
||||
EXPECT_STREQ(msg.thread_name, "worker");
|
||||
}
|
||||
|
||||
TEST(TaskLogMessageTest, HeaderNullThreadNameIsEmpty) {
|
||||
TaskLogMessage msg{};
|
||||
std::memset(msg.thread_name, 'x', sizeof(msg.thread_name));
|
||||
fill_task_log_header(&msg, 1, "tag", 1, nullptr);
|
||||
EXPECT_STREQ(msg.thread_name, "");
|
||||
}
|
||||
|
||||
TEST(TaskLogMessageTest, HeaderTruncatesLongThreadName) {
|
||||
TaskLogMessage msg{};
|
||||
fill_task_log_header(&msg, 1, "tag", 1, "a_thread_name_longer_than_sixteen");
|
||||
EXPECT_EQ(std::strlen(msg.thread_name), sizeof(msg.thread_name) - 1);
|
||||
EXPECT_EQ(std::strncmp(msg.thread_name, "a_thread_name_l", sizeof(msg.thread_name) - 1), 0);
|
||||
}
|
||||
|
||||
TEST(TaskLogMessageTest, TrimTrailingNewlines) {
|
||||
EXPECT_EQ(trim_trailing_newlines("", 0), 0);
|
||||
EXPECT_EQ(trim_trailing_newlines("abc", 3), 3);
|
||||
EXPECT_EQ(trim_trailing_newlines("abc\n\n", 5), 3);
|
||||
EXPECT_EQ(trim_trailing_newlines("\n\n\n", 3), 0);
|
||||
EXPECT_EQ(trim_trailing_newlines("a\nb", 3), 3);
|
||||
}
|
||||
|
||||
} // namespace esphome::logger::testing
|
||||
@@ -17,7 +17,6 @@ void setup() {
|
||||
App.pre_setup(name, sizeof(name) - 1, friendly_name, sizeof(friendly_name) - 1);
|
||||
auto *log = new logger::Logger(115200); // NOLINT
|
||||
log->pre_setup();
|
||||
log->set_uart_selection(logger::UART_SELECTION_UART0);
|
||||
App.register_component_(log);
|
||||
|
||||
auto *wifi = new wifi::WiFiComponent(); // NOLINT
|
||||
|
||||
Reference in new issue
Block a user