From 4bd5ec4a11bfdeaa22e8bc1148eb196272b5bf05 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Thu, 30 Dec 2021 10:02:11 +0100 Subject: [PATCH] Add more log configuration #4 --- DStarGatewayApp.cpp | 7 +++- DStarGatewayConfig.cpp | 31 ++++++++++++++-- DStarGatewayConfig.h | 5 +++ Log.cpp | 3 +- Log.h | 83 ++++++++++++++++++++++-------------------- LogSeverity.h | 29 +++++++++++++++ LogTarget.h | 9 +---- example.cfg | 4 ++ 8 files changed, 117 insertions(+), 54 deletions(-) create mode 100644 LogSeverity.h diff --git a/DStarGatewayApp.cpp b/DStarGatewayApp.cpp index ef37f43..33c6239 100644 --- a/DStarGatewayApp.cpp +++ b/DStarGatewayApp.cpp @@ -86,6 +86,7 @@ void CDStarGatewayApp::run() m_thread->Run(); m_thread->Wait(); CLog::logInfo("exiting\n"); + CLog::finalise(); } bool CDStarGatewayApp::createThread() @@ -100,10 +101,12 @@ bool CDStarGatewayApp::createThread() return false; } + // Setup Log TLog log; config.getLog(log); - CLog::addTarget(new CLogConsoleTarget(LS_INFO)); - CLog::addTarget(new CLogFileTarget(LS_INFO, log.logDir, true)); + CLog::finalise(); + if(log.m_displayLevel != LOG_NONE) CLog::addTarget(new CLogConsoleTarget(log.m_displayLevel)); + if(log.m_fileLevel != LOG_NONE) CLog::addTarget(new CLogFileTarget(log.m_fileLevel, log.logDir, log.m_fileRotate)); Tpaths paths; config.getPaths(paths); diff --git a/DStarGatewayConfig.cpp b/DStarGatewayConfig.cpp index 6629bd7..d2ad9af 100644 --- a/DStarGatewayConfig.cpp +++ b/DStarGatewayConfig.cpp @@ -126,13 +126,38 @@ bool CDStarGatewayConfig::loadAPRS(const CConfig & cfg) bool CDStarGatewayConfig::loadLog(const CConfig & cfg) { - bool ret =cfg.getValue("log", "path", m_log.logDir, 0, 2048, "/var/log/dstargateway/"); - + bool ret = cfg.getValue("log", "path", m_log.logDir, 0, 2048, "/var/log/dstargateway/"); if(ret && m_log.logDir[m_log.logDir.length() - 1] != '/') { m_log.logDir.push_back('/'); } - //TODO 20211226 check if directory are accessible + ret = cfg.getValue("log", "fileRoot", m_log.m_fileRoot, 0, 64, "dstargateway") && ret; + ret = cfg.getValue("log", "fileRotate", m_log.m_fileRotate, true) && ret; + + std::string levelStr; + ret = cfg.getValue("log", "fileLevel", levelStr, "info", {"trace", "debug", "info", "warning", "error", "fatal", "none"}) && ret; + if(ret) { + if(levelStr == "trace") m_log.m_fileLevel = LOG_TRACE; + else if(levelStr == "debug") m_log.m_fileLevel = LOG_DEBUG; + else if(levelStr == "info") m_log.m_fileLevel = LOG_INFO; + else if(levelStr == "warning") m_log.m_fileLevel = LOG_WARNING; + else if(levelStr == "error") m_log.m_fileLevel = LOG_ERROR; + else if(levelStr == "fatal") m_log.m_fileLevel = LOG_FATAL; + else if(levelStr == "none") m_log.m_fileLevel = LOG_NONE; + } + + ret = cfg.getValue("log", "displayLevel", levelStr, "info", {"trace", "debug", "info", "warning", "error", "fatal", "none"}) && ret; + if(ret) { + if(levelStr == "trace") m_log.m_displayLevel = LOG_TRACE; + else if(levelStr == "debug") m_log.m_displayLevel = LOG_DEBUG; + else if(levelStr == "info") m_log.m_displayLevel = LOG_INFO; + else if(levelStr == "warning") m_log.m_displayLevel = LOG_WARNING; + else if(levelStr == "error") m_log.m_displayLevel = LOG_ERROR; + else if(levelStr == "fatal") m_log.m_displayLevel = LOG_FATAL; + else if(levelStr == "none") m_log.m_displayLevel = LOG_NONE; + } + + //TODO 20211226 check if directories are accessible return ret; } diff --git a/DStarGatewayConfig.h b/DStarGatewayConfig.h index 56197bf..a406dc0 100644 --- a/DStarGatewayConfig.h +++ b/DStarGatewayConfig.h @@ -23,6 +23,7 @@ #include "Defs.h" #include "Config.h" +#include "LogSeverity.h" typedef struct { GATEWAY_TYPE type; @@ -79,6 +80,10 @@ typedef struct { typedef struct { std::string logDir; + LOG_SEVERITY m_displayLevel; + LOG_SEVERITY m_fileLevel; + std::string m_fileRoot; + bool m_fileRotate; } TLog; typedef struct { diff --git a/Log.cpp b/Log.cpp index c89ac81..689e69f 100644 --- a/Log.cpp +++ b/Log.cpp @@ -24,10 +24,9 @@ #include "Log.h" #include "LogConsoleTarget.h" -LOG_SEVERITY CLog::m_level(LS_DEBUG); bool CLog::m_addedTargets(false); std::recursive_mutex CLog::m_targetsMutex; -std::vector CLog::m_targets = { new CLogConsoleTarget(LS_DEBUG) }; +std::vector CLog::m_targets = { new CLogConsoleTarget(LOG_DEBUG) }; void CLog::addTarget(CLogTarget* target) { diff --git a/Log.h b/Log.h index c74eb1f..26414c8 100644 --- a/Log.h +++ b/Log.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "StringUtils.h" #include "LogTarget.h" @@ -31,7 +32,6 @@ class CLog { private: - static LOG_SEVERITY m_level; static std::vector m_targets; static bool m_addedTargets; static std::recursive_mutex m_targetsMutex; @@ -40,39 +40,41 @@ private: template static void formatLogMessage(std::string& output, LOG_SEVERITY severity, const std::string & f, Args... args) { - std::string severityStr; - switch (severity) - { - case LS_DEBUG: - severityStr = "DEBUG "; - break; - case LS_ERROR: - severityStr = "ERROR "; - break; - case LS_FATAL: - severityStr = "FATAL "; - break; - case LS_INFO : - severityStr = "INFO "; - break; - case LS_WARNING: - severityStr = "WARNING"; - break; - case LS_TRACE: - severityStr = "TRACE "; - break; - default: - break; - } + assert(severity != LOG_NONE); + + std::string severityStr; + switch (severity) + { + case LOG_DEBUG: + severityStr = "DEBUG "; + break; + case LOG_ERROR: + severityStr = "ERROR "; + break; + case LOG_FATAL: + severityStr = "FATAL "; + break; + case LOG_INFO : + severityStr = "INFO "; + break; + case LOG_WARNING: + severityStr = "WARNING"; + break; + case LOG_TRACE: + severityStr = "TRACE "; + break; + default: + break; + } - std::string message = CStringUtils::string_format(f, args...); - boost::trim(message); - std::string timeUtc; - getTimeStamp(timeUtc); - std::stringstream s; - s << "[" << timeUtc << "] [" << severityStr << "] " << message << std::endl; + std::string message = CStringUtils::string_format(f, args...); + boost::trim(message); + std::string timeUtc; + getTimeStamp(timeUtc); + std::stringstream s; + s << "[" << timeUtc << "] [" << severityStr << "] " << message << std::endl; - output = s.str(); + output = s.str(); } public: @@ -82,27 +84,27 @@ public: template static void logDebug(const std::string & f, Args... args) { - log(LS_DEBUG, f, args...); + log(LOG_DEBUG, f, args...); } template static void logInfo(const std::string & f, Args... args) { - log(LS_INFO, f, args...); + log(LOG_INFO, f, args...); } template static void logWarning(const std::string & f, Args... args) { - log(LS_WARNING, f, args...); + log(LOG_WARNING, f, args...); } template static void logError(const std::string & f, Args... args) { - log(LS_ERROR, f, args...); + log(LOG_ERROR, f, args...); } template static void logFatal(const std::string & f, Args... args) { - log(LS_FATAL, f, args...); + log(LOG_FATAL, f, args...); } template static void log(LOG_SEVERITY severity, const std::string & f, Args... args) @@ -111,8 +113,11 @@ public: std::string msg; for(auto target : m_targets) { - if(target->getLevel() >= CLog::m_level) { - if(msg.empty()) formatLogMessage(msg, severity, f, args...); + if(severity >= target->getLevel()) { + + if(msg.empty()) + formatLogMessage(msg, severity, f, args...); + target->printLog(msg); } } diff --git a/LogSeverity.h b/LogSeverity.h new file mode 100644 index 0000000..fe1144f --- /dev/null +++ b/LogSeverity.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#pragma once + +enum LOG_SEVERITY : unsigned int { + LOG_TRACE = 1, + LOG_DEBUG, + LOG_INFO, + LOG_WARNING, + LOG_ERROR, + LOG_FATAL, + LOG_NONE = 0xFFFFFFFF +}; \ No newline at end of file diff --git a/LogTarget.h b/LogTarget.h index 427c707..1f372e1 100644 --- a/LogTarget.h +++ b/LogTarget.h @@ -20,14 +20,7 @@ #include -enum LOG_SEVERITY { - LS_TRACE = 1, - LS_DEBUG, - LS_INFO, - LS_WARNING, - LS_ERROR, - LS_FATAL -}; +#include "LogSeverity.h" class CLogTarget { diff --git a/example.cfg b/example.cfg index d84358c..156def6 100644 --- a/example.cfg +++ b/example.cfg @@ -142,6 +142,10 @@ password=12345 [Log] path=/var/log/dstargateway/ +fileRoot= # defaults to dstarGateway +fileRotate= # rotate log files daily, defaults to true +fileLevel= # defaults to info, valid values are trace, debug, info, warning, error, fatal, none +displayLevel= # defaults to info, valid values are trace, debug, info, warning, error, fatal, none [Paths] data=/usr/local/share/dstargateway.d/ #Path where the data (hostfiles, audio files etc) can be found