mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 21:37:17 +01:00
use shared_ptr for log's stream
This commit is contained in:
parent
2e7ce38552
commit
7ca1cfab1a
12
Log.cpp
12
Log.cpp
|
@ -13,7 +13,8 @@ static const char * g_LogLevelStr[eNumLogLevels] =
|
||||||
|
|
||||||
void LogMsg::Process()
|
void LogMsg::Process()
|
||||||
{
|
{
|
||||||
auto& output = (log && log->GetLogStream ()) ? *log->GetLogStream () : std::cerr;
|
auto stream = log ? log->GetLogStream () : nullptr;
|
||||||
|
auto& output = stream ? *stream : std::cout;
|
||||||
if (log)
|
if (log)
|
||||||
output << log->GetTimestamp ();
|
output << log->GetTimestamp ();
|
||||||
else
|
else
|
||||||
|
@ -45,14 +46,12 @@ void Log::Flush ()
|
||||||
|
|
||||||
void Log::SetLogFile (const std::string& fullFilePath)
|
void Log::SetLogFile (const std::string& fullFilePath)
|
||||||
{
|
{
|
||||||
auto logFile = new std::ofstream (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
|
auto logFile = std::make_shared<std::ofstream> (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
|
||||||
if (logFile->is_open ())
|
if (logFile->is_open ())
|
||||||
{
|
{
|
||||||
SetLogStream (logFile);
|
SetLogStream (logFile);
|
||||||
LogPrint(eLogInfo, "Log: will send messages to ", fullFilePath);
|
LogPrint(eLogInfo, "Log: will send messages to ", fullFilePath);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
delete logFile;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::SetLogLevel (const std::string& level)
|
void Log::SetLogLevel (const std::string& level)
|
||||||
|
@ -65,11 +64,10 @@ void Log::SetLogLevel (const std::string& level)
|
||||||
LogPrint(eLogError, "Log: Unknown loglevel: ", level);
|
LogPrint(eLogError, "Log: Unknown loglevel: ", level);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LogPrint(eLogInfo, "Log: min messages level set to ", level);
|
LogPrint(eLogInfo, "Log: min msg level set to ", level);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::SetLogStream (std::ostream * logStream)
|
void Log::SetLogStream (std::shared_ptr<std::ostream> logStream)
|
||||||
{
|
{
|
||||||
if (m_LogStream) delete m_LogStream;
|
|
||||||
m_LogStream = logStream;
|
m_LogStream = logStream;
|
||||||
}
|
}
|
||||||
|
|
13
Log.h
13
Log.h
|
@ -7,6 +7,7 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <memory>
|
||||||
#include "Queue.h"
|
#include "Queue.h"
|
||||||
|
|
||||||
enum LogLevel
|
enum LogLevel
|
||||||
|
@ -34,13 +35,13 @@ class Log: public i2p::util::MsgQueue<LogMsg>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Log (): m_LogStream (nullptr) { SetOnEmpty (std::bind (&Log::Flush, this)); };
|
Log () { SetOnEmpty (std::bind (&Log::Flush, this)); };
|
||||||
~Log () { delete m_LogStream; };
|
~Log () {};
|
||||||
|
|
||||||
void SetLogFile (const std::string& fullFilePath);
|
void SetLogFile (const std::string& fullFilePath);
|
||||||
void SetLogLevel (const std::string& level);
|
void SetLogLevel (const std::string& level);
|
||||||
void SetLogStream (std::ostream * logStream);
|
void SetLogStream (std::shared_ptr<std::ostream> logStream);
|
||||||
std::ostream * GetLogStream () const { return m_LogStream; };
|
std::shared_ptr<std::ostream> GetLogStream () const { return m_LogStream; };
|
||||||
const std::string& GetTimestamp ();
|
const std::string& GetTimestamp ();
|
||||||
LogLevel GetLogLevel () { return m_MinLevel; };
|
LogLevel GetLogLevel () { return m_MinLevel; };
|
||||||
|
|
||||||
|
@ -50,7 +51,7 @@ class Log: public i2p::util::MsgQueue<LogMsg>
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::ostream * m_LogStream;
|
std::shared_ptr<std::ostream> m_LogStream;
|
||||||
enum LogLevel m_MinLevel;
|
enum LogLevel m_MinLevel;
|
||||||
std::string m_Timestamp;
|
std::string m_Timestamp;
|
||||||
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6) && !defined(__clang__) // gcc 4.6
|
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6) && !defined(__clang__) // gcc 4.6
|
||||||
|
@ -73,7 +74,7 @@ inline void StartLog (const std::string& fullFilePath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void StartLog (std::ostream * s)
|
inline void StartLog (std::shared_ptr<std::ostream> s)
|
||||||
{
|
{
|
||||||
if (!g_Log)
|
if (!g_Log)
|
||||||
{
|
{
|
||||||
|
|
2
api.cpp
2
api.cpp
|
@ -31,7 +31,7 @@ namespace api
|
||||||
i2p::crypto::TerminateCrypto ();
|
i2p::crypto::TerminateCrypto ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void StartI2P (std::ostream * logStream)
|
void StartI2P (std::shared_ptr<std::ostream> logStream)
|
||||||
{
|
{
|
||||||
if (logStream)
|
if (logStream)
|
||||||
StartLog (logStream);
|
StartLog (logStream);
|
||||||
|
|
2
api.h
2
api.h
|
@ -14,7 +14,7 @@ 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 TerminateI2P ();
|
void TerminateI2P ();
|
||||||
void StartI2P (std::ostream * logStream = nullptr);
|
void StartI2P (std::shared_ptr<std::ostream> logStream = nullptr);
|
||||||
// write system log to logStream, if not specified to <appName>.log in application's folder
|
// write system log to logStream, if not specified to <appName>.log in application's folder
|
||||||
void StopI2P ();
|
void StopI2P ();
|
||||||
void RunPeerTest (); // should be called after UPnP
|
void RunPeerTest (); // should be called after UPnP
|
||||||
|
|
Loading…
Reference in a new issue