2013-12-10 13:00:13 +00:00
|
|
|
#ifndef LOG_H__
|
|
|
|
#define LOG_H__
|
|
|
|
|
2014-04-24 15:10:46 +00:00
|
|
|
#include <string>
|
2013-12-10 13:00:13 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2014-04-24 15:10:46 +00:00
|
|
|
#include <fstream>
|
2014-04-23 16:49:02 +00:00
|
|
|
#include <functional>
|
2015-05-06 15:24:35 +00:00
|
|
|
#include <chrono>
|
2016-02-04 17:36:58 +00:00
|
|
|
#include <memory>
|
2013-12-10 13:00:13 +00:00
|
|
|
#include "Queue.h"
|
|
|
|
|
2016-03-26 13:49:45 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
|
|
|
|
2014-10-28 20:36:17 +00:00
|
|
|
enum LogLevel
|
|
|
|
{
|
|
|
|
eLogError = 0,
|
|
|
|
eLogWarning,
|
|
|
|
eLogInfo,
|
|
|
|
eLogDebug,
|
|
|
|
eNumLogLevels
|
|
|
|
};
|
|
|
|
|
2015-05-06 15:24:35 +00:00
|
|
|
class Log;
|
2013-12-10 13:00:13 +00:00
|
|
|
struct LogMsg
|
|
|
|
{
|
|
|
|
std::stringstream s;
|
2015-05-06 15:24:35 +00:00
|
|
|
Log * log;
|
2014-10-28 20:36:17 +00:00
|
|
|
LogLevel level;
|
2013-12-10 13:00:13 +00:00
|
|
|
|
2015-05-06 15:24:35 +00:00
|
|
|
LogMsg (Log * l = nullptr, LogLevel lv = eLogInfo): log (l), level (lv) {};
|
2013-12-10 13:00:13 +00:00
|
|
|
|
2014-04-22 22:10:21 +00:00
|
|
|
void Process();
|
2013-12-10 13:00:13 +00:00
|
|
|
};
|
|
|
|
|
2014-04-23 16:49:02 +00:00
|
|
|
class Log: public i2p::util::MsgQueue<LogMsg>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-02-04 17:36:58 +00:00
|
|
|
Log () { SetOnEmpty (std::bind (&Log::Flush, this)); };
|
|
|
|
~Log () {};
|
2014-04-24 15:10:46 +00:00
|
|
|
|
2016-02-29 16:02:55 +00:00
|
|
|
void SetLogFile (const std::string& fullFilePath, bool truncate = true);
|
2016-02-04 18:53:38 +00:00
|
|
|
void ReopenLogFile ();
|
2015-12-28 00:00:00 +00:00
|
|
|
void SetLogLevel (const std::string& level);
|
2016-02-04 17:36:58 +00:00
|
|
|
void SetLogStream (std::shared_ptr<std::ostream> logStream);
|
|
|
|
std::shared_ptr<std::ostream> GetLogStream () const { return m_LogStream; };
|
2015-05-06 15:24:35 +00:00
|
|
|
const std::string& GetTimestamp ();
|
2015-12-28 00:00:00 +00:00
|
|
|
LogLevel GetLogLevel () { return m_MinLevel; };
|
2016-03-26 13:40:19 +00:00
|
|
|
const std::string& GetFullFilePath () const { return m_FullFilePath; };
|
|
|
|
/** start logging to syslog */
|
2016-03-26 13:49:45 +00:00
|
|
|
void StartSyslog(const std::string & ident, const int facility);
|
2016-03-26 13:40:19 +00:00
|
|
|
/** stop logging to syslog */
|
|
|
|
void StopSyslog();
|
|
|
|
/** are we logging to syslog right now? */
|
|
|
|
bool SyslogEnabled();
|
2014-04-23 16:49:02 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
void Flush ();
|
2014-04-24 15:10:46 +00:00
|
|
|
|
|
|
|
private:
|
2016-03-26 13:40:19 +00:00
|
|
|
|
2016-02-04 18:53:38 +00:00
|
|
|
std::string m_FullFilePath; // empty if stream
|
2016-02-04 17:36:58 +00:00
|
|
|
std::shared_ptr<std::ostream> m_LogStream;
|
2015-12-28 00:00:00 +00:00
|
|
|
enum LogLevel m_MinLevel;
|
2015-05-06 15:24:35 +00:00
|
|
|
std::string m_Timestamp;
|
2015-11-20 15:02:54 +00:00
|
|
|
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6) && !defined(__clang__) // gcc 4.6
|
2015-05-06 23:18:00 +00:00
|
|
|
std::chrono::monotonic_clock::time_point m_LastTimestampUpdate;
|
|
|
|
#else
|
|
|
|
std::chrono::steady_clock::time_point m_LastTimestampUpdate;
|
2016-03-26 13:40:19 +00:00
|
|
|
#endif
|
|
|
|
std::string m_Ident;
|
2014-04-23 16:49:02 +00:00
|
|
|
};
|
|
|
|
|
2014-07-02 17:48:45 +00:00
|
|
|
extern Log * g_Log;
|
|
|
|
|
|
|
|
inline void StartLog (const std::string& fullFilePath)
|
|
|
|
{
|
|
|
|
if (!g_Log)
|
|
|
|
{
|
2015-05-09 01:42:28 +00:00
|
|
|
auto log = new Log ();
|
2014-07-02 18:25:57 +00:00
|
|
|
if (fullFilePath.length () > 0)
|
2015-05-09 01:42:28 +00:00
|
|
|
log->SetLogFile (fullFilePath);
|
|
|
|
g_Log = log;
|
2014-07-02 17:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 17:36:58 +00:00
|
|
|
inline void StartLog (std::shared_ptr<std::ostream> s)
|
2014-12-08 21:27:10 +00:00
|
|
|
{
|
|
|
|
if (!g_Log)
|
|
|
|
{
|
2015-05-09 01:42:28 +00:00
|
|
|
auto log = new Log ();
|
2014-12-08 21:27:10 +00:00
|
|
|
if (s)
|
2015-05-09 01:42:28 +00:00
|
|
|
log->SetLogStream (s);
|
|
|
|
g_Log = log;
|
2014-12-08 21:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 17:48:45 +00:00
|
|
|
inline void StopLog ()
|
|
|
|
{
|
|
|
|
if (g_Log)
|
|
|
|
{
|
2015-05-09 01:42:28 +00:00
|
|
|
auto log = g_Log;
|
2014-07-02 17:48:45 +00:00
|
|
|
g_Log = nullptr;
|
2015-05-09 01:42:28 +00:00
|
|
|
log->Stop ();
|
|
|
|
delete log;
|
2014-07-02 17:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-10 13:00:13 +00:00
|
|
|
|
2016-02-02 17:16:29 +00:00
|
|
|
inline void SetLogLevel (const std::string& level)
|
|
|
|
{
|
|
|
|
if (g_Log)
|
|
|
|
g_Log->SetLogLevel(level);
|
|
|
|
}
|
|
|
|
|
2016-02-04 18:53:38 +00:00
|
|
|
inline void ReopenLogFile ()
|
|
|
|
{
|
|
|
|
if (g_Log)
|
|
|
|
g_Log->ReopenLogFile ();
|
|
|
|
}
|
|
|
|
|
2016-03-06 02:46:01 +00:00
|
|
|
inline bool IsLogToFile ()
|
|
|
|
{
|
|
|
|
return g_Log ? !g_Log->GetFullFilePath ().empty () : false;
|
|
|
|
}
|
|
|
|
|
2016-03-26 13:40:19 +00:00
|
|
|
inline void StartSyslog()
|
|
|
|
{
|
|
|
|
StartLog("");
|
2016-03-26 13:49:45 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
g_Log->StartSyslog("i2pd", LOG_USER);
|
|
|
|
#endif
|
2016-03-26 13:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void StopSyslog()
|
|
|
|
{
|
|
|
|
if(g_Log)
|
|
|
|
g_Log->StopSyslog();
|
|
|
|
}
|
|
|
|
|
2013-12-10 13:00:13 +00:00
|
|
|
template<typename TValue>
|
|
|
|
void LogPrint (std::stringstream& s, TValue arg)
|
|
|
|
{
|
|
|
|
s << arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename TValue, typename... TArgs>
|
|
|
|
void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
|
|
|
|
{
|
|
|
|
LogPrint (s, arg);
|
|
|
|
LogPrint (s, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... TArgs>
|
2014-10-28 20:36:17 +00:00
|
|
|
void LogPrint (LogLevel level, TArgs... args)
|
2013-12-10 13:00:13 +00:00
|
|
|
{
|
2015-12-28 00:00:00 +00:00
|
|
|
if (g_Log && level > g_Log->GetLogLevel ())
|
|
|
|
return;
|
2015-05-06 15:24:35 +00:00
|
|
|
LogMsg * msg = new LogMsg (g_Log, level);
|
2013-12-10 13:00:13 +00:00
|
|
|
LogPrint (msg->s, args...);
|
|
|
|
msg->s << std::endl;
|
2015-12-28 00:00:00 +00:00
|
|
|
if (g_Log) {
|
2014-07-02 17:48:45 +00:00
|
|
|
g_Log->Put (msg);
|
2015-12-28 00:00:00 +00:00
|
|
|
} else {
|
2014-07-02 17:48:45 +00:00
|
|
|
msg->Process ();
|
|
|
|
delete msg;
|
|
|
|
}
|
2014-10-28 20:36:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 13:00:13 +00:00
|
|
|
#endif
|