From 98d5e0b56d321b72340212564a662aa2e6d6a06e Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 4 Feb 2016 13:53:38 -0500 Subject: [PATCH] #355. reopen log file by SIGHUP --- DaemonLinux.cpp | 6 +++--- Log.cpp | 11 +++++++++++ Log.h | 8 ++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/DaemonLinux.cpp b/DaemonLinux.cpp index 89df5306..9382a878 100644 --- a/DaemonLinux.cpp +++ b/DaemonLinux.cpp @@ -17,9 +17,9 @@ void handle_signal(int sig) switch (sig) { case SIGHUP: - LogPrint(eLogInfo, "Daemon: Got SIGHUP, doing nothing"); - // TODO: - break; + LogPrint(eLogInfo, "Daemon: Got SIGHUP, reopening log..."); + ReopenLogFile (); + break; case SIGABRT: case SIGTERM: case SIGINT: diff --git a/Log.cpp b/Log.cpp index d507b7dc..6391bcae 100644 --- a/Log.cpp +++ b/Log.cpp @@ -46,6 +46,7 @@ void Log::Flush () void Log::SetLogFile (const std::string& fullFilePath) { + m_FullFilePath = fullFilePath; auto logFile = std::make_shared (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc); if (logFile->is_open ()) { @@ -54,6 +55,16 @@ void Log::SetLogFile (const std::string& fullFilePath) } } +void Log::ReopenLogFile () +{ + if (m_FullFilePath.length () > 0) + { + SetLogFile (m_FullFilePath); + LogPrint(eLogInfo, "Log: file ", m_FullFilePath, " reopen"); + } +} + + void Log::SetLogLevel (const std::string& level) { if (level == "error") { m_MinLevel = eLogError; } diff --git a/Log.h b/Log.h index ec1dc015..05bae7d9 100644 --- a/Log.h +++ b/Log.h @@ -39,6 +39,7 @@ class Log: public i2p::util::MsgQueue ~Log () {}; void SetLogFile (const std::string& fullFilePath); + void ReopenLogFile (); void SetLogLevel (const std::string& level); void SetLogStream (std::shared_ptr logStream); std::shared_ptr GetLogStream () const { return m_LogStream; }; @@ -51,6 +52,7 @@ class Log: public i2p::util::MsgQueue private: + std::string m_FullFilePath; // empty if stream std::shared_ptr m_LogStream; enum LogLevel m_MinLevel; std::string m_Timestamp; @@ -102,6 +104,12 @@ inline void SetLogLevel (const std::string& level) g_Log->SetLogLevel(level); } +inline void ReopenLogFile () +{ + if (g_Log) + g_Log->ReopenLogFile (); +} + template void LogPrint (std::stringstream& s, TValue arg) {