diff --git a/Daemon.cpp b/Daemon.cpp index e0c93cb3..ebd9a52d 100644 --- a/Daemon.cpp +++ b/Daemon.cpp @@ -63,12 +63,7 @@ namespace i2p #else logfile_path.append("\\debug.log"); #endif - logfile.open(logfile_path, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc); - - if (!logfile.is_open()) - exit(-17); - - LogPrint("Logging to file enabled."); + g_Log.SetLogFile (logfile_path); LogPrint("CMD parameters:"); for (int i = 0; i < argc; ++i) diff --git a/Daemon.h b/Daemon.h index 4e27d108..f8f98467 100644 --- a/Daemon.h +++ b/Daemon.h @@ -1,5 +1,5 @@ #pragma once -#include +#include #ifdef _WIN32 #define Daemon i2p::util::DaemonWin32::Instance() @@ -24,8 +24,6 @@ namespace i2p int running; - std::ofstream logfile; - protected: Daemon_Singleton(); virtual ~Daemon_Singleton(); diff --git a/Log.cpp b/Log.cpp index 92fd8a25..4a88f2ba 100644 --- a/Log.cpp +++ b/Log.cpp @@ -1,20 +1,29 @@ #include "Log.h" -#include "Daemon.h" - Log g_Log; void LogMsg::Process() { - if (Daemon.isLogging == 1 && Daemon.logfile.is_open()) - Daemon.logfile << s.str(); - output << s.str(); + + std::cout << s.str (); // TODO: delete later } void Log::Flush () { - if (Daemon.isLogging == 1 && Daemon.logfile.is_open()) - Daemon.logfile.flush(); + if (m_LogFile) + m_LogFile->flush(); } +void Log::SetLogFile (const std::string& fullFilePath) +{ + if (m_LogFile) delete m_LogFile; + m_LogFile = new std::ofstream (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc); + if (m_LogFile->is_open ()) + LogPrint("Logging to file ", fullFilePath, " enabled."); + else + { + delete m_LogFile; + m_LogFile = nullptr; + } +} diff --git a/Log.h b/Log.h index a5856ba7..f3e7ae53 100644 --- a/Log.h +++ b/Log.h @@ -1,8 +1,10 @@ #ifndef LOG_H__ #define LOG_H__ +#include #include #include +#include #include #include "Queue.h" @@ -20,11 +22,19 @@ class Log: public i2p::util::MsgQueue { public: - Log () { SetOnEmpty (std::bind (&Log::Flush, this)); }; + Log (): m_LogFile (nullptr) { SetOnEmpty (std::bind (&Log::Flush, this)); }; + ~Log () { delete m_LogFile; }; + + void SetLogFile (const std::string& fullFilePath); + std::ofstream * GetLogFile () const { return m_LogFile; }; private: void Flush (); + + private: + + std::ofstream * m_LogFile; }; extern Log g_Log; @@ -45,7 +55,7 @@ void LogPrint (std::stringstream& s, TValue arg, TArgs... args) template void LogPrint (TArgs... args) { - LogMsg * msg = new LogMsg (); + LogMsg * msg = g_Log.GetLogFile () ? new LogMsg (*g_Log.GetLogFile ()) : new LogMsg (); LogPrint (msg->s, args...); msg->s << std::endl; g_Log.Put (msg);