Modify the logging system greatly.
This commit is contained in:
parent
bfb74d3155
commit
8559f97efb
40
Conf.cpp
40
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
|
||||
|
|
16
Conf.h
16
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;
|
||||
|
|
72
Log.cpp
72
Log.cpp
|
@ -30,14 +30,13 @@
|
|||
#include <ctime>
|
||||
#include <cassert>
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
6
Log.h
6
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue