Add ESPHOME_DEBUG_ASSERT macro that only fires when ESPHOME_DEBUG is
defined. Use it to assert global_logger is not null in log functions.
Enable ESPHOME_DEBUG in C++ unit test builds so these assertions are
active during testing but have zero cost in production firmware.
Log functions call global_logger->log_vprintf_() without a null check.
The test main.cpp skips the generated setup() (which calls
Logger::pre_setup()), so set up a static Logger before running tests.
Call global_logger->log_vprintf_() directly without null-checking
global_logger on every log call. Logger::pre_setup() sets global_logger
before any other component is created in the generated setup() function,
so it is guaranteed to be valid by the time any log function is invoked.
Also removes the __FlashStringHelper* esp_log_vprintf_ overload which
was dead code (only called from esp_log_printf_, never directly).
Add a Python codegen test to verify the ordering invariant, and a
comment on App.pre_setup() documenting the constraint.
On platforms without USE_STORE_LOG_STR_IN_FLASH (ESP32, RP2040,
LibreTiny), there is only one esp_log_printf_ overload, so the
separate esp_log_vprintf_ function just adds an unnecessary call
frame. Inline the logger dispatch directly into esp_log_printf_
for these platforms.
The const char* esp_log_vprintf_ is still provided unconditionally
for direct callers (e.g. midea component).
On ESP8266 (USE_STORE_LOG_STR_IN_FLASH), the two esp_log_printf_
overloads continue to share esp_log_vprintf_ as before.
Measured: 32 bytes flash saved on ESP32, no change on ESP8266.
Move the pure logic (string building, escaping, byte length
calculation) out of the nested closure into a module-level function
that can be tested without codegen mocks. Tests cover both copilot
review concerns: empty friendly_name with MAC suffix, and UTF-8
byte length for non-ASCII characters.
When name_add_mac_suffix is true and friendly_name is empty, emit a
mutable static char[] buffer instead of a string literal to match the
char* signature. Also use UTF-8 byte lengths instead of Python str
len() to handle non-ASCII characters correctly.
When MAC suffix is not used, pre_setup takes const char* parameters
so string literals stay in flash. When MAC suffix is used, it takes
mutable char* for the static buffers that get overwritten with the
actual MAC address. This avoids const_cast entirely.
Also adds ESPHOME_NAME_ADD_MAC_SUFFIX define for static analysis.
Replace std::string members with StringRef for Application::name_
and Application::friendly_name_. These are set once during setup()
and never modified, so std::string overhead is unnecessary.
For the MAC suffix case, codegen emits static mutable char buffers
with a placeholder suffix that pre_setup() overwrites with the
actual MAC address. For the non-suffix case, StringRef points
directly at the string literal.
Saves ~2.5KB flash and ~48 bytes RAM by eliminating std::string
template instantiations (constructor, _M_assign, _M_dispose,
_M_construct, _M_replace_cold, _S_copy).