diff --git a/Log.cpp b/Log.cpp index fc37ebf..f600b90 100644 --- a/Log.cpp +++ b/Log.cpp @@ -22,6 +22,7 @@ #include #else #include +#include #endif #include @@ -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(); } diff --git a/Log.h b/Log.h index d671ef9..0d00653 100644 --- a/Log.h +++ b/Log.h @@ -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 diff --git a/MMDVMHost.cpp b/MMDVMHost.cpp index c6f5137..4e54bbd 100644 --- a/MMDVMHost.cpp +++ b/MMDVMHost.cpp @@ -240,7 +240,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; @@ -250,7 +250,6 @@ int CMMDVMHost::run() if (m_daemon) { ::close(STDIN_FILENO); ::close(STDOUT_FILENO); - ::close(STDERR_FILENO); } #endif diff --git a/RemoteCommand.cpp b/RemoteCommand.cpp index bf3fb1e..fd96e46 100644 --- a/RemoteCommand.cpp +++ b/RemoteCommand.cpp @@ -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()