Browse Source

pass instance of std::ostream for logging from API

pull/115/head
orignal 10 years ago
parent
commit
bb05bcf39f
  1. 21
      Log.cpp
  2. 21
      Log.h
  3. 5
      api/api.cpp
  4. 4
      api/api.h

21
Log.cpp

@ -20,19 +20,24 @@ void LogMsg::Process()
void Log::Flush () void Log::Flush ()
{ {
if (m_LogFile) if (m_LogStream)
m_LogFile->flush(); m_LogStream->flush();
} }
void Log::SetLogFile (const std::string& fullFilePath) void Log::SetLogFile (const std::string& fullFilePath)
{ {
if (m_LogFile) delete m_LogFile; auto logFile = new std::ofstream (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
m_LogFile = new std::ofstream (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc); if (logFile->is_open ())
if (m_LogFile->is_open ()) {
SetLogStream (logFile);
LogPrint("Logging to file ", fullFilePath, " enabled."); LogPrint("Logging to file ", fullFilePath, " enabled.");
}
else else
{ delete logFile;
delete m_LogFile;
m_LogFile = nullptr;
} }
void Log::SetLogStream (std::ostream * logStream)
{
if (m_LogStream) delete m_LogStream;
m_LogStream = logStream;
} }

21
Log.h

@ -32,11 +32,12 @@ class Log: public i2p::util::MsgQueue<LogMsg>
{ {
public: public:
Log (): m_LogFile (nullptr) { SetOnEmpty (std::bind (&Log::Flush, this)); }; Log (): m_LogStream (nullptr) { SetOnEmpty (std::bind (&Log::Flush, this)); };
~Log () { delete m_LogFile; }; ~Log () { delete m_LogStream; };
void SetLogFile (const std::string& fullFilePath); 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: private:
@ -44,7 +45,7 @@ class Log: public i2p::util::MsgQueue<LogMsg>
private: private:
std::ofstream * m_LogFile; std::ostream * m_LogStream;
}; };
extern Log * g_Log; 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 () inline void StopLog ()
{ {
if (g_Log) if (g_Log)
@ -84,7 +95,7 @@ void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
template<typename... TArgs> template<typename... TArgs>
void LogPrint (LogLevel level, TArgs... args) 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); new LogMsg (std::cout, level);
LogPrint (msg->s, args...); LogPrint (msg->s, args...);
msg->s << std::endl; msg->s << std::endl;

5
api/api.cpp

@ -21,8 +21,11 @@ namespace api
i2p::context.Init (); i2p::context.Init ();
} }
void StartI2P () void StartI2P (std::ostream * logStream)
{ {
if (logStream)
StartLog (logStream);
else
StartLog (i2p::util::filesystem::GetAppName () + ".log"); StartLog (i2p::util::filesystem::GetAppName () + ".log");
i2p::data::netdb.Start(); i2p::data::netdb.Start();
LogPrint("NetDB started"); LogPrint("NetDB started");

4
api/api.h

@ -2,6 +2,7 @@
#define API_H__ #define API_H__
#include <memory> #include <memory>
#include <iostream>
#include "Identity.h" #include "Identity.h"
#include "Destination.h" #include "Destination.h"
#include "Streaming.h" #include "Streaming.h"
@ -12,7 +13,8 @@ namespace api
{ {
// initialization start and stop // initialization start and stop
void InitI2P (int argc, char* argv[], const char * appName); 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 <appName>.log in application's folder
void StopI2P (); void StopI2P ();
// destinations // destinations

Loading…
Cancel
Save