From bb05bcf39ff16c36c8251bf9d452d53445b6bce4 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 8 Dec 2014 16:27:10 -0500 Subject: [PATCH] pass instance of std::ostream for logging from API --- Log.cpp | 23 ++++++++++++++--------- Log.h | 21 ++++++++++++++++----- api/api.cpp | 7 +++++-- api/api.h | 4 +++- 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/Log.cpp b/Log.cpp index 10bf42ad..ac2ba756 100644 --- a/Log.cpp +++ b/Log.cpp @@ -20,19 +20,24 @@ void LogMsg::Process() void Log::Flush () { - if (m_LogFile) - m_LogFile->flush(); + if (m_LogStream) + m_LogStream->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 ()) + auto logFile = new std::ofstream (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc); + if (logFile->is_open ()) + { + SetLogStream (logFile); LogPrint("Logging to file ", fullFilePath, " enabled."); + } else - { - delete m_LogFile; - m_LogFile = nullptr; - } + delete logFile; +} + +void Log::SetLogStream (std::ostream * logStream) +{ + if (m_LogStream) delete m_LogStream; + m_LogStream = logStream; } diff --git a/Log.h b/Log.h index 41ab0ea2..b77528c5 100644 --- a/Log.h +++ b/Log.h @@ -32,11 +32,12 @@ class Log: public i2p::util::MsgQueue { public: - Log (): m_LogFile (nullptr) { SetOnEmpty (std::bind (&Log::Flush, this)); }; - ~Log () { delete m_LogFile; }; + Log (): m_LogStream (nullptr) { SetOnEmpty (std::bind (&Log::Flush, this)); }; + ~Log () { delete m_LogStream; }; void SetLogFile (const std::string& fullFilePath); - std::ofstream * GetLogFile () const { return m_LogFile; }; + void SetLogStream (std::ostream * logStream); + std::ostream * GetLogStream () const { return m_LogStream; }; private: @@ -44,7 +45,7 @@ class Log: public i2p::util::MsgQueue private: - std::ofstream * m_LogFile; + std::ostream * m_LogStream; }; extern Log * g_Log; @@ -59,6 +60,16 @@ inline void StartLog (const std::string& fullFilePath) } } +inline void StartLog (std::ostream * s) +{ + if (!g_Log) + { + g_Log = new Log (); + if (s) + g_Log->SetLogStream (s); + } +} + inline void StopLog () { if (g_Log) @@ -84,7 +95,7 @@ void LogPrint (std::stringstream& s, TValue arg, TArgs... args) template void LogPrint (LogLevel level, TArgs... args) { - LogMsg * msg = (g_Log && g_Log->GetLogFile ()) ? new LogMsg (*g_Log->GetLogFile (), level) : + LogMsg * msg = (g_Log && g_Log->GetLogStream ()) ? new LogMsg (*g_Log->GetLogStream (), level) : new LogMsg (std::cout, level); LogPrint (msg->s, args...); msg->s << std::endl; diff --git a/api/api.cpp b/api/api.cpp index daf82722..c9e69229 100644 --- a/api/api.cpp +++ b/api/api.cpp @@ -21,9 +21,12 @@ namespace api i2p::context.Init (); } - void StartI2P () + void StartI2P (std::ostream * logStream) { - StartLog (i2p::util::filesystem::GetAppName () + ".log"); + if (logStream) + StartLog (logStream); + else + StartLog (i2p::util::filesystem::GetAppName () + ".log"); i2p::data::netdb.Start(); LogPrint("NetDB started"); i2p::transport::transports.Start(); diff --git a/api/api.h b/api/api.h index 72db509f..1088487b 100644 --- a/api/api.h +++ b/api/api.h @@ -2,6 +2,7 @@ #define API_H__ #include +#include #include "Identity.h" #include "Destination.h" #include "Streaming.h" @@ -12,7 +13,8 @@ namespace api { // initialization start and stop void InitI2P (int argc, char* argv[], const char * appName); - void StartI2P (); + void StartI2P (std::ostream * logStream = nullptr); + // write system log to logStream, if not specified to .log in application's folder void StopI2P (); // destinations