mirror of
https://github.com/g4klx/DMRGateway
synced 2025-12-23 06:45:39 +08:00
Ad simple JSON logging.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
29
Utils.cpp
29
Utils.cpp
@@ -17,6 +17,13 @@
|
||||
#include <cstdio>
|
||||
#include <cassert>
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
2
Utils.h
2
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:
|
||||
};
|
||||
|
||||
|
||||
@@ -19,6 +19,6 @@
|
||||
#if !defined(VERSION_H)
|
||||
#define VERSION_H
|
||||
|
||||
const char* VERSION = "20230707";
|
||||
const char* VERSION = "20230711";
|
||||
|
||||
#endif
|
||||
|
||||
13
schema.json
Normal file
13
schema.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"$defs": {
|
||||
"timestamp": {"type": "string"}
|
||||
},
|
||||
|
||||
"status": {
|
||||
"type": "object",
|
||||
"timestamp": {"$ref": "#/$defs/timestamp"},
|
||||
"message": {"type": "string"},
|
||||
"required": ["timestamp", "message"]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user