2013-12-10 17:00:13 +04:00
|
|
|
#ifndef LOG_H__
|
|
|
|
#define LOG_H__
|
|
|
|
|
2014-04-24 19:10:46 +04:00
|
|
|
#include <string>
|
2013-12-10 17:00:13 +04:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2014-04-24 19:10:46 +04:00
|
|
|
#include <fstream>
|
2014-04-23 20:49:02 +04:00
|
|
|
#include <functional>
|
2015-05-06 18:24:35 +03:00
|
|
|
#include <chrono>
|
2013-12-10 17:00:13 +04:00
|
|
|
#include "Queue.h"
|
|
|
|
|
2014-10-28 23:36:17 +03:00
|
|
|
enum LogLevel
|
|
|
|
{
|
|
|
|
eLogError = 0,
|
|
|
|
eLogWarning,
|
|
|
|
eLogInfo,
|
2015-07-20 19:08:55 +03:00
|
|
|
eLogDebug,
|
2014-10-28 23:36:17 +03:00
|
|
|
eNumLogLevels
|
|
|
|
};
|
|
|
|
|
2015-05-06 18:24:35 +03:00
|
|
|
class Log;
|
2013-12-10 17:00:13 +04:00
|
|
|
struct LogMsg
|
|
|
|
{
|
|
|
|
std::stringstream s;
|
2015-07-20 19:08:55 +03:00
|
|
|
Log * log;
|
2014-10-28 23:36:17 +03:00
|
|
|
LogLevel level;
|
2013-12-10 17:00:13 +04:00
|
|
|
|
2015-05-06 18:24:35 +03:00
|
|
|
LogMsg (Log * l = nullptr, LogLevel lv = eLogInfo): log (l), level (lv) {};
|
2015-07-20 19:08:55 +03:00
|
|
|
|
2014-04-23 02:10:21 +04:00
|
|
|
void Process();
|
2013-12-10 17:00:13 +04:00
|
|
|
};
|
|
|
|
|
2014-04-23 20:49:02 +04:00
|
|
|
class Log: public i2p::util::MsgQueue<LogMsg>
|
|
|
|
{
|
2015-07-17 00:29:52 +03:00
|
|
|
public:
|
2014-04-23 20:49:02 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
Log (): m_LogStream (nullptr) { SetOnEmpty (std::bind (&Log::Flush, this)); };
|
|
|
|
~Log () { delete m_LogStream; };
|
2014-04-24 19:10:46 +04:00
|
|
|
|
|
|
|
void SetLogFile (const std::string& fullFilePath);
|
2014-12-09 00:27:10 +03:00
|
|
|
void SetLogStream (std::ostream * logStream);
|
2015-07-20 19:08:55 +03:00
|
|
|
std::ostream * GetLogStream () const { return m_LogStream; };
|
2015-05-06 18:24:35 +03:00
|
|
|
const std::string& GetTimestamp ();
|
2014-04-23 20:49:02 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
private:
|
2014-04-23 20:49:02 +04:00
|
|
|
|
2015-07-17 00:29:52 +03:00
|
|
|
void Flush ();
|
2014-04-24 19:10:46 +04:00
|
|
|
|
|
|
|
private:
|
2015-07-20 19:08:55 +03:00
|
|
|
|
2014-12-09 00:27:10 +03:00
|
|
|
std::ostream * m_LogStream;
|
2015-05-06 18:24:35 +03:00
|
|
|
std::string m_Timestamp;
|
2015-07-20 19:08:55 +03:00
|
|
|
#if !defined(__APPLE__)
|
2015-05-07 02:18:00 +03:00
|
|
|
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6) // gcc 4.6
|
|
|
|
std::chrono::monotonic_clock::time_point m_LastTimestampUpdate;
|
2015-07-20 19:08:55 +03:00
|
|
|
#else
|
|
|
|
std::chrono::steady_clock::time_point m_LastTimestampUpdate;
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
std::chrono::steady_clock::time_point m_LastTimestampUpdate;
|
|
|
|
#endif
|
2014-04-23 20:49:02 +04:00
|
|
|
};
|
|
|
|
|
2014-07-02 21:48:45 +04:00
|
|
|
extern Log * g_Log;
|
|
|
|
|
|
|
|
inline void StartLog (const std::string& fullFilePath)
|
|
|
|
{
|
|
|
|
if (!g_Log)
|
2015-07-20 19:08:55 +03:00
|
|
|
{
|
2015-05-09 04:42:28 +03:00
|
|
|
auto log = new Log ();
|
2014-07-02 22:25:57 +04:00
|
|
|
if (fullFilePath.length () > 0)
|
2015-05-09 04:42:28 +03:00
|
|
|
log->SetLogFile (fullFilePath);
|
|
|
|
g_Log = log;
|
2015-07-20 19:08:55 +03:00
|
|
|
}
|
2014-07-02 21:48:45 +04:00
|
|
|
}
|
|
|
|
|
2014-12-09 00:27:10 +03:00
|
|
|
inline void StartLog (std::ostream * s)
|
|
|
|
{
|
|
|
|
if (!g_Log)
|
2015-07-20 19:08:55 +03:00
|
|
|
{
|
2015-05-09 04:42:28 +03:00
|
|
|
auto log = new Log ();
|
2014-12-09 00:27:10 +03:00
|
|
|
if (s)
|
2015-05-09 04:42:28 +03:00
|
|
|
log->SetLogStream (s);
|
|
|
|
g_Log = log;
|
2015-07-20 19:08:55 +03:00
|
|
|
}
|
2014-12-09 00:27:10 +03:00
|
|
|
}
|
|
|
|
|
2014-07-02 21:48:45 +04:00
|
|
|
inline void StopLog ()
|
|
|
|
{
|
|
|
|
if (g_Log)
|
|
|
|
{
|
2015-05-09 04:42:28 +03:00
|
|
|
auto log = g_Log;
|
2014-07-02 21:48:45 +04:00
|
|
|
g_Log = nullptr;
|
2015-05-09 04:42:28 +03:00
|
|
|
log->Stop ();
|
|
|
|
delete log;
|
2015-07-20 19:08:55 +03:00
|
|
|
}
|
2014-07-02 21:48:45 +04:00
|
|
|
}
|
2013-12-10 17:00:13 +04:00
|
|
|
|
|
|
|
template<typename TValue>
|
2015-07-20 19:08:55 +03:00
|
|
|
void LogPrint (std::stringstream& s, TValue arg)
|
2013-12-10 17:00:13 +04:00
|
|
|
{
|
2015-07-17 00:29:52 +03:00
|
|
|
s << arg;
|
2013-12-10 17:00:13 +04:00
|
|
|
}
|
2015-07-20 19:08:55 +03:00
|
|
|
|
2013-12-10 17:00:13 +04:00
|
|
|
template<typename TValue, typename... TArgs>
|
2015-07-20 19:08:55 +03:00
|
|
|
void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
|
2013-12-10 17:00:13 +04:00
|
|
|
{
|
2015-07-17 00:29:52 +03:00
|
|
|
LogPrint (s, arg);
|
|
|
|
LogPrint (s, args...);
|
2013-12-10 17:00:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... TArgs>
|
2015-07-20 19:08:55 +03:00
|
|
|
void LogPrint (LogLevel level, TArgs... args)
|
2013-12-10 17:00:13 +04:00
|
|
|
{
|
2015-05-06 18:24:35 +03:00
|
|
|
LogMsg * msg = new LogMsg (g_Log, level);
|
2013-12-10 17:00:13 +04:00
|
|
|
LogPrint (msg->s, args...);
|
|
|
|
msg->s << std::endl;
|
2015-07-20 19:08:55 +03:00
|
|
|
if (g_Log)
|
2014-07-02 21:48:45 +04:00
|
|
|
g_Log->Put (msg);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg->Process ();
|
|
|
|
delete msg;
|
|
|
|
}
|
2014-10-28 23:36:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... TArgs>
|
2015-07-20 19:08:55 +03:00
|
|
|
void LogPrint (TArgs... args)
|
2014-10-28 23:36:17 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, args...);
|
2015-07-20 19:08:55 +03:00
|
|
|
}
|
2013-12-10 17:00:13 +04:00
|
|
|
|
|
|
|
#endif
|