fix(FileManager,Logging): day-rotated SD logs + boot reset-reason reporting

SD logging was a silent no-op on fresh cards: appendLine() opened /logs/vesper.log
in FILE_APPEND without ever creating the /logs directory first. FileManager now
exposes appendLogLine(), which creates the directory, rotates to YYYY-MM-DD.log
files, and prunes oldest files once a 512KB total budget is exceeded.

LoggingHandler gains logs.list / logs.download commands (chunked, paginated) so
SD logs can be pulled off a device remotely for offline-device troubleshooting.

Telemetry now captures esp_reset_reason() at boot and classifies it as a fault
(panic/watchdog/brownout) or an expected reset (power-on/software/deep sleep).
CommunicationRouter reports it once MQTT connects: faults go to status/alerts,
expected resets go to status/info (type: boot_reason). MQTTDriver gained a
setOnConnectCallback hook to support this.

F-045, F-046, F-048 in the feature catalog.
This commit is contained in:
2026-07-15 00:04:21 +03:00
parent 399b4d056c
commit 2b4fd7c5b0
16 changed files with 419 additions and 22 deletions
@@ -6,9 +6,14 @@
*/
#include "CommunicationRouter.hpp"
#include <time.h>
#define TAG "CommRouter"
// Total size budget for /logs on SD, across all rotated daily files.
// Oldest day is pruned first once this is exceeded.
static constexpr size_t SD_LOG_MAX_TOTAL_BYTES = 512 * 1024; // 512KB
#include "../../ConfigManager/ConfigManager.hpp"
#include "../../OTAManager/OTAManager.hpp"
#include "../../Networking/Networking.hpp"
@@ -124,6 +129,10 @@ void CommunicationRouter::begin() {
Logging::setSubsystemMqttLevel("Telemetry", Logging::INFO); // load data useful
Logging::setSubsystemMqttLevel("OTAManager", Logging::INFO); // update events important
_mqttClient.setOnConnectCallback([this](bool /*sessionPresent*/) {
reportBootReasonOnce();
});
LOG_INFO(TAG, "MQTT transport initialised");
// ── HTTP ─────────────────────────────────────────────────────────────────
@@ -191,9 +200,11 @@ void CommunicationRouter::setFileManagerReference(FileManager* fm) {
if (fm != nullptr) {
Logging::setSdWriteCallback([fm](const String& line) {
fm->appendLine("/logs/vesper.log", line);
// Day-rotated: /logs/YYYY-MM-DD.log, capped at SD_LOG_MAX_TOTAL_BYTES
// total across the directory (oldest day pruned first).
fm->appendLogLine("/logs", line, time(nullptr), SD_LOG_MAX_TOTAL_BYTES);
});
LOG_INFO(TAG, "SD log channel registered -> /logs/vesper.log");
LOG_INFO(TAG, "SD log channel registered -> /logs/YYYY-MM-DD.log (cap: %u bytes)", (unsigned)SD_LOG_MAX_TOTAL_BYTES);
}
}
@@ -281,8 +292,8 @@ void CommunicationRouter::registerHandlers() {
_bus.registerHandler(std::make_unique<FileHandler>(*_fileManager));
}
// Logging — no optional dependencies
_bus.registerHandler(std::make_unique<LoggingHandler>(_configManager));
// Logging — FileManager optional; logs.list/logs.download degrade gracefully without it
_bus.registerHandler(std::make_unique<LoggingHandler>(_configManager, _fileManager));
// Telemetry
if (_telemetry) {
@@ -489,6 +500,23 @@ void CommunicationRouter::publishAlert(const String& subsystem, const String& ne
LOG_DEBUG(TAG, "Alert published: %s → %s", subsystem.c_str(), newState.c_str());
}
void CommunicationRouter::reportBootReasonOnce() {
if (_bootReasonReported || !_telemetry) return;
_bootReasonReported = true;
String reason = _telemetry->getResetReasonString();
if (_telemetry->isFaultReset()) {
publishAlert("System", "WARNING", "Device reset due to fault: " + reason);
LOG_WARNING(TAG, "Fault reset reported: %s", reason.c_str());
} else {
JsonDocument payload;
payload["reason"] = reason;
publishInfo("boot_reason", payload);
LOG_INFO(TAG, "Boot reason reported: %s", reason.c_str());
}
}
void CommunicationRouter::publishInfo(const String& type, const JsonDocument& payload) {
if (!_mqttClient.isConnected()) return;