Introducing RotateLog configuration option that allows disabling timestamps on the logfiles.

This commit is contained in:
Jacob Schramm
2020-10-28 23:19:24 +01:00
parent cfc313e5b9
commit 53ae146667
8 changed files with 28 additions and 7 deletions

1
.gitignore vendored
View File

@@ -15,5 +15,6 @@ RemoteCommand
*.user *.user
*.VC.db *.VC.db
.vs .vs
.vscode
*.ambe *.ambe
GitVersion.h GitVersion.h

View File

@@ -82,6 +82,7 @@ m_logDisplayLevel(0U),
m_logFileLevel(0U), m_logFileLevel(0U),
m_logFilePath(), m_logFilePath(),
m_logFileRoot(), m_logFileRoot(),
m_logRotateLogs(1U),
m_cwIdEnabled(false), m_cwIdEnabled(false),
m_cwIdTime(10U), m_cwIdTime(10U),
m_cwIdCallsign(), m_cwIdCallsign(),
@@ -453,6 +454,8 @@ bool CConf::read()
m_logFileLevel = (unsigned int)::atoi(value); m_logFileLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "DisplayLevel") == 0) else if (::strcmp(key, "DisplayLevel") == 0)
m_logDisplayLevel = (unsigned int)::atoi(value); m_logDisplayLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "RotateLogs") == 0)
m_logRotateLogs = (unsigned int)::atoi(value);
} else if (section == SECTION_CWID) { } else if (section == SECTION_CWID) {
if (::strcmp(key, "Enable") == 0) if (::strcmp(key, "Enable") == 0)
m_cwIdEnabled = ::atoi(value) == 1; m_cwIdEnabled = ::atoi(value) == 1;
@@ -1074,6 +1077,11 @@ std::string CConf::getLogFileRoot() const
return m_logFileRoot; return m_logFileRoot;
} }
unsigned int CConf::getLogRotateLogs() const
{
return m_logRotateLogs;
}
bool CConf::getCWIdEnabled() const bool CConf::getCWIdEnabled() const
{ {
return m_cwIdEnabled; return m_cwIdEnabled;

2
Conf.h
View File

@@ -54,6 +54,7 @@ public:
unsigned int getLogFileLevel() const; unsigned int getLogFileLevel() const;
std::string getLogFilePath() const; std::string getLogFilePath() const;
std::string getLogFileRoot() const; std::string getLogFileRoot() const;
unsigned int getLogRotateLogs() const;
// The CW ID section // The CW ID section
bool getCWIdEnabled() const; bool getCWIdEnabled() const;
@@ -337,6 +338,7 @@ private:
unsigned int m_logFileLevel; unsigned int m_logFileLevel;
std::string m_logFilePath; std::string m_logFilePath;
std::string m_logFileRoot; std::string m_logFileRoot;
unsigned int m_logRotateLogs;
bool m_cwIdEnabled; bool m_cwIdEnabled;
unsigned int m_cwIdTime; unsigned int m_cwIdTime;

15
Log.cpp
View File

@@ -41,6 +41,8 @@ static bool m_daemon = false;
static unsigned int m_displayLevel = 2U; static unsigned int m_displayLevel = 2U;
static unsigned int m_rotateLogs = 1U;
static struct tm m_tm; static struct tm m_tm;
static char LEVELS[] = " DMIWEF"; static char LEVELS[] = " DMIWEF";
@@ -66,10 +68,16 @@ static bool LogOpen()
} }
char filename[200U]; char filename[200U];
char timestamp[37U] = "";
if (m_rotateLogs) {
::sprintf(timestamp, "-%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
}
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
::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); ::sprintf(filename, "%s\\%s%s.log", m_filePath.c_str(), m_fileRoot.c_str(), timestamp);
#else #else
::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); ::sprintf(filename, "%s/%s%s.log", m_filePath.c_str(), m_fileRoot.c_str(), timestamp);
#endif #endif
if ((m_fpLog = ::fopen(filename, "a+t")) != NULL) { if ((m_fpLog = ::fopen(filename, "a+t")) != NULL) {
@@ -86,12 +94,13 @@ static bool LogOpen()
return status; return status;
} }
bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel) bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, unsigned int rotateLogs)
{ {
m_filePath = filePath; m_filePath = filePath;
m_fileRoot = fileRoot; m_fileRoot = fileRoot;
m_fileLevel = fileLevel; m_fileLevel = fileLevel;
m_displayLevel = displayLevel; m_displayLevel = displayLevel;
m_rotateLogs = rotateLogs;
m_daemon = daemon; m_daemon = daemon;
if (m_daemon) if (m_daemon)

2
Log.h
View File

@@ -30,7 +30,7 @@
extern void Log(unsigned int level, const char* fmt, ...); extern void Log(unsigned int level, const char* fmt, ...);
extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel); extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, unsigned int rotateLogs);
extern void LogFinalise(); extern void LogFinalise();
#endif #endif

View File

@@ -26,6 +26,7 @@ DisplayLevel=1
FileLevel=1 FileLevel=1
FilePath=. FilePath=.
FileRoot=MMDVM FileRoot=MMDVM
RotateLogs=1
[CW Id] [CW Id]
Enable=1 Enable=1

View File

@@ -242,9 +242,9 @@ int CMMDVMHost::run()
#endif #endif
#if !defined(_WIN32) && !defined(_WIN64) #if !defined(_WIN32) && !defined(_WIN64)
ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogRotateLogs());
#else #else
ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogRotateLogs());
#endif #endif
if (!ret) { if (!ret) {
::fprintf(stderr, "MMDVMHost: unable to open the log file\n"); ::fprintf(stderr, "MMDVMHost: unable to open the log file\n");

View File

@@ -51,7 +51,7 @@ int main(int argc, char** argv)
CRemoteCommand::CRemoteCommand(unsigned int port) : CRemoteCommand::CRemoteCommand(unsigned int port) :
m_port(port) m_port(port)
{ {
::LogInitialise(false, ".", "RemoteCommand", 2U, 2U); ::LogInitialise(false, ".", "RemoteCommand", 2U, 2U, 1U);
} }
CRemoteCommand::~CRemoteCommand() CRemoteCommand::~CRemoteCommand()