From 8559f97efb22276a60100902258a2bfa7ee581ef Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Mon, 7 Mar 2016 18:42:05 +0000 Subject: [PATCH] Modify the logging system greatly. --- Conf.cpp | 40 ++++++++++++++-------------- Conf.h | 16 ++++++------ Log.cpp | 72 +++++++++++++++++++++++---------------------------- Log.h | 6 ++--- MMDVM.ini | 8 +++--- MMDVMHost.cpp | 4 +-- 6 files changed, 67 insertions(+), 79 deletions(-) diff --git a/Conf.cpp b/Conf.cpp index ab61be2..af56965 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -56,10 +56,10 @@ m_height(0), m_location(), m_description(), m_url(), -m_logLevel(0U), -m_logPath(), -m_logRoot(), -m_logDisplay(true), +m_logDisplayLevel(0U), +m_logFileLevel(0U), +m_logFilePath(), +m_logFileRoot(), m_modemPort(), m_modemRXInvert(false), m_modemTXInvert(false), @@ -181,14 +181,14 @@ bool CConf::read() else if (::strcmp(key, "URL") == 0) m_url = value; } else if (section == SECTION_LOG) { - if (::strcmp(key, "Path") == 0) - m_logPath = value; - else if (::strcmp(key, "Root") == 0) - m_logRoot = value; - else if (::strcmp(key, "Level") == 0) - m_logLevel = (unsigned int)::atoi(value); - else if (::strcmp(key, "Display") == 0) - m_logDisplay = ::atoi(value) == 1; + if (::strcmp(key, "FilePath") == 0) + m_logFilePath = value; + else if (::strcmp(key, "FileRoot") == 0) + m_logFileRoot = 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 (section == SECTION_MODEM) { if (::strcmp(key, "Port") == 0) m_modemPort = value; @@ -343,24 +343,24 @@ std::string CConf::getURL() const return m_url; } -unsigned int CConf::getLogLevel() const +unsigned int CConf::getLogDisplayLevel() const { - return m_logLevel; + return m_logDisplayLevel; } -std::string CConf::getLogPath() const +unsigned int CConf::getLogFileLevel() const { - return m_logPath; + return m_logFileLevel; } -std::string CConf::getLogRoot() const +std::string CConf::getLogFilePath() const { - return m_logRoot; + return m_logFilePath; } -bool CConf::getLogDisplay() const +std::string CConf::getLogFileRoot() const { - return m_logDisplay; + return m_logFileRoot; } std::string CConf::getModemPort() const diff --git a/Conf.h b/Conf.h index 39c7684..cd2e188 100644 --- a/Conf.h +++ b/Conf.h @@ -48,10 +48,10 @@ public: std::string getURL() const; // The Log section - std::string getLogPath() const; - std::string getLogRoot() const; - unsigned int getLogLevel() const; - bool getLogDisplay() const; + unsigned int getLogDisplayLevel() const; + unsigned int getLogFileLevel() const; + std::string getLogFilePath() const; + std::string getLogFileRoot() const; // The Modem section std::string getModemPort() const; @@ -121,10 +121,10 @@ private: std::string m_description; std::string m_url; - unsigned int m_logLevel; - std::string m_logPath; - std::string m_logRoot; - bool m_logDisplay; + unsigned int m_logDisplayLevel; + unsigned int m_logFileLevel; + std::string m_logFilePath; + std::string m_logFileRoot; std::string m_modemPort; bool m_modemRXInvert; diff --git a/Log.cpp b/Log.cpp index fbb9082..5f824aa 100644 --- a/Log.cpp +++ b/Log.cpp @@ -30,14 +30,13 @@ #include #include -static std::string m_path; -static std::string m_root; +static unsigned int m_fileLevel = 2U; +static std::string m_filePath; +static std::string m_fileRoot; static FILE* m_fpLog = NULL; -static bool m_display = true; - -static unsigned int m_level = 2U; +static unsigned int m_displayLevel = 2U; static struct tm m_tm; @@ -45,6 +44,9 @@ static char LEVELS[] = " DMIWEF"; static bool LogOpen() { + if (m_fileLevel == 0U) + return true; + time_t now; ::time(&now); @@ -60,9 +62,9 @@ static bool LogOpen() char filename[50U]; #if defined(_WIN32) || defined(_WIN64) - ::sprintf(filename, "%s\\%s-%04d-%02d-%02d.log", m_path.c_str(), m_root.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); + ::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_path.c_str(), m_root.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); + ::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 m_fpLog = ::fopen(filename, "a+t"); @@ -71,11 +73,12 @@ static bool LogOpen() return m_fpLog != NULL; } -bool LogInitialise(const std::string& path, const std::string& root, bool display) +bool LogInitialise(const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel) { - m_path = path; - m_root = root; - m_display = display; + m_filePath = filePath; + m_fileRoot = fileRoot; + m_fileLevel = fileLevel; + m_displayLevel = displayLevel; return ::LogOpen(); } @@ -85,58 +88,47 @@ void LogFinalise() ::fclose(m_fpLog); } -void LogSetLevel(unsigned int level) -{ - m_level = level; -} - void Log(unsigned int level, const char* fmt, ...) { - assert(level < 7U); assert(fmt != NULL); - if (level < m_level) - return; - - bool ret = ::LogOpen(); - if (!ret) - return; - + char buffer[300U]; #if defined(_WIN32) || defined(_WIN64) SYSTEMTIME st; ::GetSystemTime(&st); - ::fprintf(m_fpLog, "%c: %04u-%02u-%02u %02u:%02u:%02u.%03u ", LEVELS[level], st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); - if (m_display) - ::fprintf(stdout, "%c: %04u-%02u-%02u %02u:%02u:%02u.%03u ", LEVELS[level], st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); + ::sprintf(buffer, "%c: %04u-%02u-%02u %02u:%02u:%02u.%03u ", LEVELS[level], st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); #else struct timeval now; ::gettimeofday(&now, NULL); - struct tm* tm = ::gmtime(&now.tv_sec); + struct tm* tm = ::gmtime(&now.tv_sec); - ::fprintf(m_fpLog, "%c: %04d-%02d-%02d %02d:%02d:%02d.%03lu ", LEVELS[level], tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000U); - if (m_display) - ::fprintf(stdout, "%c: %04d-%02d-%02d %02d:%02d:%02d.%03lu ", LEVELS[level], tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000U); + ::sprintf(buffer, "%c: %04d-%02d-%02d %02d:%02d:%02d.%03lu ", LEVELS[level], tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000U); #endif - va_list vl; - va_start(vl, fmt); + va_list vl; + va_start(vl, fmt); - char buffer[200U]; - ::vsprintf(buffer, fmt, vl); + ::vsprintf(buffer + ::strlen(buffer), fmt, vl); va_end(vl); - ::fprintf(m_fpLog, "%s\n", buffer); - ::fflush(m_fpLog); + if (level >= m_fileLevel && m_fileLevel != 0U) { + bool ret = ::LogOpen(); + if (!ret) + return; - if (m_display) { - ::fprintf(stdout, "%s\n", buffer); + ::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 + if (level == 6U) { // Fatal ::fclose(m_fpLog); exit(1); } diff --git a/Log.h b/Log.h index a1dc869..d671ef9 100644 --- a/Log.h +++ b/Log.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016 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,9 +30,7 @@ extern void Log(unsigned int level, const char* fmt, ...); -extern bool LogInitialise(const std::string& path, const std::string& root, bool display); +extern bool LogInitialise(const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel); extern void LogFinalise(); -extern void LogSetLevel(unsigned int level); - #endif diff --git a/MMDVM.ini b/MMDVM.ini index 4d100f4..fbc5150 100644 --- a/MMDVM.ini +++ b/MMDVM.ini @@ -18,10 +18,10 @@ URL=www.google.co.uk [Log] # Logging levels, 0=No logging -Level=1 -Path=. -Root=MMDVM -Display=1 +DisplayLevel=1 +FileLevel=1 +FilePath=. +FileRoot=MMDVM [Modem] # Port=/dev/ttyACM0 diff --git a/MMDVMHost.cpp b/MMDVMHost.cpp index 1acb5c4..209de28 100644 --- a/MMDVMHost.cpp +++ b/MMDVMHost.cpp @@ -95,14 +95,12 @@ int CMMDVMHost::run() return 1; } - ret = ::LogInitialise(m_conf.getLogPath(), m_conf.getLogRoot(), m_conf.getLogDisplay()); + ret = ::LogInitialise(m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); if (!ret) { ::fprintf(stderr, "MMDVMHost: unable to open the log file\n"); return 1; } - ::LogSetLevel(m_conf.getLogLevel()); - LogInfo(HEADER1); LogInfo(HEADER2); LogInfo(HEADER3);