Modify the logging system greatly.

This commit is contained in:
Jonathan Naylor 2016-03-07 18:42:05 +00:00
parent bfb74d3155
commit 8559f97efb
6 changed files with 67 additions and 79 deletions

View file

@ -56,10 +56,10 @@ m_height(0),
m_location(), m_location(),
m_description(), m_description(),
m_url(), m_url(),
m_logLevel(0U), m_logDisplayLevel(0U),
m_logPath(), m_logFileLevel(0U),
m_logRoot(), m_logFilePath(),
m_logDisplay(true), m_logFileRoot(),
m_modemPort(), m_modemPort(),
m_modemRXInvert(false), m_modemRXInvert(false),
m_modemTXInvert(false), m_modemTXInvert(false),
@ -181,14 +181,14 @@ bool CConf::read()
else if (::strcmp(key, "URL") == 0) else if (::strcmp(key, "URL") == 0)
m_url = value; m_url = value;
} else if (section == SECTION_LOG) { } else if (section == SECTION_LOG) {
if (::strcmp(key, "Path") == 0) if (::strcmp(key, "FilePath") == 0)
m_logPath = value; m_logFilePath = value;
else if (::strcmp(key, "Root") == 0) else if (::strcmp(key, "FileRoot") == 0)
m_logRoot = value; m_logFileRoot = value;
else if (::strcmp(key, "Level") == 0) else if (::strcmp(key, "FileLevel") == 0)
m_logLevel = (unsigned int)::atoi(value); m_logFileLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "Display") == 0) else if (::strcmp(key, "DisplayLevel") == 0)
m_logDisplay = ::atoi(value) == 1; m_logDisplayLevel = (unsigned int)::atoi(value);
} else if (section == SECTION_MODEM) { } else if (section == SECTION_MODEM) {
if (::strcmp(key, "Port") == 0) if (::strcmp(key, "Port") == 0)
m_modemPort = value; m_modemPort = value;
@ -343,24 +343,24 @@ std::string CConf::getURL() const
return m_url; 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 std::string CConf::getModemPort() const

16
Conf.h
View file

@ -48,10 +48,10 @@ public:
std::string getURL() const; std::string getURL() const;
// The Log section // The Log section
std::string getLogPath() const; unsigned int getLogDisplayLevel() const;
std::string getLogRoot() const; unsigned int getLogFileLevel() const;
unsigned int getLogLevel() const; std::string getLogFilePath() const;
bool getLogDisplay() const; std::string getLogFileRoot() const;
// The Modem section // The Modem section
std::string getModemPort() const; std::string getModemPort() const;
@ -121,10 +121,10 @@ private:
std::string m_description; std::string m_description;
std::string m_url; std::string m_url;
unsigned int m_logLevel; unsigned int m_logDisplayLevel;
std::string m_logPath; unsigned int m_logFileLevel;
std::string m_logRoot; std::string m_logFilePath;
bool m_logDisplay; std::string m_logFileRoot;
std::string m_modemPort; std::string m_modemPort;
bool m_modemRXInvert; bool m_modemRXInvert;

72
Log.cpp
View file

@ -30,14 +30,13 @@
#include <ctime> #include <ctime>
#include <cassert> #include <cassert>
static std::string m_path; static unsigned int m_fileLevel = 2U;
static std::string m_root; static std::string m_filePath;
static std::string m_fileRoot;
static FILE* m_fpLog = NULL; static FILE* m_fpLog = NULL;
static bool m_display = true; static unsigned int m_displayLevel = 2U;
static unsigned int m_level = 2U;
static struct tm m_tm; static struct tm m_tm;
@ -45,6 +44,9 @@ static char LEVELS[] = " DMIWEF";
static bool LogOpen() static bool LogOpen()
{ {
if (m_fileLevel == 0U)
return true;
time_t now; time_t now;
::time(&now); ::time(&now);
@ -60,9 +62,9 @@ static bool LogOpen()
char filename[50U]; char filename[50U];
#if defined(_WIN32) || defined(_WIN64) #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 #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 #endif
m_fpLog = ::fopen(filename, "a+t"); m_fpLog = ::fopen(filename, "a+t");
@ -71,11 +73,12 @@ static bool LogOpen()
return m_fpLog != NULL; 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_filePath = filePath;
m_root = root; m_fileRoot = fileRoot;
m_display = display; m_fileLevel = fileLevel;
m_displayLevel = displayLevel;
return ::LogOpen(); return ::LogOpen();
} }
@ -85,58 +88,47 @@ void LogFinalise()
::fclose(m_fpLog); ::fclose(m_fpLog);
} }
void LogSetLevel(unsigned int level)
{
m_level = level;
}
void Log(unsigned int level, const char* fmt, ...) void Log(unsigned int level, const char* fmt, ...)
{ {
assert(level < 7U);
assert(fmt != NULL); assert(fmt != NULL);
if (level < m_level) char buffer[300U];
return;
bool ret = ::LogOpen();
if (!ret)
return;
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
SYSTEMTIME st; SYSTEMTIME st;
::GetSystemTime(&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); ::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);
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);
#else #else
struct timeval now; struct timeval now;
::gettimeofday(&now, NULL); ::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); ::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);
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);
#endif #endif
va_list vl; va_list vl;
va_start(vl, fmt); va_start(vl, fmt);
char buffer[200U]; ::vsprintf(buffer + ::strlen(buffer), fmt, vl);
::vsprintf(buffer, fmt, vl);
va_end(vl); va_end(vl);
::fprintf(m_fpLog, "%s\n", buffer); if (level >= m_fileLevel && m_fileLevel != 0U) {
::fflush(m_fpLog); bool ret = ::LogOpen();
if (!ret)
return;
if (m_display) { ::fprintf(m_fpLog, "%s\n", buffer);
::fprintf(stdout, "%s\n", buffer); ::fflush(m_fpLog);
}
if (level >= m_displayLevel && m_displayLevel != 0U) {
::fprintf(stdout, "%s\n", buffer);
::fflush(stdout); ::fflush(stdout);
} }
if (level == 6U) { // Fatal if (level == 6U) { // Fatal
::fclose(m_fpLog); ::fclose(m_fpLog);
exit(1); exit(1);
} }

6
Log.h
View file

@ -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 * 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 * 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 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 LogFinalise();
extern void LogSetLevel(unsigned int level);
#endif #endif

View file

@ -18,10 +18,10 @@ URL=www.google.co.uk
[Log] [Log]
# Logging levels, 0=No logging # Logging levels, 0=No logging
Level=1 DisplayLevel=1
Path=. FileLevel=1
Root=MMDVM FilePath=.
Display=1 FileRoot=MMDVM
[Modem] [Modem]
# Port=/dev/ttyACM0 # Port=/dev/ttyACM0

View file

@ -95,14 +95,12 @@ int CMMDVMHost::run()
return 1; 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) { if (!ret) {
::fprintf(stderr, "MMDVMHost: unable to open the log file\n"); ::fprintf(stderr, "MMDVMHost: unable to open the log file\n");
return 1; return 1;
} }
::LogSetLevel(m_conf.getLogLevel());
LogInfo(HEADER1); LogInfo(HEADER1);
LogInfo(HEADER2); LogInfo(HEADER2);
LogInfo(HEADER3); LogInfo(HEADER3);