Merge branch 'FM' into FM_Ext

This commit is contained in:
Jonathan Naylor 2020-05-09 12:36:44 +01:00
commit 2a8e1ce9f4
4 changed files with 20 additions and 7 deletions

20
Log.cpp
View file

@ -22,6 +22,7 @@
#include <Windows.h> #include <Windows.h>
#else #else
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h>
#endif #endif
#include <cstdio> #include <cstdio>
@ -36,6 +37,7 @@ static std::string m_filePath;
static std::string m_fileRoot; static std::string m_fileRoot;
static FILE* m_fpLog = NULL; static FILE* m_fpLog = NULL;
static bool m_daemon = false;
static unsigned int m_displayLevel = 2U; static unsigned int m_displayLevel = 2U;
@ -45,6 +47,8 @@ static char LEVELS[] = " DMIWEF";
static bool LogOpen() static bool LogOpen()
{ {
bool status = false;
if (m_fileLevel == 0U) if (m_fileLevel == 0U)
return true; return true;
@ -68,18 +72,28 @@ static bool LogOpen()
::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-%04d-%02d-%02d.log", m_filePath.c_str(), m_fileRoot.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
#endif #endif
m_fpLog = ::fopen(filename, "a+t"); 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
}
m_tm = *tm; m_tm = *tm;
return m_fpLog != NULL; return status;
} }
bool LogInitialise(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)
{ {
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_daemon = daemon;
return ::LogOpen(); return ::LogOpen();
} }

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

View file

@ -240,7 +240,7 @@ int CMMDVMHost::run()
#endif #endif
#endif #endif
ret = ::LogInitialise(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());
if (!ret) { if (!ret) {
::fprintf(stderr, "MMDVMHost: unable to open the log file\n"); ::fprintf(stderr, "MMDVMHost: unable to open the log file\n");
return 1; return 1;
@ -250,7 +250,6 @@ int CMMDVMHost::run()
if (m_daemon) { if (m_daemon) {
::close(STDIN_FILENO); ::close(STDIN_FILENO);
::close(STDOUT_FILENO); ::close(STDOUT_FILENO);
::close(STDERR_FILENO);
} }
#endif #endif

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(".", "RemoteCommand", 2U, 2U); ::LogInitialise(false, ".", "RemoteCommand", 2U, 2U);
} }
CRemoteCommand::~CRemoteCommand() CRemoteCommand::~CRemoteCommand()