Skip to content

Commit

Permalink
Merge pull request g4klx#648 from xfxian/logrotate
Browse files Browse the repository at this point in the history
Introducing RotateLog configuration option that allows disabling timestamps on the logfiles.
  • Loading branch information
phl0 authored Oct 29, 2020
2 parents cfc313e + 7a5bbda commit 9932394
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ RemoteCommand
*.user
*.VC.db
.vs
.vscode
*.ambe
GitVersion.h
8 changes: 8 additions & 0 deletions Conf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ m_logDisplayLevel(0U),
m_logFileLevel(0U),
m_logFilePath(),
m_logFileRoot(),
m_logTimestampLogs(1U),
m_cwIdEnabled(false),
m_cwIdTime(10U),
m_cwIdCallsign(),
Expand Down Expand Up @@ -453,6 +454,8 @@ bool CConf::read()
m_logFileLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "DisplayLevel") == 0)
m_logDisplayLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "TimestampLogs") == 0)
m_logTimestampLogs = (unsigned int)::atoi(value);
} else if (section == SECTION_CWID) {
if (::strcmp(key, "Enable") == 0)
m_cwIdEnabled = ::atoi(value) == 1;
Expand Down Expand Up @@ -1074,6 +1077,11 @@ std::string CConf::getLogFileRoot() const
return m_logFileRoot;
}

unsigned int CConf::getLogTimestampLogs() const
{
return m_logTimestampLogs;
}

bool CConf::getCWIdEnabled() const
{
return m_cwIdEnabled;
Expand Down
2 changes: 2 additions & 0 deletions Conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class CConf
unsigned int getLogFileLevel() const;
std::string getLogFilePath() const;
std::string getLogFileRoot() const;
unsigned int getLogTimestampLogs() const;

// The CW ID section
bool getCWIdEnabled() const;
Expand Down Expand Up @@ -337,6 +338,7 @@ class CConf
unsigned int m_logFileLevel;
std::string m_logFilePath;
std::string m_logFileRoot;
unsigned int m_logTimestampLogs;

bool m_cwIdEnabled;
unsigned int m_cwIdTime;
Expand Down
15 changes: 12 additions & 3 deletions Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static bool m_daemon = false;

static unsigned int m_displayLevel = 2U;

static unsigned int m_timestampLogs = 1U;

static struct tm m_tm;

static char LEVELS[] = " DMIWEF";
Expand All @@ -66,10 +68,16 @@ static bool LogOpen()
}

char filename[200U];
char timestamp[37U] = "";

if (m_timestampLogs) {
::sprintf(timestamp, "-%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
}

#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
::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

if ((m_fpLog = ::fopen(filename, "a+t")) != NULL) {
Expand All @@ -86,12 +94,13 @@ static bool LogOpen()
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 timestampLogs)
{
m_filePath = filePath;
m_fileRoot = fileRoot;
m_fileLevel = fileLevel;
m_displayLevel = displayLevel;
m_timestampLogs = timestampLogs;
m_daemon = daemon;

if (m_daemon)
Expand Down
2 changes: 1 addition & 1 deletion Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

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 timestampLogs);
extern void LogFinalise();

#endif
1 change: 1 addition & 0 deletions MMDVM.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ DisplayLevel=1
FileLevel=1
FilePath=.
FileRoot=MMDVM
TimestampLogs=1

[CW Id]
Enable=1
Expand Down
4 changes: 2 additions & 2 deletions MMDVMHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ int CMMDVMHost::run()
#endif

#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.getLogTimestampLogs());
#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.getLogTimestampLogs());
#endif
if (!ret) {
::fprintf(stderr, "MMDVMHost: unable to open the log file\n");
Expand Down
2 changes: 1 addition & 1 deletion RemoteCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int main(int argc, char** argv)
CRemoteCommand::CRemoteCommand(unsigned int port) :
m_port(port)
{
::LogInitialise(false, ".", "RemoteCommand", 2U, 2U);
::LogInitialise(false, ".", "RemoteCommand", 2U, 2U, 1U);
}

CRemoteCommand::~CRemoteCommand()
Expand Down

0 comments on commit 9932394

Please sign in to comment.