Add json file rotation option (#9783)

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
Jonathan Bennett
2026-03-06 10:24:32 -06:00
committed by GitHub
parent 86dad90573
commit 0ed537a336
4 changed files with 29 additions and 1 deletions

View File

@@ -187,6 +187,7 @@ Logging:
LogLevel: info # debug, info, warn, error
# TraceFile: /var/log/meshtasticd.json
# JSONFile: /packets.json # File location for JSON output of decoded packets
# JSONFileRotate: 60 # Rotate JSON file every N minutes, or 0 for no rotation
# JSONFilter: position # filter for packets to save to JSON file
# AsciiLogs: true # default if not specified is !isatty() on stdout

View File

@@ -16,6 +16,7 @@
#endif
#include "Default.h"
#if ARCH_PORTDUINO
#include "Throttle.h"
#include "platform/portduino/PortduinoGlue.h"
#endif
#if ENABLE_JSON_LOGGING || ARCH_PORTDUINO
@@ -532,6 +533,25 @@ DecodeState perhapsDecode(meshtastic_MeshPacket *p)
if (portduino_config.traceFilename != "" || portduino_config.logoutputlevel == level_trace) {
LOG_TRACE("%s", MeshPacketSerializer::JsonSerialize(p, false).c_str());
} else if (portduino_config.JSONFilename != "") {
if (portduino_config.JSONFileRotate != 0) {
static uint32_t fileage = 0;
if (portduino_config.JSONFileRotate != 0 &&
(fileage == 0 || !Throttle::isWithinTimespanMs(fileage, portduino_config.JSONFileRotate * 60 * 1000))) {
time_t timestamp = time(NULL);
struct tm *timeinfo;
char buffer[80];
timeinfo = localtime(&timestamp);
strftime(buffer, 80, "%Y%m%d-%H%M%S", timeinfo);
std::string datetime(buffer);
if (JSONFile.is_open()) {
JSONFile.close();
}
JSONFile.open(portduino_config.JSONFilename + "_" + datetime, std::ios::out | std::ios::app);
fileage = millis();
}
}
if (portduino_config.JSONFilter == (_meshtastic_PortNum)0 || portduino_config.JSONFilter == p->decoded.portnum) {
JSONFile << MeshPacketSerializer::JsonSerialize(p, false) << std::endl;
}

View File

@@ -630,7 +630,9 @@ void portduinoSetup()
}
} else if (portduino_config.JSONFilename != "") {
try {
JSONFile.open(portduino_config.JSONFilename, std::ios::out | std::ios::app);
if (portduino_config.JSONFileRotate == 0) {
JSONFile.open(portduino_config.JSONFilename, std::ios::out | std::ios::app);
}
} catch (std::ofstream::failure &e) {
std::cout << "*** JSONFile Exception " << e.what() << std::endl;
exit(EXIT_FAILURE);
@@ -687,6 +689,7 @@ bool loadConfig(const char *configPath)
}
portduino_config.traceFilename = yamlConfig["Logging"]["TraceFile"].as<std::string>("");
portduino_config.JSONFilename = yamlConfig["Logging"]["JSONFile"].as<std::string>("");
portduino_config.JSONFileRotate = yamlConfig["Logging"]["JSONFileRotate"].as<int>(0);
portduino_config.JSONFilter = (_meshtastic_PortNum)yamlConfig["Logging"]["JSONFilter"].as<int>(0);
if (yamlConfig["Logging"]["JSONFilter"].as<std::string>("") == "textmessage")
portduino_config.JSONFilter = meshtastic_PortNum_TEXT_MESSAGE_APP;

View File

@@ -165,6 +165,7 @@ extern struct portduino_config_struct {
bool ascii_logs_explicit = false;
std::string JSONFilename;
int JSONFileRotate = 0;
meshtastic_PortNum JSONFilter = (_meshtastic_PortNum)0;
// Webserver
@@ -472,6 +473,9 @@ extern struct portduino_config_struct {
out << YAML::Key << "TraceFile" << YAML::Value << traceFilename;
if (JSONFilename != "") {
out << YAML::Key << "JSONFile" << YAML::Value << JSONFilename;
if (JSONFileRotate != 0)
out << YAML::Key << "JSONFileRotate" << YAML::Value << JSONFileRotate;
if (JSONFilter == meshtastic_PortNum_TEXT_MESSAGE_APP)
out << YAML::Key << "JSONFilter" << YAML::Value << "textmessage";
else if (JSONFilter == meshtastic_PortNum_TELEMETRY_APP)