mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
2.2 KiB
130 lines
2.2 KiB
11 years ago
|
#ifndef LOG_H__
|
||
|
#define LOG_H__
|
||
|
|
||
11 years ago
|
#include <string>
|
||
11 years ago
|
#include <iostream>
|
||
|
#include <sstream>
|
||
11 years ago
|
#include <fstream>
|
||
11 years ago
|
#include <functional>
|
||
10 years ago
|
#include <chrono>
|
||
10 years ago
|
#include "Queue.h"
|
||
11 years ago
|
|
||
10 years ago
|
enum LogLevel
|
||
|
{
|
||
|
eLogError = 0,
|
||
|
eLogWarning,
|
||
|
eLogInfo,
|
||
10 years ago
|
eLogDebug,
|
||
10 years ago
|
eNumLogLevels
|
||
|
};
|
||
|
|
||
10 years ago
|
class Log;
|
||
11 years ago
|
struct LogMsg
|
||
|
{
|
||
|
std::stringstream s;
|
||
10 years ago
|
Log * log;
|
||
10 years ago
|
LogLevel level;
|
||
11 years ago
|
|
||
10 years ago
|
LogMsg (Log * l = nullptr, LogLevel lv = eLogInfo): log (l), level (lv) {};
|
||
10 years ago
|
|
||
11 years ago
|
void Process();
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
class Log: public i2p::util::MsgQueue<LogMsg>
|
||
|
{
|
||
10 years ago
|
public:
|
||
11 years ago
|
|
||
10 years ago
|
Log (): m_LogStream (nullptr) { SetOnEmpty (std::bind (&Log::Flush, this)); };
|
||
|
~Log () { delete m_LogStream; };
|
||
11 years ago
|
|
||
|
void SetLogFile (const std::string& fullFilePath);
|
||
10 years ago
|
void SetLogStream (std::ostream * logStream);
|
||
10 years ago
|
std::ostream * GetLogStream () const { return m_LogStream; };
|
||
10 years ago
|
const std::string& GetTimestamp ();
|
||
11 years ago
|
|
||
10 years ago
|
private:
|
||
11 years ago
|
|
||
10 years ago
|
void Flush ();
|
||
11 years ago
|
|
||
|
private:
|
||
10 years ago
|
|
||
10 years ago
|
std::ostream * m_LogStream;
|
||
10 years ago
|
std::string m_Timestamp;
|
||
10 years ago
|
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6) && !defined(__clang__)
|
||
10 years ago
|
std::chrono::monotonic_clock::time_point m_LastTimestampUpdate;
|
||
10 years ago
|
#else
|
||
|
std::chrono::steady_clock::time_point m_LastTimestampUpdate;
|
||
|
#endif
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
extern Log * g_Log;
|
||
|
|
||
|
inline void StartLog (const std::string& fullFilePath)
|
||
|
{
|
||
|
if (!g_Log)
|
||
10 years ago
|
{
|
||
10 years ago
|
auto log = new Log ();
|
||
11 years ago
|
if (fullFilePath.length () > 0)
|
||
10 years ago
|
log->SetLogFile (fullFilePath);
|
||
|
g_Log = log;
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
inline void StartLog (std::ostream * s)
|
||
|
{
|
||
|
if (!g_Log)
|
||
10 years ago
|
{
|
||
10 years ago
|
auto log = new Log ();
|
||
10 years ago
|
if (s)
|
||
10 years ago
|
log->SetLogStream (s);
|
||
|
g_Log = log;
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
inline void StopLog ()
|
||
|
{
|
||
|
if (g_Log)
|
||
|
{
|
||
10 years ago
|
auto log = g_Log;
|
||
11 years ago
|
g_Log = nullptr;
|
||
10 years ago
|
log->Stop ();
|
||
|
delete log;
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
template<typename TValue>
|
||
10 years ago
|
void LogPrint (std::stringstream& s, TValue arg)
|
||
11 years ago
|
{
|
||
10 years ago
|
s << arg;
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
11 years ago
|
template<typename TValue, typename... TArgs>
|
||
10 years ago
|
void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
|
||
11 years ago
|
{
|
||
10 years ago
|
LogPrint (s, arg);
|
||
|
LogPrint (s, args...);
|
||
11 years ago
|
}
|
||
|
|
||
|
template<typename... TArgs>
|
||
10 years ago
|
void LogPrint (LogLevel level, TArgs... args)
|
||
11 years ago
|
{
|
||
10 years ago
|
LogMsg * msg = new LogMsg (g_Log, level);
|
||
11 years ago
|
LogPrint (msg->s, args...);
|
||
|
msg->s << std::endl;
|
||
10 years ago
|
if (g_Log)
|
||
11 years ago
|
g_Log->Put (msg);
|
||
|
else
|
||
|
{
|
||
|
msg->Process ();
|
||
|
delete msg;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
template<typename... TArgs>
|
||
10 years ago
|
void LogPrint (TArgs... args)
|
||
10 years ago
|
{
|
||
|
LogPrint (eLogInfo, args...);
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
|
#endif
|