i2pd/libi2pd/Log.h

187 lines
4.0 KiB
C
Raw Normal View History

2016-03-27 03:00:00 +03:00
/*
* Copyright (c) 2013-2016, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2013-12-10 17:00:13 +04:00
#ifndef LOG_H__
#define LOG_H__
2016-03-27 03:00:00 +03:00
#include <ctime>
2014-04-24 19:10:46 +04:00
#include <string>
2013-12-10 17:00:13 +04:00
#include <iostream>
2014-04-24 19:10:46 +04:00
#include <fstream>
2016-03-27 03:00:00 +03:00
#include <sstream>
#include <chrono>
2016-02-04 20:36:58 +03:00
#include <memory>
#include <thread>
2013-12-10 17:00:13 +04:00
#include "Queue.h"
2016-03-26 16:49:45 +03:00
#ifndef _WIN32
#include <syslog.h>
#endif
2014-10-28 23:36:17 +03:00
enum LogLevel
{
eLogError = 0,
eLogWarning,
eLogInfo,
eLogDebug,
eNumLogLevels
};
2016-03-27 03:00:00 +03:00
enum LogType {
eLogStdout = 0,
eLogStream,
eLogFile,
#ifndef _WIN32
eLogSyslog,
2016-03-26 16:40:19 +03:00
#endif
2014-04-23 20:49:02 +04:00
};
2016-03-27 03:00:00 +03:00
namespace i2p {
namespace log {
2016-07-28 16:49:43 +03:00
2016-03-27 03:00:00 +03:00
struct LogMsg; /* forward declaration */
2014-07-02 21:48:45 +04:00
2016-03-27 03:00:00 +03:00
class Log
2014-07-02 21:48:45 +04:00
{
2016-03-27 03:00:00 +03:00
private:
enum LogType m_Destination;
enum LogLevel m_MinLevel;
std::shared_ptr<std::ostream> m_LogStream;
std::string m_Logfile;
std::time_t m_LastTimestamp;
char m_LastDateTime[64];
i2p::util::Queue<std::shared_ptr<LogMsg> > m_Queue;
bool m_HasColors;
volatile bool m_IsRunning;
std::thread * m_Thread;
2016-03-27 03:00:00 +03:00
private:
/** prevent making copies */
Log (const Log &);
const Log& operator=(const Log&);
void Run ();
void Process (std::shared_ptr<LogMsg> msg);
2016-03-27 03:00:00 +03:00
/**
* @brief Makes formatted string from unix timestamp
* @param ts Second since epoch
*
* This function internally caches the result for last provided value
*/
const char * TimeAsString(std::time_t ts);
public:
Log ();
~Log ();
LogType GetLogType () { return m_Destination; };
LogLevel GetLogLevel () { return m_MinLevel; };
void Start ();
void Stop ();
2016-03-27 03:00:00 +03:00
/**
2016-05-31 03:00:00 +03:00
* @brief Sets minimal allowed level for log messages
2016-03-27 03:00:00 +03:00
* @param level String with wanted minimal msg level
*/
void SetLogLevel (const std::string& level);
/**
* @brief Sets log destination to logfile
* @param path Path to logfile
*/
void SendTo (const std::string &path);
/**
* @brief Sets log destination to given output stream
* @param os Output stream
*/
2016-05-31 03:00:00 +03:00
void SendTo (std::shared_ptr<std::ostream> os);
2016-03-27 03:00:00 +03:00
#ifndef _WIN32
/**
* @brief Sets log destination to syslog
* @param name Wanted program name
* @param facility Wanted log category
*/
void SendTo (const char *name, int facility);
#endif
/**
* @brief Format log message and write to output stream/syslog
* @param msg Pointer to processed message
*/
void Append(std::shared_ptr<i2p::log::LogMsg> &);
/** @brief Reopen log file */
void Reopen();
};
/**
2016-05-31 03:00:00 +03:00
* @struct LogMsg
* @brief Log message container
2016-03-27 03:00:00 +03:00
*
* We creating it somewhere with LogPrint(),
* then put in MsgQueue for later processing.
*/
struct LogMsg {
std::time_t timestamp;
std::string text; /**< message text as single string */
LogLevel level; /**< message level */
2016-03-28 17:00:00 +03:00
std::thread::id tid; /**< id of thread that generated message */
2016-10-20 16:12:15 +03:00
LogMsg (LogLevel lvl, std::time_t ts, const std::string & txt): timestamp(ts), text(txt), level(lvl) {};
2016-03-27 03:00:00 +03:00
};
Log & Logger();
} // log
2016-10-20 16:12:15 +03:00
}
2016-03-27 03:00:00 +03:00
/** internal usage only -- folding args array to single string */
2013-12-10 17:00:13 +04:00
template<typename TValue>
2016-11-09 18:16:37 +03:00
void LogPrint (std::stringstream& s, TValue&& arg) noexcept
2013-12-10 17:00:13 +04:00
{
s << std::forward<TValue>(arg);
2013-12-10 17:00:13 +04:00
}
2016-03-27 03:00:00 +03:00
/** internal usage only -- folding args array to single string */
2013-12-10 17:00:13 +04:00
template<typename TValue, typename... TArgs>
2016-11-09 18:16:37 +03:00
void LogPrint (std::stringstream& s, TValue&& arg, TArgs&&... args) noexcept
2013-12-10 17:00:13 +04:00
{
LogPrint (s, std::forward<TValue>(arg));
LogPrint (s, std::forward<TArgs>(args)...);
2013-12-10 17:00:13 +04:00
}
2016-03-27 03:00:00 +03:00
/**
* @brief Create log message and send it to queue
* @param level Message level (eLogError, eLogInfo, ...)
* @param args Array of message parts
*/
2013-12-10 17:00:13 +04:00
template<typename... TArgs>
2016-11-09 18:16:37 +03:00
void LogPrint (LogLevel level, TArgs&&... args) noexcept
2013-12-10 17:00:13 +04:00
{
2016-03-27 03:00:00 +03:00
i2p::log::Log &log = i2p::log::Logger();
if (level > log.GetLogLevel ())
2015-12-28 03:00:00 +03:00
return;
2016-03-27 03:00:00 +03:00
// fold message to single string
std::stringstream ss("");
LogPrint (ss, std::forward<TArgs>(args)...);
2016-07-28 16:49:43 +03:00
2016-03-27 03:00:00 +03:00
auto msg = std::make_shared<i2p::log::LogMsg>(level, std::time(nullptr), ss.str());
2016-03-28 17:00:00 +03:00
msg->tid = std::this_thread::get_id();
2016-03-27 03:00:00 +03:00
log.Append(msg);
2014-10-28 23:36:17 +03:00
}
2016-03-27 03:00:00 +03:00
#endif // LOG_H__