Skip to content

Commit

Permalink
Add optional log file rotation and UDP socket reopening.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Oct 31, 2020
1 parent 34b3bc6 commit a0d60b8
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 8 deletions.
8 changes: 8 additions & 0 deletions Conf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ m_logDisplayLevel(0U),
m_logFileLevel(0U),
m_logFilePath(),
m_logFileRoot(),
m_logFileRotate(true),
m_cwIdEnabled(false),
m_cwIdTime(10U),
m_cwIdCallsign(),
Expand Down Expand Up @@ -431,6 +432,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, "FileRotate") == 0)
m_logFileRotate = ::atoi(value) == 1;
} else if (section == SECTION_CWID) {
if (::strcmp(key, "Enable") == 0)
m_cwIdEnabled = ::atoi(value) == 1;
Expand Down Expand Up @@ -1029,6 +1032,11 @@ std::string CConf::getLogFileRoot() const
return m_logFileRoot;
}

bool CConf::getLogFileRotate() const
{
return m_logFileRotate;
}

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 @@ -48,6 +48,7 @@ class CConf
unsigned int getLogFileLevel() const;
std::string getLogFilePath() const;
std::string getLogFileRoot() const;
bool getLogFileRotate() const;

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

bool m_cwIdEnabled;
unsigned int m_cwIdTime;
Expand Down
43 changes: 41 additions & 2 deletions Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
static unsigned int m_fileLevel = 2U;
static std::string m_filePath;
static std::string m_fileRoot;
static bool m_fileRotate = true;

static FILE* m_fpLog = NULL;
static bool m_daemon = false;
Expand All @@ -45,7 +46,7 @@ static struct tm m_tm;

static char LEVELS[] = " DMIWEF";

static bool LogOpen()
static bool logOpenRotate()
{
bool status = false;

Expand Down Expand Up @@ -86,13 +87,51 @@ static bool LogOpen()
return status;
}

bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel)
static bool logOpenNoRotate()
{
bool status = false;

if (m_fileLevel == 0U)
return true;

if (m_fpLog != NULL)
return true;

char filename[200U];
#if defined(_WIN32) || defined(_WIN64)
::sprintf(filename, "%s\\%s.log", m_filePath.c_str(), m_fileRoot.c_str());
#else
::sprintf(filename, "%s/%s.log", m_filePath.c_str(), m_fileRoot.c_str());
#endif

if ((m_fpLog = ::fopen(filename, "a+t")) != NULL) {
status = true;

#if !defined(_WIN32) && !defined(_WIN64)
if (m_daemon)
dup2(fileno(m_fpLog), fileno(stderr));
#endif
}

return status;
}

bool LogOpen()
{
if (m_fileRotate)
return logOpenRotate();
else
return logOpenNoRotate();
}

bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, bool rotate)
{
m_filePath = filePath;
m_fileRoot = fileRoot;
m_fileLevel = fileLevel;
m_displayLevel = displayLevel;
m_daemon = daemon;
m_fileRotate = rotate;

if (m_daemon)
m_displayLevel = 0U;
Expand Down
4 changes: 2 additions & 2 deletions Log.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX
* Copyright (C) 2015,2016,2020 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,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, bool rotate);
extern void LogFinalise();

#endif
1 change: 1 addition & 0 deletions MMDVM.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ DisplayLevel=1
FileLevel=1
FilePath=.
FileRoot=MMDVM
FileRotate=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 @@ -241,9 +241,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.getLogFileRotate());
#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.getLogFileRotate());
#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 @@ -53,7 +53,7 @@ m_port(port)
{
CUDPSocket::startup();

::LogInitialise(false, ".", "RemoteCommand", 2U, 2U);
::LogInitialise(false, ".", "RemoteCommand", 2U, 2U, false);
}

CRemoteCommand::~CRemoteCommand()
Expand Down
6 changes: 6 additions & 0 deletions UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ int CUDPSocket::read(unsigned char* buffer, unsigned int length, sockaddr_storag
LogError("Error returned from recvfrom, err: %lu", ::GetLastError());
#else
LogError("Error returned from recvfrom, err: %d", errno);

if (len == -1 && errno == ENOTSOCK) {
LogMessage("Re-opening UDP port on %u", m_port);
close();
open();
}
#endif
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
#if !defined(VERSION_H)
#define VERSION_H

const char* VERSION = "20201013";
const char* VERSION = "20201031";

#endif

0 comments on commit a0d60b8

Please sign in to comment.