feat(Logging): prefix SD log lines with wall-clock date/time + uptime

SD lines previously carried only millis(), which is meaningless when reading
a log file after the fact without knowing when boot happened. New format:

  2026-07-15 | 12:28:04 | 13460843 -> 🔵 [DEBG][OutputManager] message

Date/time from time(nullptr), set by NTP (Timekeeper::syncTimeWithNTP) —
works regardless of HAS_RTC. Falls back to "unset-date | unset-time" if NTP
hasn't synced yet, rather than printing a bogus 1970 epoch date. Also added
the level icon to SD lines (previously Serial-only) for faster visual
scanning when reading a raw log file.

F-050 in the feature catalog.
This commit is contained in:
2026-07-15 00:33:05 +03:00
parent e737769887
commit 326ae2b767
3 changed files with 49 additions and 7 deletions
+17
View File
@@ -59,6 +59,7 @@ Every feature — user-facing or significant internal mechanism — is listed he
| F-047 | Per-subsystem MQTT log rate limiting (token bucket + drop summary) | v2.1.2 | Stable | | F-047 | Per-subsystem MQTT log rate limiting (token bucket + drop summary) | v2.1.2 | Stable |
| F-048 | SD log retrieval commands (`logs.list` / `logs.download`) | v2.1.2 | Stable | | F-048 | SD log retrieval commands (`logs.list` / `logs.download`) | v2.1.2 | Stable |
| F-049 | `firmware.status` rollback safety-net diagnostics (retry/failure counts, backup partition) | v2.1.2 | Stable | | F-049 | `firmware.status` rollback safety-net diagnostics (retry/failure counts, backup partition) | v2.1.2 | Stable |
| F-050 | SD log lines prefixed with wall-clock date/time + uptime | v2.1.2 | Stable |
--- ---
@@ -671,3 +672,19 @@ Subsystems: `src/Communication/Handlers/LoggingHandler/LoggingHandler.cpp`, `src
`FirmwareValidator` tracked startup retry count, runtime failure count, and backup partition info internally (NVS-persisted) but never exposed them remotely — only visible via `LOG_*` output. Added public getters (`getStartupRetryCount()`, `getRuntimeFailureCount()`) and extended `firmware.status` with `startup_retry_count`, `max_startup_retries`, `runtime_failure_count`, `max_runtime_failures`, `backup_version`, `backup_is_valid`. `FirmwareValidator` tracked startup retry count, runtime failure count, and backup partition info internally (NVS-persisted) but never exposed them remotely — only visible via `LOG_*` output. Added public getters (`getStartupRetryCount()`, `getRuntimeFailureCount()`) and extended `firmware.status` with `startup_retry_count`, `max_startup_retries`, `runtime_failure_count`, `max_runtime_failures`, `backup_version`, `backup_is_valid`.
Subsystems: `src/FirmwareValidator/FirmwareValidator.hpp`, `src/Communication/Handlers/FirmwareHandler/FirmwareHandler.cpp` Subsystems: `src/FirmwareValidator/FirmwareValidator.hpp`, `src/Communication/Handlers/FirmwareHandler/FirmwareHandler.cpp`
---
### F-050 — SD log lines prefixed with wall-clock date/time + uptime
The SD log line format previously carried only `millis()` — useless for reading a log file after the fact without knowing when boot happened. Lines are now prefixed with real date/time plus uptime:
```
2026-07-15 | 12:28:04 | 13460843 -> 🔵 [DEBG][OutputManager] message
```
Date/time comes from `time(nullptr)`, set by NTP via `Timekeeper::syncTimeWithNTP()` — works regardless of `HAS_RTC`, since NTP sync sets the ESP32 system clock independently of any RTC peripheral. Prints `unset-date | unset-time` instead of a bogus 1970 epoch date if NTP hasn't synced yet (matches the day-rotation file's `unset-date.log` fallback from F-045). The level icon (🔴🟡🟢🔵⚪) — previously Serial-only — is now also written to SD for faster visual scanning.
Serial and MQTT lines are unchanged (Serial already had the icon; MQTT already carries a `timestamp` field, albeit `millis()`-only).
Subsystem: `src/Logging/Logging.cpp`
+31 -6
View File
@@ -1,4 +1,5 @@
#include "Logging.hpp" #include "Logging.hpp"
#include <time.h>
// ═══════════════════════════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════════════════════════
// STATIC MEMBER INITIALIZATION // STATIC MEMBER INITIALIZATION
@@ -290,18 +291,42 @@ void Logging::writeToSd(LogLevel level, const char* levelStr, const char* tag, c
if (isWriting) return; if (isWriting) return;
isWriting = true; isWriting = true;
// Plain text line: [WARN][BellEngine] message (timestamp: 12345ms) // Line: 2026-07-15 | 12:28:04 | 13460843 -> 🔵 [DEBG][Tag] message
// Date/time comes from time(nullptr) — set by NTP (Timekeeper::syncTimeWithNTP)
// regardless of HAS_RTC. Falls back to "unset-date | unset-time" before that
// has run rather than printing a bogus 1970 epoch date.
char dateBuf[11] = "unset-date";
char timeBuf[11] = "unset-time";
time_t now = time(nullptr);
if (now > 0) {
struct tm timeInfo;
gmtime_r(&now, &timeInfo);
snprintf(dateBuf, sizeof(dateBuf), "%04d-%02d-%02d",
timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday);
snprintf(timeBuf, sizeof(timeBuf), "%02d:%02d:%02d",
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec);
}
const char* icon = (level == ERROR) ? "🔴" :
(level == WARNING) ? "🟡" :
(level == INFO) ? "🟢" :
(level == DEBUG) ? "🔵" : "⚪";
String line; String line;
line.reserve(300); line.reserve(350);
line = "["; line = dateBuf;
line += " | ";
line += timeBuf;
line += " | ";
line += millis();
line += " -> ";
line += icon;
line += " [";
line += levelStr; line += levelStr;
line += "]["; line += "][";
line += tag; line += tag;
line += "] "; line += "] ";
line += message; line += message;
line += " (";
line += millis();
line += "ms)";
_sdCallback(line); _sdCallback(line);
+1 -1
View File
@@ -92,7 +92,7 @@ Drops are not silent: the next time that tag is allowed to publish again, a `WAR
``` ```
`timestamp` is `millis()` (ms since boot), not wall-clock time. `timestamp` is `millis()` (ms since boot), not wall-clock time.
**SD line (plain text):** `[LEVL][Tag] message (12345ms)`, written to a day-rotated file — see [FileManager](../FileManager/README.md#log-file-rotation). **SD line (plain text):** `2026-07-15 | 12:28:04 | 13460843 -> 🔵 [DEBG][Tag] message`, written to a day-rotated file — see [FileManager](../FileManager/README.md#log-file-rotation). Date/time comes from `time(nullptr)` (set by NTP via `Timekeeper::syncTimeWithNTP()`, independent of `HAS_RTC`) — printed as `unset-date | unset-time` if no wall clock is available yet. The number after the second `|` is `millis()` (uptime in ms), unaffected by wall-clock availability.
--- ---