mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 04:04:16 +00:00
pass instance of std::ostream for logging from API
This commit is contained in:
parent
b7d1b74ffa
commit
bb05bcf39f
25
Log.cpp
25
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 ())
|
|
||||||
LogPrint("Logging to file ", fullFilePath, " enabled.");
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
delete m_LogFile;
|
SetLogStream (logFile);
|
||||||
m_LogFile = nullptr;
|
LogPrint("Logging to file ", fullFilePath, " enabled.");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
delete logFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Log::SetLogStream (std::ostream * logStream)
|
||||||
|
{
|
||||||
|
if (m_LogStream) delete m_LogStream;
|
||||||
|
m_LogStream = logStream;
|
||||||
}
|
}
|
||||||
|
21
Log.h
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;
|
||||||
|
@ -21,9 +21,12 @@ namespace api
|
|||||||
i2p::context.Init ();
|
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();
|
i2p::data::netdb.Start();
|
||||||
LogPrint("NetDB started");
|
LogPrint("NetDB started");
|
||||||
i2p::transport::transports.Start();
|
i2p::transport::transports.Start();
|
||||||
|
@ -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…
x
Reference in New Issue
Block a user