From 589942f52c6f0b12f0a1f469156386529817bc01 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 20 Dec 2025 11:11:54 -1000 Subject: [PATCH] simplify logic --- esphome/components/syslog/esphome_syslog.cpp | 16 ++++--------- tests/integration/fixtures/syslog.yaml | 5 ++++ tests/integration/test_syslog.py | 24 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/esphome/components/syslog/esphome_syslog.cpp b/esphome/components/syslog/esphome_syslog.cpp index 8e7f809ecc..2fef1889f1 100644 --- a/esphome/components/syslog/esphome_syslog.cpp +++ b/esphome/components/syslog/esphome_syslog.cpp @@ -55,18 +55,12 @@ void Syslog::log_(const int level, const char *tag, const char *message, size_t offset = ret; remaining -= ret; - // Write timestamp directly into packet (RFC 5424: use "-" if time not valid) + // Write timestamp directly into packet (RFC 5424: use "-" if time not valid or strftime fails) auto now = this->time_->now(); - if (now.is_valid()) { - size_t written = now.strftime(packet + offset, remaining, "%b %e %H:%M:%S"); - if (written > 0) { - offset += written; - remaining -= written; - } else if (remaining > 0) { - // strftime failed; write NILVALUE as fallback - packet[offset++] = '-'; - remaining--; - } + size_t ts_written = now.is_valid() ? now.strftime(packet + offset, remaining, "%b %e %H:%M:%S") : 0; + if (ts_written > 0) { + offset += ts_written; + remaining -= ts_written; } else if (remaining > 0) { packet[offset++] = '-'; remaining--; diff --git a/tests/integration/fixtures/syslog.yaml b/tests/integration/fixtures/syslog.yaml index fee00eb8ff..df376087e3 100644 --- a/tests/integration/fixtures/syslog.yaml +++ b/tests/integration/fixtures/syslog.yaml @@ -16,6 +16,11 @@ api: "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD" "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + - service: log_short_message + then: + - lambda: |- + // Log a short message that should arrive complete (not truncated) + ESP_LOGI("shorttest", "BEGIN|SHORT_MESSAGE_CONTENT|FINISH"); logger: level: DEBUG diff --git a/tests/integration/test_syslog.py b/tests/integration/test_syslog.py index fee493c5cb..b31a19392c 100644 --- a/tests/integration/test_syslog.py +++ b/tests/integration/test_syslog.py @@ -258,3 +258,27 @@ async def test_syslog( assert "|END" not in trunc_msg, ( "Message should be truncated before END marker" ) + + # Test short message - should arrive complete (not truncated) + short_service = next( + (s for s in services if s.name == "log_short_message"), None + ) + assert short_service is not None, "log_short_message service not found" + + await client.execute_service(short_service, {}) + + try: + short_msg = await receiver.wait_for_pattern(r"shorttest.*BEGIN\|") + except TimeoutError: + pytest.fail( + f"Short test message not received. Got: {receiver.messages[-10:]}" + ) + + # Verify short message arrived complete with both markers + assert "BEGIN|" in short_msg, "Short message missing BEGIN marker" + assert "|FINISH" in short_msg, ( + f"Short message truncated unexpectedly: {short_msg}" + ) + assert "SHORT_MESSAGE_CONTENT" in short_msg, ( + f"Short message content missing: {short_msg}" + )