diff --git a/P25Gateway/Conf.cpp b/P25Gateway/Conf.cpp index 8620a68..063ce13 100644 --- a/P25Gateway/Conf.cpp +++ b/P25Gateway/Conf.cpp @@ -53,6 +53,7 @@ m_logDisplayLevel(0U), m_logFileLevel(0U), m_logFilePath(), m_logFileRoot(), +m_logFileRotate(true), m_networkPort(0U), m_networkHosts1(), m_networkHosts2(), @@ -168,6 +169,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); @@ -286,6 +289,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/P25Gateway/Conf.h b/P25Gateway/Conf.h index f7e573e..23c2624 100644 --- a/P25Gateway/Conf.h +++ b/P25Gateway/Conf.h @@ -52,6 +52,7 @@ public: unsigned int getLogFileLevel() const; std::string getLogFilePath() const; std::string getLogFileRoot() const; + bool getLogFileRotate() const; // The Network section unsigned int getNetworkPort() const; @@ -91,6 +92,7 @@ private: unsigned int m_logFileLevel; std::string m_logFilePath; std::string m_logFileRoot; + bool m_logFileRotate; unsigned int m_networkPort; std::string m_networkHosts1; diff --git a/P25Gateway/Log.cpp b/P25Gateway/Log.cpp index 1d5ad29..5d80ca4 100644 --- a/P25Gateway/Log.cpp +++ b/P25Gateway/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/P25Gateway/Log.h b/P25Gateway/Log.h index 0d00653..ae95b60 100644 --- a/P25Gateway/Log.h +++ b/P25Gateway/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/P25Gateway/P25Gateway.cpp b/P25Gateway/P25Gateway.cpp index 3feec94..f08d61c 100644 --- a/P25Gateway/P25Gateway.cpp +++ b/P25Gateway/P25Gateway.cpp @@ -164,9 +164,9 @@ void CP25Gateway::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, "P25Gateway: unable to open the log file\n"); diff --git a/P25Gateway/P25Gateway.ini b/P25Gateway/P25Gateway.ini index ff465ca..16ab7ba 100644 --- a/P25Gateway/P25Gateway.ini +++ b/P25Gateway/P25Gateway.ini @@ -21,6 +21,7 @@ DisplayLevel=1 FileLevel=1 FilePath=. FileRoot=P25Gateway +FileRotate=1 [Network] Port=42010 diff --git a/P25Gateway/UDPSocket.cpp b/P25Gateway/UDPSocket.cpp index 3f6192a..510c8a2 100644 --- a/P25Gateway/UDPSocket.cpp +++ b/P25Gateway/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/P25Gateway/Version.h b/P25Gateway/Version.h index ff3fa74..8d5b383 100644 --- a/P25Gateway/Version.h +++ b/P25Gateway/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/P25Parrot/UDPSocket.cpp b/P25Parrot/UDPSocket.cpp index 3f6192a..510c8a2 100644 --- a/P25Parrot/UDPSocket.cpp +++ b/P25Parrot/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/P25Parrot/Version.h b/P25Parrot/Version.h index a718af3..e9186a6 100644 --- a/P25Parrot/Version.h +++ b/P25Parrot/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/P25Reflector/Conf.cpp b/P25Reflector/Conf.cpp index ef08973..aed8e17 100644 --- a/P25Reflector/Conf.cpp +++ b/P25Reflector/Conf.cpp @@ -43,6 +43,7 @@ m_logDisplayLevel(0U), m_logFileLevel(0U), m_logFilePath(), m_logFileRoot(), +m_logFileRotate(true), m_networkPort(0U), m_networkDebug(false) { @@ -124,6 +125,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); @@ -172,6 +175,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/P25Reflector/Conf.h b/P25Reflector/Conf.h index 0d93da5..9bd012e 100644 --- a/P25Reflector/Conf.h +++ b/P25Reflector/Conf.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 @@ -42,6 +42,7 @@ public: unsigned int getLogFileLevel() const; std::string getLogFilePath() const; std::string getLogFileRoot() const; + bool getLogFileRotate() const; // The Network section unsigned int getNetworkPort() const; @@ -58,6 +59,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/P25Reflector/Log.cpp b/P25Reflector/Log.cpp index 1d5ad29..5d80ca4 100644 --- a/P25Reflector/Log.cpp +++ b/P25Reflector/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/P25Reflector/Log.h b/P25Reflector/Log.h index 0d00653..ae95b60 100644 --- a/P25Reflector/Log.h +++ b/P25Reflector/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/P25Reflector/P25Reflector.cpp b/P25Reflector/P25Reflector.cpp index f65073d..0acfc14 100644 --- a/P25Reflector/P25Reflector.cpp +++ b/P25Reflector/P25Reflector.cpp @@ -154,9 +154,9 @@ void CP25Reflector::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, "P25Gateway: unable to open the log file\n"); diff --git a/P25Reflector/P25Reflector.ini b/P25Reflector/P25Reflector.ini index 6856584..2a5f541 100644 --- a/P25Reflector/P25Reflector.ini +++ b/P25Reflector/P25Reflector.ini @@ -11,6 +11,7 @@ DisplayLevel=1 FileLevel=1 FilePath=. FileRoot=P25Reflector +FileRotate=1 [Network] Port=41000 diff --git a/P25Reflector/UDPSocket.cpp b/P25Reflector/UDPSocket.cpp index 3f6192a..510c8a2 100644 --- a/P25Reflector/UDPSocket.cpp +++ b/P25Reflector/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/P25Reflector/Version.h b/P25Reflector/Version.h index e72ac9d..9e751e8 100644 --- a/P25Reflector/Version.h +++ b/P25Reflector/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20200920"; +const char* VERSION = "20201101"; #endif