From 9774051fabfb43257194be8830c976eab32f53d9 Mon Sep 17 00:00:00 2001 From: MichaelDvP <59284019+MichaelDvP@users.noreply.github.com> Date: Sun, 15 Aug 2021 10:24:57 +0200 Subject: [PATCH] Syslog BOM only for utf-8 messages, #91 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tested with extra message with udf-characters: ``` Aug 15 09:30:12 ems-esp32 emsesp 000+00:00:00.000 I 0 Starting Syslog Aug 15 09:30:13 ems-esp32 emsesp 000+00:00:00.000 I 2 EMS Device library loaded with 79 records Aug 15 09:30:13 ems-esp32 emsesp 000+00:00:00.000 I 3 Testing syslog with udf-8-chars: €öäü߀öäü߀öäüß Aug 15 09:30:13 ems-esp32 emsesp 000+00:00:03.741 I 5 Starting NTP ``` --- lib/uuid-syslog/src/syslog.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/uuid-syslog/src/syslog.cpp b/lib/uuid-syslog/src/syslog.cpp index 8d8b18625..6f5de8431 100644 --- a/lib/uuid-syslog/src/syslog.cpp +++ b/lib/uuid-syslog/src/syslog.cpp @@ -434,11 +434,23 @@ bool SyslogService::transmit(const QueuedLogMessage & message) { udp_.print('-'); } - udp_.printf_P(PSTR(" %s %s - - - \xEF\xBB\xBF"), hostname_.c_str(), uuid::read_flash_string(message.content_->name).c_str()); + udp_.printf_P(PSTR(" %s %s - - - "), hostname_.c_str(), uuid::read_flash_string(message.content_->name).c_str()); - udp_.print(uuid::log::format_timestamp_ms(message.content_->uptime_ms, 3).c_str()); - udp_.printf_P(PSTR(" %c %lu: "), uuid::log::format_level_char(message.content_->level), message.id_); - udp_.print(message.content_->text.c_str()); + char id_c_str[15]; + snprintf_P(id_c_str, sizeof(id_c_str), PSTR(" %lu "), message.id_); + std::string msgstr = uuid::log::format_timestamp_ms(message.content_->uptime_ms, 3) + + ' ' + + uuid::log::format_level_char(message.content_->level) + + id_c_str + + message.content_->text; + for (uint16_t i = 0; i < msgstr.length(); i++) { + if (msgstr.at(i) & 0x80) { + udp_.print("\xEF\xBB\xBF"); + // udp_.print(""); // marker for testing if BOM is created for udf-8 + break; + } + } + udp_.print(msgstr.c_str()); bool ok = (udp_.endPacket() == 1); last_transmit_ = uuid::get_uptime_ms();