Redirect stderr messages to the log file, as in daemon mode assert() failure are silent and make wrong INI file hard to fix.

This commit is contained in:
Daniel Caujolle-Bert 2020-05-09 12:11:08 +02:00
parent 5410ca3ce8
commit c3efabf56c
No known key found for this signature in database
GPG Key ID: A7B7A4BEAA82789B
4 changed files with 20 additions and 7 deletions

20
Log.cpp
View File

@ -22,6 +22,7 @@
#include <Windows.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif
#include <cstdio>
@ -36,6 +37,7 @@ static std::string m_filePath;
static std::string m_fileRoot;
static FILE* m_fpLog = NULL;
static bool m_daemon = false;
static unsigned int m_displayLevel = 2U;
@ -45,6 +47,8 @@ static char LEVELS[] = " DMIWEF";
static bool LogOpen()
{
bool status = false;
if (m_fileLevel == 0U)
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);
#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;
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_fileRoot = fileRoot;
m_fileLevel = fileLevel;
m_displayLevel = displayLevel;
m_daemon = daemon;
return ::LogOpen();
}

2
Log.h
View File

@ -30,7 +30,7 @@
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();
#endif

View File

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

View File

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