From 023b5d48e0455b236974c518120aeea5ed05c149 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Sat, 31 Oct 2020 22:45:54 +0000 Subject: [PATCH] Add optional log file rotation and UDP error handling. --- Conf.cpp | 8 ++++++++ Conf.h | 2 ++ DMRGateway.cpp | 4 ++-- DMRGateway.ini | 1 + Log.cpp | 43 +++++++++++++++++++++++++++++++++++++++++-- Log.h | 4 ++-- UDPSocket.cpp | 6 ++++++ Version.h | 2 +- 8 files changed, 63 insertions(+), 7 deletions(-) diff --git a/Conf.cpp b/Conf.cpp index f8ece94..c29ae34 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -61,6 +61,7 @@ m_logDisplayLevel(0U), m_logFileLevel(0U), m_logFilePath(), m_logFileRoot(), +m_logFileRotate(true), m_infoLatitude(0.0F), m_infoLongitude(0.0F), m_infoHeight(0), @@ -292,6 +293,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_VOICE) { if (::strcmp(key, "Enabled") == 0) m_voiceEnabled = ::atoi(value) == 1; @@ -1047,6 +1050,11 @@ std::string CConf::getLogFileRoot() const return m_logFileRoot; } +bool CConf::getLogFileRotate() const +{ + return m_logFileRotate; +} + bool CConf::getVoiceEnabled() const { return m_voiceEnabled; diff --git a/Conf.h b/Conf.h index 18c3a7a..85e38a6 100644 --- a/Conf.h +++ b/Conf.h @@ -93,6 +93,7 @@ public: unsigned int getLogFileLevel() const; std::string getLogFilePath() const; std::string getLogFileRoot() const; + bool getLogFileRotate() const; // The Voice section bool getVoiceEnabled() const; @@ -260,6 +261,7 @@ private: unsigned int m_logFileLevel; std::string m_logFilePath; std::string m_logFileRoot; + bool m_logFileRotate; float m_infoLatitude; float m_infoLongitude; diff --git a/DMRGateway.cpp b/DMRGateway.cpp index 7a8a773..6f183a7 100644 --- a/DMRGateway.cpp +++ b/DMRGateway.cpp @@ -337,9 +337,9 @@ int CDMRGateway::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, "DMRGateway: unable to open the log file\n"); diff --git a/DMRGateway.ini b/DMRGateway.ini index 3aa42b1..a4a9cbf 100644 --- a/DMRGateway.ini +++ b/DMRGateway.ini @@ -16,6 +16,7 @@ DisplayLevel=1 FileLevel=1 FilePath=. FileRoot=DMRGateway +FileRotate=1 [Voice] Enabled=1 diff --git a/Log.cpp b/Log.cpp index 1d5ad29..5d80ca4 100644 --- a/Log.cpp +++ b/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/Log.h b/Log.h index 0d00653..ae95b60 100644 --- a/Log.h +++ b/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/UDPSocket.cpp b/UDPSocket.cpp index 3f6192a..510c8a2 100644 --- a/UDPSocket.cpp +++ b/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/Version.h b/Version.h index 3e08686..b85724b 100644 --- a/Version.h +++ b/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20200920"; +const char* VERSION = "20201031"; #endif