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.
187 lines
4.0 KiB
187 lines
4.0 KiB
9 years ago
|
/*
|
||
|
* 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
|
||
|
*/
|
||
|
|
||
11 years ago
|
#ifndef LOG_H__
|
||
|
#define LOG_H__
|
||
|
|
||
9 years ago
|
#include <ctime>
|
||
11 years ago
|
#include <string>
|
||
11 years ago
|
#include <iostream>
|
||
11 years ago
|
#include <fstream>
|
||
9 years ago
|
#include <sstream>
|
||
10 years ago
|
#include <chrono>
|
||
9 years ago
|
#include <memory>
|
||
8 years ago
|
#include <thread>
|
||
11 years ago
|
#include "Queue.h"
|
||
|
|
||
9 years ago
|
#ifndef _WIN32
|
||
|
#include <syslog.h>
|
||
|
#endif
|
||
|
|
||
10 years ago
|
enum LogLevel
|
||
|
{
|
||
|
eLogError = 0,
|
||
|
eLogWarning,
|
||
|
eLogInfo,
|
||
|
eLogDebug,
|
||
|
eNumLogLevels
|
||
|
};
|
||
|
|
||
9 years ago
|
enum LogType {
|
||
|
eLogStdout = 0,
|
||
|
eLogStream,
|
||
|
eLogFile,
|
||
|
#ifndef _WIN32
|
||
|
eLogSyslog,
|
||
9 years ago
|
#endif
|
||
11 years ago
|
};
|
||
|
|
||
9 years ago
|
namespace i2p {
|
||
|
namespace log {
|
||
9 years ago
|
|
||
9 years ago
|
struct LogMsg; /* forward declaration */
|
||
11 years ago
|
|
||
9 years ago
|
class Log
|
||
11 years ago
|
{
|
||
9 years ago
|
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;
|
||
8 years ago
|
bool m_HasColors;
|
||
8 years ago
|
volatile bool m_IsRunning;
|
||
|
std::thread * m_Thread;
|
||
9 years ago
|
|
||
|
private:
|
||
|
|
||
|
/** prevent making copies */
|
||
|
Log (const Log &);
|
||
|
const Log& operator=(const Log&);
|
||
|
|
||
8 years ago
|
void Run ();
|
||
|
void Process (std::shared_ptr<LogMsg> msg);
|
||
9 years ago
|
|
||
|
/**
|
||
|
* @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; };
|
||
|
|
||
8 years ago
|
void Start ();
|
||
|
void Stop ();
|
||
|
|
||
9 years ago
|
/**
|
||
9 years ago
|
* @brief Sets minimal allowed level for log messages
|
||
9 years ago
|
* @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
|
||
|
*/
|
||
9 years ago
|
void SendTo (std::shared_ptr<std::ostream> os);
|
||
9 years ago
|
|
||
|
#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();
|
||
|
};
|
||
|
|
||
|
/**
|
||
9 years ago
|
* @struct LogMsg
|
||
|
* @brief Log message container
|
||
9 years ago
|
*
|
||
|
* 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 */
|
||
9 years ago
|
std::thread::id tid; /**< id of thread that generated message */
|
||
8 years ago
|
|
||
9 years ago
|
LogMsg (LogLevel lvl, std::time_t ts, const std::string & txt): timestamp(ts), text(txt), level(lvl) {};
|
||
9 years ago
|
};
|
||
|
|
||
|
Log & Logger();
|
||
|
} // log
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
|
/** internal usage only -- folding args array to single string */
|
||
11 years ago
|
template<typename TValue>
|
||
8 years ago
|
void LogPrint (std::stringstream& s, TValue&& arg) noexcept
|
||
11 years ago
|
{
|
||
8 years ago
|
s << std::forward<TValue>(arg);
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
|
/** internal usage only -- folding args array to single string */
|
||
11 years ago
|
template<typename TValue, typename... TArgs>
|
||
8 years ago
|
void LogPrint (std::stringstream& s, TValue&& arg, TArgs&&... args) noexcept
|
||
11 years ago
|
{
|
||
8 years ago
|
LogPrint (s, std::forward<TValue>(arg));
|
||
|
LogPrint (s, std::forward<TArgs>(args)...);
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
/**
|
||
|
* @brief Create log message and send it to queue
|
||
|
* @param level Message level (eLogError, eLogInfo, ...)
|
||
|
* @param args Array of message parts
|
||
|
*/
|
||
11 years ago
|
template<typename... TArgs>
|
||
8 years ago
|
void LogPrint (LogLevel level, TArgs&&... args) noexcept
|
||
11 years ago
|
{
|
||
9 years ago
|
i2p::log::Log &log = i2p::log::Logger();
|
||
|
if (level > log.GetLogLevel ())
|
||
9 years ago
|
return;
|
||
9 years ago
|
|
||
|
// fold message to single string
|
||
|
std::stringstream ss("");
|
||
|
|
||
8 years ago
|
LogPrint (ss, std::forward<TArgs>(args)...);
|
||
9 years ago
|
|
||
9 years ago
|
auto msg = std::make_shared<i2p::log::LogMsg>(level, std::time(nullptr), ss.str());
|
||
9 years ago
|
msg->tid = std::this_thread::get_id();
|
||
9 years ago
|
log.Append(msg);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
#endif // LOG_H__
|