mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-28 11:47:48 +02:00
pass instance of std::ostream for logging from API
This commit is contained in:
parent
b7d1b74ffa
commit
bb05bcf39f
4 changed files with 39 additions and 18 deletions
21
Log.h
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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue