Add basic JSON logging.

This commit is contained in:
Jonathan Naylor 2023-07-11 11:25:39 +01:00
parent f0a271ae53
commit 97db2e9b45
5 changed files with 66 additions and 16 deletions

View file

@ -22,8 +22,9 @@
#include "Version.h"
#include "Thread.h"
#include "Timer.h"
#include "Log.h"
#include "REGEX.h"
#include "Utils.h"
#include "Log.h"
#include "GitVersion.h"
#include <regex>
@ -260,8 +261,6 @@ int CDAPNETGateway::run()
ret = m_pocsagNetwork->open();
if (!ret) {
::LogError("Cannot open the repeater network port");
::LogFinalise();
return 1;
}
@ -272,8 +271,6 @@ int CDAPNETGateway::run()
if (dapnetAuthKey.length() == 0 || dapnetAuthKey == "TOPSECRET") {
::LogError("AuthKey not set or invalid");
::LogFinalise();
return 1;
}
@ -283,10 +280,7 @@ int CDAPNETGateway::run()
m_pocsagNetwork->close();
delete m_pocsagNetwork;
delete m_dapnetNetwork;
::LogError("Cannot open the DAPNET network port");
::LogFinalise();
return 1;
}
@ -296,10 +290,7 @@ int CDAPNETGateway::run()
m_dapnetNetwork->close();
delete m_pocsagNetwork;
delete m_dapnetNetwork;
::LogError("Cannot login to the DAPNET network");
::LogFinalise();
return 1;
}
@ -319,8 +310,10 @@ int CDAPNETGateway::run()
if (m_regexWhitelist->load())
regexWhitelist = m_regexWhitelist->get();
LogMessage("DAPNETGateway-%s is starting", VERSION);
LogMessage("Built %s %s (GitID #%.7s)", __TIME__, __DATE__, gitversion);
LogInfo("DAPNETGateway-%s is starting", VERSION);
LogInfo("Built %s %s (GitID #%.7s)", __TIME__, __DATE__, gitversion);
writeJSONStatus("DAPNETGateway is starting");
for (;;) {
unsigned char buffer[200U];
@ -431,14 +424,15 @@ int CDAPNETGateway::run()
CThread::sleep(10U);
}
LogInfo("DAPNETGateway is stopping");
writeJSONStatus("DAPNETGateway is stopping");
m_pocsagNetwork->close();
delete m_pocsagNetwork;
m_dapnetNetwork->close();
delete m_dapnetNetwork;
::LogFinalise();
return 0;
}
@ -624,3 +618,14 @@ bool CDAPNETGateway::sendMessage(CPOCSAGMessage* message) const
return true;
}
}
void CDAPNETGateway::writeJSONStatus(const std::string& status)
{
nlohmann::json json;
json["timestamp"] = CUtils::createTimestamp();
json["message"] = status;
WriteJSON("status", json);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 by Jonathan Naylor G4KLX
* Copyright (C) 2018,2023 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -60,6 +60,8 @@ private:
unsigned int calculateCodewords(const CPOCSAGMessage* message) const;
void loadSchedule();
bool sendMessage(CPOCSAGMessage* message) const;
void writeJSONStatus(const std::string& status);
};
#endif

View file

@ -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);
@ -70,3 +77,24 @@ void CUtils::dump(int level, const std::string& title, const unsigned char* data
}
}
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;
}

View file

@ -21,6 +21,8 @@ public:
static void dump(const std::string& title, const unsigned char* data, unsigned int length);
static void dump(int level, const std::string& title, const unsigned char* data, unsigned int length);
static std::string createTimestamp();
private:
};

13
schema.json Normal file
View file

@ -0,0 +1,13 @@
{
"$defs": {
"timestamp": {"type": "string"}
},
"status": {
"type": "object",
"timestamp": {"$ref": "#/$defs/timestamp"},
"message": {"type": "string"},
"required": ["timestamp", "message"]
}
}