mirror of
https://github.com/esphome/esphome.git
synced 2026-01-16 15:04:48 -07:00
Compare commits
2 Commits
uart_debug
...
syslog_snp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
22882abbe7 | ||
|
|
88e1295e2f |
@@ -47,29 +47,27 @@ void Syslog::log_(const int level, const char *tag, const char *message, size_t
|
||||
size_t remaining = sizeof(packet);
|
||||
|
||||
// Write PRI - abort if this fails as packet would be malformed
|
||||
int ret = snprintf(packet, remaining, "<%d>", pri);
|
||||
if (ret <= 0 || static_cast<size_t>(ret) >= remaining) {
|
||||
return;
|
||||
offset = buf_append_printf(packet, sizeof(packet), 0, "<%d>", pri);
|
||||
if (offset == 0) {
|
||||
return; // PRI always produces at least "<0>" (3 chars), so 0 means error
|
||||
}
|
||||
offset = ret;
|
||||
remaining -= ret;
|
||||
remaining -= offset;
|
||||
|
||||
// Write timestamp directly into packet (RFC 5424: use "-" if time not valid or strftime fails)
|
||||
auto now = this->time_->now();
|
||||
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--;
|
||||
}
|
||||
|
||||
// Write hostname, tag, and message
|
||||
ret = snprintf(packet + offset, remaining, " %s %s: %.*s", App.get_name().c_str(), tag, (int) len, message);
|
||||
if (ret > 0) {
|
||||
// snprintf returns chars that would be written; clamp to actual buffer space
|
||||
offset += std::min(static_cast<size_t>(ret), remaining > 0 ? remaining - 1 : 0);
|
||||
offset = buf_append_printf(packet, sizeof(packet), offset, " %s %s: %.*s", App.get_name().c_str(), tag, (int) len,
|
||||
message);
|
||||
// Clamp to exclude null terminator position if buffer was filled
|
||||
if (offset >= sizeof(packet)) {
|
||||
offset = sizeof(packet) - 1;
|
||||
}
|
||||
|
||||
if (offset > 0) {
|
||||
|
||||
@@ -107,7 +107,7 @@ void UARTDebug::log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uin
|
||||
if (i > 0) {
|
||||
res += separator;
|
||||
}
|
||||
buf_append_printf(buf, sizeof(buf), 0, "%02X", bytes[i]);
|
||||
sprintf(buf, "%02X", bytes[i]);
|
||||
res += buf;
|
||||
}
|
||||
ESP_LOGD(TAG, "%s", res.c_str());
|
||||
@@ -147,7 +147,7 @@ void UARTDebug::log_string(UARTDirection direction, std::vector<uint8_t> bytes)
|
||||
} else if (bytes[i] == 92) {
|
||||
res += "\\\\";
|
||||
} else if (bytes[i] < 32 || bytes[i] > 127) {
|
||||
buf_append_printf(buf, sizeof(buf), 0, "\\x%02X", bytes[i]);
|
||||
sprintf(buf, "\\x%02X", bytes[i]);
|
||||
res += buf;
|
||||
} else {
|
||||
res += bytes[i];
|
||||
@@ -189,7 +189,7 @@ void UARTDebug::log_binary(UARTDirection direction, std::vector<uint8_t> bytes,
|
||||
if (i > 0) {
|
||||
res += separator;
|
||||
}
|
||||
buf_append_printf(buf, sizeof(buf), 0, "0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", BYTE_TO_BINARY(bytes[i]), bytes[i]);
|
||||
sprintf(buf, "0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", BYTE_TO_BINARY(bytes[i]), bytes[i]);
|
||||
res += buf;
|
||||
}
|
||||
ESP_LOGD(TAG, "%s", res.c_str());
|
||||
|
||||
Reference in New Issue
Block a user