pass instance of std::ostream for logging from API

pull/115/head
orignal 10 years ago
parent b7d1b74ffa
commit bb05bcf39f

@ -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;
}

21
Log.h

@ -32,11 +32,12 @@ class Log: public i2p::util::MsgQueue<LogMsg>
{
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<LogMsg>
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<typename... TArgs>
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;

@ -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();

@ -2,6 +2,7 @@
#define API_H__
#include <memory>
#include <iostream>
#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 <appName>.log in application's folder
void StopI2P ();
// destinations

Loading…
Cancel
Save