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:
@@ -1,14 +1,25 @@
|
||||
#include "LoggingHandler.hpp"
|
||||
#include "../../../ConfigManager/ConfigManager.hpp"
|
||||
#include "../../../Logging/Logging.hpp"
|
||||
#include "../../../FileManager/FileManager.hpp"
|
||||
|
||||
#define TAG "LoggingHandler"
|
||||
|
||||
LoggingHandler::LoggingHandler(ConfigManager& configManager)
|
||||
: _configManager(configManager) {}
|
||||
// Directory where day-rotated SD logs live — must match CommunicationRouter's
|
||||
// setFileManagerReference wiring (Logging.hpp SD channel).
|
||||
static const char* SD_LOG_DIR = "/logs";
|
||||
|
||||
// Cap a single logs.download response chunk — keeps every response small
|
||||
// regardless of file size, matching the JSON-payload sizes used elsewhere
|
||||
// (MQTT/WS/HTTP transports).
|
||||
static constexpr size_t LOGS_DOWNLOAD_MAX_CHUNK_BYTES = 4096;
|
||||
|
||||
LoggingHandler::LoggingHandler(ConfigManager& configManager, FileManager* fileManager)
|
||||
: _configManager(configManager), _fileManager(fileManager) {}
|
||||
|
||||
std::vector<String> LoggingHandler::commands() const {
|
||||
return { "log.set_serial", "log.set_sd", "log.set_mqtt", "log.get_config" };
|
||||
return { "log.set_serial", "log.set_sd", "log.set_mqtt", "log.get_config",
|
||||
"logs.list", "logs.download" };
|
||||
}
|
||||
|
||||
CommandResult LoggingHandler::execute(const CommandEnvelope& envelope) {
|
||||
@@ -16,6 +27,8 @@ CommandResult LoggingHandler::execute(const CommandEnvelope& envelope) {
|
||||
if (envelope.command == "log.set_sd") return handleSetLevel(envelope, "sd");
|
||||
if (envelope.command == "log.set_mqtt") return handleSetLevel(envelope, "mqtt");
|
||||
if (envelope.command == "log.get_config") return handleGetConfig(envelope);
|
||||
if (envelope.command == "logs.list") return handleLogsList(envelope);
|
||||
if (envelope.command == "logs.download") return handleLogsDownload(envelope);
|
||||
return CommandResult::err(envelope.command, "Unknown log command");
|
||||
}
|
||||
|
||||
@@ -60,3 +73,49 @@ CommandResult LoggingHandler::handleGetConfig(const CommandEnvelope& e) {
|
||||
doc["mqtt_level"] = _configManager.getMqttLogLevel();
|
||||
return CommandResult::ok("log.get_config", doc);
|
||||
}
|
||||
|
||||
CommandResult LoggingHandler::handleLogsList(const CommandEnvelope& e) {
|
||||
if (!_fileManager) {
|
||||
return CommandResult::err("logs.list", "SD card not available on this device");
|
||||
}
|
||||
|
||||
String listJson = _fileManager->listLogFilesAsJson(SD_LOG_DIR);
|
||||
JsonDocument doc;
|
||||
DeserializationError err = deserializeJson(doc, listJson);
|
||||
if (err) {
|
||||
LOG_ERROR(TAG, "Failed to parse log file list JSON: %s", err.c_str());
|
||||
return CommandResult::err("logs.list", "Failed to read log directory");
|
||||
}
|
||||
|
||||
JsonDocument result;
|
||||
result["files"] = doc["files"];
|
||||
return CommandResult::ok("logs.list", result);
|
||||
}
|
||||
|
||||
CommandResult LoggingHandler::handleLogsDownload(const CommandEnvelope& e) {
|
||||
if (!_fileManager) {
|
||||
return CommandResult::err("logs.download", "SD card not available on this device");
|
||||
}
|
||||
|
||||
if (!e.contents["file"].is<const char*>()) {
|
||||
return CommandResult::err("logs.download", "Missing required field: file");
|
||||
}
|
||||
|
||||
String fileName = e.contents["file"].as<String>();
|
||||
size_t offset = e.contents["offset"].is<size_t>() ? e.contents["offset"].as<size_t>() : 0;
|
||||
|
||||
String chunkJson = _fileManager->readLogChunkAsJson(SD_LOG_DIR, fileName, offset,
|
||||
LOGS_DOWNLOAD_MAX_CHUNK_BYTES);
|
||||
JsonDocument doc;
|
||||
DeserializationError err = deserializeJson(doc, chunkJson);
|
||||
if (err || doc.isNull()) {
|
||||
return CommandResult::err("logs.download", "Log file not found: " + fileName);
|
||||
}
|
||||
|
||||
JsonDocument result;
|
||||
result["file"] = fileName;
|
||||
result["data"] = doc["data"];
|
||||
result["next_offset"] = doc["next_offset"];
|
||||
result["eof"] = doc["eof"];
|
||||
return CommandResult::ok("logs.download", result);
|
||||
}
|
||||
|
||||
@@ -4,17 +4,21 @@
|
||||
#include "../ICommandHandler.hpp"
|
||||
|
||||
class ConfigManager;
|
||||
class FileManager;
|
||||
|
||||
class LoggingHandler : public ICommandHandler {
|
||||
public:
|
||||
explicit LoggingHandler(ConfigManager& configManager);
|
||||
explicit LoggingHandler(ConfigManager& configManager, FileManager* fileManager = nullptr);
|
||||
|
||||
std::vector<String> commands() const override;
|
||||
CommandResult execute(const CommandEnvelope& envelope) override;
|
||||
|
||||
private:
|
||||
ConfigManager& _configManager;
|
||||
FileManager* _fileManager; // nullptr on boards without SD — logs.* commands degrade gracefully
|
||||
|
||||
CommandResult handleSetLevel(const CommandEnvelope& e, const String& channel);
|
||||
CommandResult handleGetConfig(const CommandEnvelope& e);
|
||||
CommandResult handleLogsList(const CommandEnvelope& e);
|
||||
CommandResult handleLogsDownload(const CommandEnvelope& e);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user