Skip to content

Commit

Permalink
Modify the logging system greatly.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Mar 7, 2016
1 parent bfb74d3 commit 8559f97
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 79 deletions.
40 changes: 20 additions & 20 deletions Conf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions Conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class CConf
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;
Expand Down Expand Up @@ -121,10 +121,10 @@ class CConf
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;
Expand Down
72 changes: 32 additions & 40 deletions Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@
#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;

static char LEVELS[] = " DMIWEF";

static bool LogOpen()
{
if (m_fileLevel == 0U)
return true;

time_t now;
::time(&now);

Expand All @@ -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");
Expand All @@ -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();
}

Expand All @@ -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;

::fprintf(m_fpLog, "%s\n", buffer);
::fflush(m_fpLog);
}

if (m_display) {
::fprintf(stdout, "%s\n", buffer);
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);
}
Expand Down
6 changes: 2 additions & 4 deletions Log.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions MMDVM.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions MMDVMHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8559f97

Please sign in to comment.