Add json file rotation option (#9783)
Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
co-authored by
GitHub
Ben Meadors
parent
86dad90573
commit
0ed537a336
@@ -187,6 +187,7 @@ Logging:
|
|||||||
LogLevel: info # debug, info, warn, error
|
LogLevel: info # debug, info, warn, error
|
||||||
# TraceFile: /var/log/meshtasticd.json
|
# TraceFile: /var/log/meshtasticd.json
|
||||||
# JSONFile: /packets.json # File location for JSON output of decoded packets
|
# 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
|
# JSONFilter: position # filter for packets to save to JSON file
|
||||||
# AsciiLogs: true # default if not specified is !isatty() on stdout
|
# AsciiLogs: true # default if not specified is !isatty() on stdout
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#endif
|
#endif
|
||||||
#include "Default.h"
|
#include "Default.h"
|
||||||
#if ARCH_PORTDUINO
|
#if ARCH_PORTDUINO
|
||||||
|
#include "Throttle.h"
|
||||||
#include "platform/portduino/PortduinoGlue.h"
|
#include "platform/portduino/PortduinoGlue.h"
|
||||||
#endif
|
#endif
|
||||||
#if ENABLE_JSON_LOGGING || ARCH_PORTDUINO
|
#if ENABLE_JSON_LOGGING || ARCH_PORTDUINO
|
||||||
@@ -532,6 +533,25 @@ DecodeState perhapsDecode(meshtastic_MeshPacket *p)
|
|||||||
if (portduino_config.traceFilename != "" || portduino_config.logoutputlevel == level_trace) {
|
if (portduino_config.traceFilename != "" || portduino_config.logoutputlevel == level_trace) {
|
||||||
LOG_TRACE("%s", MeshPacketSerializer::JsonSerialize(p, false).c_str());
|
LOG_TRACE("%s", MeshPacketSerializer::JsonSerialize(p, false).c_str());
|
||||||
} else if (portduino_config.JSONFilename != "") {
|
} 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(×tamp);
|
||||||
|
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) {
|
if (portduino_config.JSONFilter == (_meshtastic_PortNum)0 || portduino_config.JSONFilter == p->decoded.portnum) {
|
||||||
JSONFile << MeshPacketSerializer::JsonSerialize(p, false) << std::endl;
|
JSONFile << MeshPacketSerializer::JsonSerialize(p, false) << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -630,7 +630,9 @@ void portduinoSetup()
|
|||||||
}
|
}
|
||||||
} else if (portduino_config.JSONFilename != "") {
|
} else if (portduino_config.JSONFilename != "") {
|
||||||
try {
|
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) {
|
} catch (std::ofstream::failure &e) {
|
||||||
std::cout << "*** JSONFile Exception " << e.what() << std::endl;
|
std::cout << "*** JSONFile Exception " << e.what() << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@@ -687,6 +689,7 @@ bool loadConfig(const char *configPath)
|
|||||||
}
|
}
|
||||||
portduino_config.traceFilename = yamlConfig["Logging"]["TraceFile"].as<std::string>("");
|
portduino_config.traceFilename = yamlConfig["Logging"]["TraceFile"].as<std::string>("");
|
||||||
portduino_config.JSONFilename = yamlConfig["Logging"]["JSONFile"].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);
|
portduino_config.JSONFilter = (_meshtastic_PortNum)yamlConfig["Logging"]["JSONFilter"].as<int>(0);
|
||||||
if (yamlConfig["Logging"]["JSONFilter"].as<std::string>("") == "textmessage")
|
if (yamlConfig["Logging"]["JSONFilter"].as<std::string>("") == "textmessage")
|
||||||
portduino_config.JSONFilter = meshtastic_PortNum_TEXT_MESSAGE_APP;
|
portduino_config.JSONFilter = meshtastic_PortNum_TEXT_MESSAGE_APP;
|
||||||
|
|||||||
@@ -165,6 +165,7 @@ extern struct portduino_config_struct {
|
|||||||
bool ascii_logs_explicit = false;
|
bool ascii_logs_explicit = false;
|
||||||
|
|
||||||
std::string JSONFilename;
|
std::string JSONFilename;
|
||||||
|
int JSONFileRotate = 0;
|
||||||
meshtastic_PortNum JSONFilter = (_meshtastic_PortNum)0;
|
meshtastic_PortNum JSONFilter = (_meshtastic_PortNum)0;
|
||||||
|
|
||||||
// Webserver
|
// Webserver
|
||||||
@@ -472,6 +473,9 @@ extern struct portduino_config_struct {
|
|||||||
out << YAML::Key << "TraceFile" << YAML::Value << traceFilename;
|
out << YAML::Key << "TraceFile" << YAML::Value << traceFilename;
|
||||||
if (JSONFilename != "") {
|
if (JSONFilename != "") {
|
||||||
out << YAML::Key << "JSONFile" << YAML::Value << 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)
|
if (JSONFilter == meshtastic_PortNum_TEXT_MESSAGE_APP)
|
||||||
out << YAML::Key << "JSONFilter" << YAML::Value << "textmessage";
|
out << YAML::Key << "JSONFilter" << YAML::Value << "textmessage";
|
||||||
else if (JSONFilter == meshtastic_PortNum_TELEMETRY_APP)
|
else if (JSONFilter == meshtastic_PortNum_TELEMETRY_APP)
|
||||||
|
|||||||
Reference in New Issue
Block a user