From 852caca96e06aed1513f68be4aba28f33ed09bf2 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Tue, 11 Jul 2023 16:54:30 +0100 Subject: [PATCH] Ad simple JSON logging. --- DMRGateway.cpp | 24 +++++++++++++++++++++--- DMRGateway.h | 2 ++ Utils.cpp | 29 +++++++++++++++++++++++++++++ Utils.h | 2 ++ Version.h | 2 +- schema.json | 13 +++++++++++++ 6 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 schema.json diff --git a/DMRGateway.cpp b/DMRGateway.cpp index 73bcd38..16f3682 100644 --- a/DMRGateway.cpp +++ b/DMRGateway.cpp @@ -31,6 +31,7 @@ #include "Version.h" #include "Thread.h" #include "DMRLC.h" +#include "Utils.h" #include "Sync.h" #include "Log.h" #include "GitVersion.h" @@ -106,6 +107,7 @@ int main(int argc, char** argv) int ret = 0; do { + m_killed = false; m_signal = 0; gateway = new CDMRGateway(std::string(iniFile)); @@ -379,8 +381,10 @@ int CDMRGateway::run() LogInfo(HEADER3); LogInfo(HEADER4); - LogMessage("DMRGateway-%s is starting", VERSION); - LogMessage("Built %s %s (GitID #%.7s)", __TIME__, __DATE__, gitversion); + LogInfo("DMRGateway-%s is starting", VERSION); + LogInfo("Built %s %s (GitID #%.7s)", __TIME__, __DATE__, gitversion); + + writeJSONStatus("DMRGateway is starting"); ret = createMMDVM(); if (!ret) @@ -524,7 +528,8 @@ int CDMRGateway::run() CStopWatch stopWatch; stopWatch.start(); - LogMessage("DMRGateway-%s is running", VERSION); + LogInfo("DMRGateway-%s is starting", VERSION); + LogInfo("Built %s %s (GitID #%.7s)", __TIME__, __DATE__, gitversion); while (!m_killed) { if (m_networkXlxEnabled && (m_xlxNetwork != NULL)) { @@ -1261,6 +1266,9 @@ int CDMRGateway::run() CThread::sleep(10U); } + LogInfo("DMRGateway is stopping"); + writeJSONStatus("DMRGateway is stopping"); + delete m_xlxVoice; m_repeater->close(); @@ -2640,6 +2648,16 @@ void CDMRGateway::buildNetworkHostNetworkString(std::string &str, const std::str } } +void CDMRGateway::writeJSONStatus(const std::string& status) +{ + nlohmann::json json; + + json["timestamp"] = CUtils::createTimestamp(); + json["message"] = status; + + WriteJSON("status", json); +} + void CDMRGateway::onDynamic(const unsigned char* message, unsigned int length) { assert(gateway != NULL); diff --git a/DMRGateway.h b/DMRGateway.h index 0f97728..9b669bb 100644 --- a/DMRGateway.h +++ b/DMRGateway.h @@ -157,6 +157,8 @@ private: void buildNetworkStatusNetworkString(std::string &str, const std::string& name, CDMRNetwork* network, bool enabled); void buildNetworkHostNetworkString(std::string &str, const std::string& name, CDMRNetwork* network); + void writeJSONStatus(const std::string& status); + static void onCommand(const unsigned char* message, unsigned int length); static void onDynamic(const unsigned char* message, unsigned int length); }; diff --git a/Utils.cpp b/Utils.cpp index 49ded13..757f9fe 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -17,6 +17,13 @@ #include #include +#if defined(_WIN32) || defined(_WIN64) +#include +#else +#include +#include +#endif + void CUtils::dump(const std::string& title, const unsigned char* data, unsigned int length) { assert(data != NULL); @@ -144,3 +151,25 @@ void CUtils::bitsToByteLE(const bool* bits, unsigned char& byte) byte |= bits[6U] ? 0x40U : 0x00U; byte |= bits[7U] ? 0x80U : 0x00U; } + +std::string CUtils::createTimestamp() +{ + char buffer[100U]; + +#if defined(_WIN32) || defined(_WIN64) + SYSTEMTIME st; + ::GetSystemTime(&st); + + ::sprintf(buffer, "%04u-%02u-%02u %02u:%02u:%02u.%03u", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); +#else + struct timeval now; + ::gettimeofday(&now, NULL); + + struct tm* tm = ::gmtime(&now.tv_sec); + + ::sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d.%03lld", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000LL); +#endif + + return buffer; +} + diff --git a/Utils.h b/Utils.h index ade28c0..458e2ef 100644 --- a/Utils.h +++ b/Utils.h @@ -30,6 +30,8 @@ public: static void bitsToByteBE(const bool* bits, unsigned char& byte); static void bitsToByteLE(const bool* bits, unsigned char& byte); + static std::string createTimestamp(); + private: }; diff --git a/Version.h b/Version.h index a971c36..05ca373 100644 --- a/Version.h +++ b/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20230707"; +const char* VERSION = "20230711"; #endif diff --git a/schema.json b/schema.json new file mode 100644 index 0000000..58be79f --- /dev/null +++ b/schema.json @@ -0,0 +1,13 @@ +{ + "$defs": { + "timestamp": {"type": "string"} + }, + + "status": { + "type": "object", + "timestamp": {"$ref": "#/$defs/timestamp"}, + "message": {"type": "string"}, + "required": ["timestamp", "message"] + } +} +