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
+26 -3
View File
@@ -23,6 +23,9 @@
#include <HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <vector>
#include <algorithm>
#include <time.h>
#include "../Logging/Logging.hpp"
#include "../ConfigManager/ConfigManager.hpp"
@@ -51,18 +54,38 @@ public:
// Append a single text line to a file (used by SD log channel)
bool appendLine(const String& filePath, const String& line);
// Append a line to a day-rotated log file under logDir (e.g. "/logs").
// Creates the directory if missing, names the file YYYY-MM-DD.log using
// `timestamp` (falls back to a fixed bucket if timestamp <= 0, i.e. no
// wall clock yet), and prunes oldest dated files once the directory
// exceeds maxTotalBytes.
bool appendLogLine(const String& logDir, const String& line, time_t timestamp,
size_t maxTotalBytes);
// List dated log files under logDir as JSON: {"files":[{"name":"2026-07-14.log","size":1234}, ...]}
String listLogFilesAsJson(const String& logDir);
// Read a bounded chunk of a log file for remote retrieval (paginated —
// callers loop, feeding next_offset back in, until eof is true).
// Returns JSON: {"data":"<raw text chunk>","next_offset":N,"eof":bool}
// Empty JSON object on error (file not found, etc).
String readLogChunkAsJson(const String& logDir, const String& fileName,
size_t offset, size_t maxBytes);
// ═══════════════════════════════════════════════════════════════════════════════
// HEALTH CHECK METHOD
// ═══════════════════════════════════════════════════════════════════════════════
/** @brief Check if FileManager is in healthy state */
bool isHealthy() const;
private:
// Helper functions
bool initializeSD();
bool ensureDirectoryExists(const String& dirPath);
String logFileNameForTimestamp(time_t timestamp) const;
void pruneLogDirectory(const String& logDir, size_t maxTotalBytes);
};
#endif