mirror of
https://github.com/esphome/esphome.git
synced 2026-09-01 10:36:01 +00:00
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.
44 lines
1.6 KiB
Python
44 lines
1.6 KiB
Python
"""Tests for the logger component."""
|
|
|
|
import re
|
|
|
|
|
|
def test_logger_pre_setup_before_other_components(generate_main):
|
|
"""Logger::pre_setup() must be called before any other component is created.
|
|
|
|
Log functions call global_logger->log_vprintf_() without a null check,
|
|
so global_logger must be set before anything can log.
|
|
"""
|
|
main_cpp = generate_main("tests/component_tests/logger/test_logger.yaml")
|
|
|
|
# Find the position of logger pre_setup
|
|
pre_setup_match = re.search(r"->pre_setup\(\)", main_cpp)
|
|
assert pre_setup_match is not None, "Logger pre_setup() not found in generated code"
|
|
|
|
# Find all "new " allocations (component creation)
|
|
new_allocations = list(re.finditer(r"\bnew [\w:]+", main_cpp))
|
|
assert len(new_allocations) > 0, "No component allocations found"
|
|
|
|
# Find the logger allocation
|
|
logger_new = None
|
|
for alloc in new_allocations:
|
|
if "logger" in alloc.group():
|
|
logger_new = alloc
|
|
break
|
|
|
|
assert logger_new is not None, (
|
|
f"Logger allocation not found in: {[a.group() for a in new_allocations]}"
|
|
)
|
|
|
|
# All non-logger allocations must appear after pre_setup()
|
|
for alloc in new_allocations:
|
|
if alloc == logger_new:
|
|
continue
|
|
# Skip "new (&App)" placement new which is before logger
|
|
if "(&App)" in main_cpp[max(0, alloc.start() - 5) : alloc.start()]:
|
|
continue
|
|
assert alloc.start() > pre_setup_match.start(), (
|
|
f"Component allocation '{alloc.group()}' at position {alloc.start()} "
|
|
f"appears before logger pre_setup() at position {pre_setup_match.start()}"
|
|
)
|