From ce5dfaf2402a6b8ab0d91d50a31b8c63b1413f14 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Sun, 1 Nov 2020 12:45:51 +0000 Subject: [PATCH] Add optional log file rotation and fix UDP issue. --- NXDNGateway/Conf.cpp | 8 ++++++ NXDNGateway/Conf.h | 2 ++ NXDNGateway/Log.cpp | 43 +++++++++++++++++++++++++++++++-- NXDNGateway/Log.h | 4 +-- NXDNGateway/NXDNGateway.cpp | 4 +-- NXDNGateway/NXDNGateway.ini | 1 + NXDNGateway/UDPSocket.cpp | 6 +++++ NXDNGateway/Version.h | 2 +- NXDNParrot/UDPSocket.cpp | 6 +++++ NXDNParrot/Version.h | 2 +- NXDNReflector/Conf.cpp | 8 ++++++ NXDNReflector/Conf.h | 2 ++ NXDNReflector/Log.cpp | 43 +++++++++++++++++++++++++++++++-- NXDNReflector/Log.h | 4 +-- NXDNReflector/NXDNReflector.cpp | 4 +-- NXDNReflector/NXDNReflector.ini | 1 + NXDNReflector/UDPSocket.cpp | 6 +++++ NXDNReflector/Version.h | 2 +- 18 files changed, 133 insertions(+), 15 deletions(-) diff --git a/NXDNGateway/Conf.cpp b/NXDNGateway/Conf.cpp index a134377..358c114 100644 --- a/NXDNGateway/Conf.cpp +++ b/NXDNGateway/Conf.cpp @@ -66,6 +66,7 @@ m_logDisplayLevel(0U), m_logFileLevel(0U), m_logFilePath(), m_logFileRoot(), +m_logFileRotate(true), m_aprsEnabled(false), m_aprsAddress("127.0.0.1"), m_aprsPort(8673U), @@ -221,6 +222,8 @@ bool CConf::read() m_logFileLevel = (unsigned int)::atoi(value); else if (::strcmp(key, "DisplayLevel") == 0) m_logDisplayLevel = (unsigned int)::atoi(value); + else if (::strcmp(key, "FileRotate") == 0) + m_logFileRotate = ::atoi(value) == 1; } else if (section == SECTION_APRS) { if (::strcmp(key, "Enable") == 0) m_aprsEnabled = ::atoi(value) == 1; @@ -432,6 +435,11 @@ std::string CConf::getLogFileRoot() const return m_logFileRoot; } +bool CConf::getLogFileRotate() const +{ + return m_logFileRotate; +} + unsigned int CConf::getNetworkPort() const { return m_networkPort; diff --git a/NXDNGateway/Conf.h b/NXDNGateway/Conf.h index 57df9f2..ca217af 100644 --- a/NXDNGateway/Conf.h +++ b/NXDNGateway/Conf.h @@ -71,6 +71,7 @@ public: unsigned int getLogFileLevel() const; std::string getLogFilePath() const; std::string getLogFileRoot() const; + bool getLogFileRotate() const; // The Network section unsigned int getNetworkPort() const; @@ -126,6 +127,7 @@ private: unsigned int m_logFileLevel; std::string m_logFilePath; std::string m_logFileRoot; + bool m_logFileRotate; bool m_aprsEnabled; std::string m_aprsAddress; diff --git a/NXDNGateway/Log.cpp b/NXDNGateway/Log.cpp index 1d5ad29..5d80ca4 100644 --- a/NXDNGateway/Log.cpp +++ b/NXDNGateway/Log.cpp @@ -35,6 +35,7 @@ static unsigned int m_fileLevel = 2U; static std::string m_filePath; static std::string m_fileRoot; +static bool m_fileRotate = true; static FILE* m_fpLog = NULL; static bool m_daemon = false; @@ -45,7 +46,7 @@ static struct tm m_tm; static char LEVELS[] = " DMIWEF"; -static bool LogOpen() +static bool logOpenRotate() { bool status = false; @@ -86,13 +87,51 @@ static bool LogOpen() return status; } -bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel) +static bool logOpenNoRotate() +{ + bool status = false; + + if (m_fileLevel == 0U) + return true; + + if (m_fpLog != NULL) + return true; + + char filename[200U]; +#if defined(_WIN32) || defined(_WIN64) + ::sprintf(filename, "%s\\%s.log", m_filePath.c_str(), m_fileRoot.c_str()); +#else + ::sprintf(filename, "%s/%s.log", m_filePath.c_str(), m_fileRoot.c_str()); +#endif + + if ((m_fpLog = ::fopen(filename, "a+t")) != NULL) { + status = true; + +#if !defined(_WIN32) && !defined(_WIN64) + if (m_daemon) + dup2(fileno(m_fpLog), fileno(stderr)); +#endif + } + + return status; +} + +bool LogOpen() +{ + if (m_fileRotate) + return logOpenRotate(); + else + return logOpenNoRotate(); +} + +bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, bool rotate) { m_filePath = filePath; m_fileRoot = fileRoot; m_fileLevel = fileLevel; m_displayLevel = displayLevel; m_daemon = daemon; + m_fileRotate = rotate; if (m_daemon) m_displayLevel = 0U; diff --git a/NXDNGateway/Log.h b/NXDNGateway/Log.h index 0d00653..ae95b60 100644 --- a/NXDNGateway/Log.h +++ b/NXDNGateway/Log.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2020 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 @@ -30,7 +30,7 @@ extern void Log(unsigned int level, const char* fmt, ...); -extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel); +extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, bool rotate); extern void LogFinalise(); #endif diff --git a/NXDNGateway/NXDNGateway.cpp b/NXDNGateway/NXDNGateway.cpp index dde6e95..777f7ca 100644 --- a/NXDNGateway/NXDNGateway.cpp +++ b/NXDNGateway/NXDNGateway.cpp @@ -174,9 +174,9 @@ void CNXDNGateway::run() #endif #if !defined(_WIN32) && !defined(_WIN64) - ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); + ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate()); #else - ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); + ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate()); #endif if (!ret) { ::fprintf(stderr, "NXDNGateway: unable to open the log file\n"); diff --git a/NXDNGateway/NXDNGateway.ini b/NXDNGateway/NXDNGateway.ini index 33bae16..9480fa4 100644 --- a/NXDNGateway/NXDNGateway.ini +++ b/NXDNGateway/NXDNGateway.ini @@ -51,6 +51,7 @@ DisplayLevel=1 FileLevel=1 FilePath=. FileRoot=NXDNGateway +FileRotate=1 [Network] Port=14050 diff --git a/NXDNGateway/UDPSocket.cpp b/NXDNGateway/UDPSocket.cpp index 3f6192a..510c8a2 100644 --- a/NXDNGateway/UDPSocket.cpp +++ b/NXDNGateway/UDPSocket.cpp @@ -289,6 +289,12 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag LogError("Error returned from recvfrom, err: %lu", ::GetLastError()); #else LogError("Error returned from recvfrom, err: %d", errno); + + if (len == -1 && errno == ENOTSOCK) { + LogMessage("Re-opening UDP port on %u", m_port); + close(); + open(); + } #endif return -1; } diff --git a/NXDNGateway/Version.h b/NXDNGateway/Version.h index 2953473..8a542ec 100644 --- a/NXDNGateway/Version.h +++ b/NXDNGateway/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20201027"; +const char* VERSION = "20201101"; #endif diff --git a/NXDNParrot/UDPSocket.cpp b/NXDNParrot/UDPSocket.cpp index 3f6192a..510c8a2 100644 --- a/NXDNParrot/UDPSocket.cpp +++ b/NXDNParrot/UDPSocket.cpp @@ -289,6 +289,12 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag LogError("Error returned from recvfrom, err: %lu", ::GetLastError()); #else LogError("Error returned from recvfrom, err: %d", errno); + + if (len == -1 && errno == ENOTSOCK) { + LogMessage("Re-opening UDP port on %u", m_port); + close(); + open(); + } #endif return -1; } diff --git a/NXDNParrot/Version.h b/NXDNParrot/Version.h index 10e1e36..6e33237 100644 --- a/NXDNParrot/Version.h +++ b/NXDNParrot/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20200920"; +const char* VERSION = "20201101"; #endif diff --git a/NXDNReflector/Conf.cpp b/NXDNReflector/Conf.cpp index 7d3b7c5..6593fa3 100644 --- a/NXDNReflector/Conf.cpp +++ b/NXDNReflector/Conf.cpp @@ -46,6 +46,7 @@ m_logDisplayLevel(0U), m_logFileLevel(0U), m_logFilePath(), m_logFileRoot(), +m_logFileRotate(true), m_networkPort(0U), m_networkDebug(false), m_icomEnabled(false), @@ -143,6 +144,8 @@ bool CConf::read() m_logFileLevel = (unsigned int)::atoi(value); else if (::strcmp(key, "DisplayLevel") == 0) m_logDisplayLevel = (unsigned int)::atoi(value); + else if (::strcmp(key, "FileRotate") == 0) + m_logFileRotate = ::atoi(value) == 1; } else if (section == SECTION_NETWORK) { if (::strcmp(key, "Port") == 0) m_networkPort = (unsigned int)::atoi(value); @@ -218,6 +221,11 @@ std::string CConf::getLogFileRoot() const return m_logFileRoot; } +bool CConf::getLogFileRotate() const +{ + return m_logFileRotate; +} + unsigned int CConf::getNetworkPort() const { return m_networkPort; diff --git a/NXDNReflector/Conf.h b/NXDNReflector/Conf.h index ed32c16..a89b28b 100644 --- a/NXDNReflector/Conf.h +++ b/NXDNReflector/Conf.h @@ -43,6 +43,7 @@ public: unsigned int getLogFileLevel() const; std::string getLogFilePath() const; std::string getLogFileRoot() const; + bool getLogFileRotate() const; // The Network section unsigned int getNetworkPort() const; @@ -74,6 +75,7 @@ private: unsigned int m_logFileLevel; std::string m_logFilePath; std::string m_logFileRoot; + bool m_logFileRotate; unsigned int m_networkPort; bool m_networkDebug; diff --git a/NXDNReflector/Log.cpp b/NXDNReflector/Log.cpp index 1d5ad29..5d80ca4 100644 --- a/NXDNReflector/Log.cpp +++ b/NXDNReflector/Log.cpp @@ -35,6 +35,7 @@ static unsigned int m_fileLevel = 2U; static std::string m_filePath; static std::string m_fileRoot; +static bool m_fileRotate = true; static FILE* m_fpLog = NULL; static bool m_daemon = false; @@ -45,7 +46,7 @@ static struct tm m_tm; static char LEVELS[] = " DMIWEF"; -static bool LogOpen() +static bool logOpenRotate() { bool status = false; @@ -86,13 +87,51 @@ static bool LogOpen() return status; } -bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel) +static bool logOpenNoRotate() +{ + bool status = false; + + if (m_fileLevel == 0U) + return true; + + if (m_fpLog != NULL) + return true; + + char filename[200U]; +#if defined(_WIN32) || defined(_WIN64) + ::sprintf(filename, "%s\\%s.log", m_filePath.c_str(), m_fileRoot.c_str()); +#else + ::sprintf(filename, "%s/%s.log", m_filePath.c_str(), m_fileRoot.c_str()); +#endif + + if ((m_fpLog = ::fopen(filename, "a+t")) != NULL) { + status = true; + +#if !defined(_WIN32) && !defined(_WIN64) + if (m_daemon) + dup2(fileno(m_fpLog), fileno(stderr)); +#endif + } + + return status; +} + +bool LogOpen() +{ + if (m_fileRotate) + return logOpenRotate(); + else + return logOpenNoRotate(); +} + +bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, bool rotate) { m_filePath = filePath; m_fileRoot = fileRoot; m_fileLevel = fileLevel; m_displayLevel = displayLevel; m_daemon = daemon; + m_fileRotate = rotate; if (m_daemon) m_displayLevel = 0U; diff --git a/NXDNReflector/Log.h b/NXDNReflector/Log.h index 0d00653..ae95b60 100644 --- a/NXDNReflector/Log.h +++ b/NXDNReflector/Log.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2020 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 @@ -30,7 +30,7 @@ extern void Log(unsigned int level, const char* fmt, ...); -extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel); +extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, bool rotate); extern void LogFinalise(); #endif diff --git a/NXDNReflector/NXDNReflector.cpp b/NXDNReflector/NXDNReflector.cpp index 502b20b..2d4f2a3 100644 --- a/NXDNReflector/NXDNReflector.cpp +++ b/NXDNReflector/NXDNReflector.cpp @@ -155,9 +155,9 @@ void CNXDNReflector::run() #endif #if !defined(_WIN32) && !defined(_WIN64) - ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); + ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate()); #else - ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); + ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate()); #endif if (!ret) { ::fprintf(stderr, "NXDNReflector: unable to open the log file\n"); diff --git a/NXDNReflector/NXDNReflector.ini b/NXDNReflector/NXDNReflector.ini index ca89b1d..475a6f5 100644 --- a/NXDNReflector/NXDNReflector.ini +++ b/NXDNReflector/NXDNReflector.ini @@ -12,6 +12,7 @@ DisplayLevel=1 FileLevel=1 FilePath=. FileRoot=NXDNReflector +FileRotate=1 [Network] Port=41400 diff --git a/NXDNReflector/UDPSocket.cpp b/NXDNReflector/UDPSocket.cpp index 3f6192a..510c8a2 100644 --- a/NXDNReflector/UDPSocket.cpp +++ b/NXDNReflector/UDPSocket.cpp @@ -289,6 +289,12 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag LogError("Error returned from recvfrom, err: %lu", ::GetLastError()); #else LogError("Error returned from recvfrom, err: %d", errno); + + if (len == -1 && errno == ENOTSOCK) { + LogMessage("Re-opening UDP port on %u", m_port); + close(); + open(); + } #endif return -1; } diff --git a/NXDNReflector/Version.h b/NXDNReflector/Version.h index f0a8fe6..fb0377d 100644 --- a/NXDNReflector/Version.h +++ b/NXDNReflector/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20200920"; +const char* VERSION = "20201101"; #endif