diff --git a/Conf.cpp b/Conf.cpp index f6c7678..d021f5b 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -78,10 +78,6 @@ m_description(), m_url(), m_logMQTTLevel(0U), m_logDisplayLevel(0U), -m_logFileLevel(0U), -m_logFilePath(), -m_logFileRoot(), -m_logFileRotate(true), m_mqttHost("127.0.0.1"), m_mqttPort(1883), m_mqttKeepalive(60U), @@ -457,18 +453,10 @@ bool CConf::read() else if (::strcmp(key, "URL") == 0) m_url = value; } else if (section == SECTION_LOG) { - if (::strcmp(key, "FilePath") == 0) - m_logFilePath = value; - else if (::strcmp(key, "FileRoot") == 0) - m_logFileRoot = value; - else if (::strcmp(key, "MQTTLevel") == 0) + if (::strcmp(key, "MQTTLevel") == 0) m_logMQTTLevel = (unsigned int)::atoi(value); - else if (::strcmp(key, "FileLevel") == 0) - 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_MQTT) { if (::strcmp(key, "Host") == 0) m_mqttHost = value; @@ -1117,26 +1105,6 @@ unsigned int CConf::getLogDisplayLevel() const return m_logDisplayLevel; } -unsigned int CConf::getLogFileLevel() const -{ - return m_logFileLevel; -} - -std::string CConf::getLogFilePath() const -{ - return m_logFilePath; -} - -std::string CConf::getLogFileRoot() const -{ - return m_logFileRoot; -} - -bool CConf::getLogFileRotate() const -{ - return m_logFileRotate; -} - std::string CConf::getMQTTHost() const { return m_mqttHost; diff --git a/Conf.h b/Conf.h index 4be509b..818e007 100644 --- a/Conf.h +++ b/Conf.h @@ -51,10 +51,6 @@ public: // The Log section unsigned int getLogMQTTLevel() const; unsigned int getLogDisplayLevel() const; - unsigned int getLogFileLevel() const; - std::string getLogFilePath() const; - std::string getLogFileRoot() const; - bool getLogFileRotate() const; // The MQTT section std::string getMQTTHost() const; @@ -350,10 +346,6 @@ private: unsigned int m_logMQTTLevel; unsigned int m_logDisplayLevel; - unsigned int m_logFileLevel; - std::string m_logFilePath; - std::string m_logFileRoot; - bool m_logFileRotate; std::string m_mqttHost; unsigned short m_mqttPort; diff --git a/Log.cpp b/Log.cpp index b6a5d47..5643d85 100644 --- a/Log.cpp +++ b/Log.cpp @@ -37,119 +37,18 @@ CMQTTConnection* m_mqtt = NULL; static unsigned int m_mqttLevel = 2U; -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; - static unsigned int m_displayLevel = 2U; -static struct tm m_tm; - static char LEVELS[] = " DMIWEF"; -static bool logOpenRotate() +void LogInitialise(unsigned int displayLevel, unsigned int mqttLevel) { - bool status = false; - - if (m_fileLevel == 0U) - return true; - - time_t now; - ::time(&now); - - struct tm* tm = ::gmtime(&now); - - if (tm->tm_mday == m_tm.tm_mday && tm->tm_mon == m_tm.tm_mon && tm->tm_year == m_tm.tm_year) { - if (m_fpLog != NULL) - return true; - } else { - if (m_fpLog != NULL) - ::fclose(m_fpLog); - } - - char filename[200U]; -#if defined(_WIN32) || defined(_WIN64) - ::sprintf(filename, "%s\\%s-%04d-%02d-%02d.log", m_filePath.c_str(), m_fileRoot.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); -#else - ::sprintf(filename, "%s/%s-%04d-%02d-%02d.log", m_filePath.c_str(), m_fileRoot.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); -#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 - } - - m_tm = *tm; - - return status; -} - -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, unsigned int mqttLevel, bool rotate) -{ - m_filePath = filePath; - m_fileRoot = fileRoot; m_mqttLevel = mqttLevel; - m_fileLevel = fileLevel; m_displayLevel = displayLevel; - m_daemon = daemon; - m_fileRotate = rotate; - - if (m_daemon) - m_displayLevel = 0U; - - return ::LogOpen(); } void LogFinalise() { - if (m_fpLog != NULL) - ::fclose(m_fpLog); - if (m_mqtt != NULL) { m_mqtt->close(); delete m_mqtt; @@ -186,24 +85,13 @@ void Log(unsigned int level, const char* fmt, ...) if (m_mqtt != NULL && level >= m_mqttLevel && m_mqttLevel != 0U) m_mqtt->publish("log", buffer); - if (level >= m_fileLevel && m_fileLevel != 0U) { - bool ret = ::LogOpen(); - if (!ret) - return; - - ::fprintf(m_fpLog, "%s\n", buffer); - ::fflush(m_fpLog); - } - if (level >= m_displayLevel && m_displayLevel != 0U) { ::fprintf(stdout, "%s\n", buffer); ::fflush(stdout); } - if (level == 6U) { // Fatal - ::fclose(m_fpLog); + if (level == 6U) // Fatal exit(1); - } } void WriteJSON(const std::string& topLevel, nlohmann::json& json) diff --git a/Log.h b/Log.h index a3fb7f6..7392164 100644 --- a/Log.h +++ b/Log.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016,2020,2022 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2020,2022,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 @@ -32,7 +32,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, unsigned int mqttLevel, bool rotate); +extern void LogInitialise(unsigned int displayLevel, unsigned int mqttLevel); extern void LogFinalise(); extern void WriteJSON(const std::string& topLevel, nlohmann::json& json); diff --git a/MMDVM.ini b/MMDVM.ini index 4bb07f5..8bc9d22 100644 --- a/MMDVM.ini +++ b/MMDVM.ini @@ -24,10 +24,6 @@ URL=www.google.co.uk # Logging levels, 0=No logging MQTTLevel=1 DisplayLevel=1 -FileLevel=1 -FilePath=. -FileRoot=MMDVM -FileRotate=1 [MQTT] Host=127.0.0.1 diff --git a/MMDVMHost.cpp b/MMDVMHost.cpp index 6df625e..9b57272 100644 --- a/MMDVMHost.cpp +++ b/MMDVMHost.cpp @@ -282,15 +282,7 @@ int CMMDVMHost::run() #endif #endif -#if !defined(_WIN32) && !defined(_WIN64) - ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel(), m_conf.getLogFileRotate()); -#else - ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel(), m_conf.getLogFileRotate()); -#endif - if (!ret) { - ::fprintf(stderr, "MMDVMHost: unable to open the log file\n"); - return 1; - } + ::LogInitialise(m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel()); std::vector> subscriptions; subscriptions.push_back(std::make_pair("display", CMMDVMHost::onDisplay));