2018-05-27 14:04:30 +00:00
|
|
|
#ifndef LLARP_LOGGER_HPP
|
|
|
|
#define LLARP_LOGGER_HPP
|
|
|
|
|
2018-07-24 06:15:29 +00:00
|
|
|
#include <llarp/time.h>
|
2018-05-28 13:49:44 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <iomanip>
|
2018-05-27 14:04:30 +00:00
|
|
|
#include <iostream>
|
2018-07-25 00:35:11 +00:00
|
|
|
#include <llarp/threading.hpp>
|
2018-05-27 14:04:30 +00:00
|
|
|
#include <sstream>
|
2018-06-01 17:47:37 +00:00
|
|
|
#include <string>
|
2018-05-27 14:04:30 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-06-23 14:52:15 +00:00
|
|
|
// probably will need to move out of llarp namespace for c api
|
2018-05-27 14:04:30 +00:00
|
|
|
enum LogLevel
|
|
|
|
{
|
|
|
|
eLogDebug,
|
|
|
|
eLogInfo,
|
|
|
|
eLogWarn,
|
|
|
|
eLogError
|
|
|
|
};
|
|
|
|
|
2018-05-28 13:49:44 +00:00
|
|
|
struct Logger
|
|
|
|
{
|
2018-06-06 21:23:57 +00:00
|
|
|
LogLevel minlevel = eLogInfo;
|
2018-07-24 06:20:05 +00:00
|
|
|
std::ostream& out;
|
2018-07-18 20:58:16 +00:00
|
|
|
std::mutex access;
|
2018-07-24 06:20:05 +00:00
|
|
|
Logger() : Logger(std::cout)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
Logger(std::ostream& o) : out(o)
|
|
|
|
{
|
|
|
|
}
|
2018-05-28 13:49:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern Logger _glog;
|
|
|
|
|
|
|
|
void
|
|
|
|
SetLogLevel(LogLevel lvl);
|
2018-05-27 14:04:30 +00:00
|
|
|
|
|
|
|
/** internal */
|
|
|
|
template < typename TArg >
|
|
|
|
void
|
2018-06-01 17:47:37 +00:00
|
|
|
LogAppend(std::stringstream& ss, TArg&& arg) noexcept
|
2018-05-27 14:04:30 +00:00
|
|
|
{
|
|
|
|
ss << std::forward< TArg >(arg);
|
|
|
|
}
|
|
|
|
/** internal */
|
|
|
|
template < typename TArg, typename... TArgs >
|
|
|
|
void
|
2018-06-01 17:47:37 +00:00
|
|
|
LogAppend(std::stringstream& ss, TArg&& arg, TArgs&&... args) noexcept
|
2018-05-27 14:04:30 +00:00
|
|
|
{
|
|
|
|
LogAppend(ss, std::forward< TArg >(arg));
|
|
|
|
LogAppend(ss, std::forward< TArgs >(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** internal */
|
|
|
|
template < typename... TArgs >
|
|
|
|
void
|
2018-06-01 17:47:37 +00:00
|
|
|
_Log(LogLevel lvl, const char* fname, TArgs&&... args) noexcept
|
2018-05-27 14:04:30 +00:00
|
|
|
{
|
2018-05-28 13:49:44 +00:00
|
|
|
if(_glog.minlevel > lvl)
|
2018-05-27 14:04:30 +00:00
|
|
|
return;
|
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
std::stringstream ss;
|
2018-05-27 14:04:30 +00:00
|
|
|
switch(lvl)
|
|
|
|
{
|
|
|
|
case eLogDebug:
|
2018-05-28 13:49:44 +00:00
|
|
|
ss << (char)27 << "[0m";
|
2018-05-27 14:04:30 +00:00
|
|
|
ss << "[DBG] ";
|
|
|
|
break;
|
|
|
|
case eLogInfo:
|
2018-05-28 13:49:44 +00:00
|
|
|
ss << (char)27 << "[1m";
|
2018-05-27 14:04:30 +00:00
|
|
|
ss << "[NFO] ";
|
|
|
|
break;
|
|
|
|
case eLogWarn:
|
2018-05-28 13:49:44 +00:00
|
|
|
ss << (char)27 << "[1;33m";
|
2018-05-27 14:04:30 +00:00
|
|
|
ss << "[WRN] ";
|
|
|
|
break;
|
|
|
|
case eLogError:
|
2018-05-28 13:49:44 +00:00
|
|
|
ss << (char)27 << "[1;31m";
|
2018-05-27 14:04:30 +00:00
|
|
|
ss << "[ERR] ";
|
|
|
|
break;
|
|
|
|
}
|
2018-06-01 17:47:37 +00:00
|
|
|
std::string tag = fname;
|
2018-07-24 06:15:29 +00:00
|
|
|
ss << llarp_time_now_ms() << " " << tag;
|
2018-06-06 12:46:26 +00:00
|
|
|
ss << "\t";
|
2018-05-27 14:04:30 +00:00
|
|
|
LogAppend(ss, std::forward< TArgs >(args)...);
|
2018-05-28 13:49:44 +00:00
|
|
|
ss << (char)27 << "[0;0m";
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
|
|
|
std::unique_lock< std::mutex > lock(_glog.access);
|
|
|
|
_glog.out << ss.str() << std::endl;
|
2018-06-06 21:23:57 +00:00
|
|
|
#ifdef SHADOW_TESTNET
|
2018-07-18 20:58:16 +00:00
|
|
|
_glog.out << "\n" << std::flush;
|
2018-06-06 21:23:57 +00:00
|
|
|
#endif
|
2018-07-18 20:58:16 +00:00
|
|
|
}
|
2018-05-27 14:04:30 +00:00
|
|
|
}
|
2018-06-20 12:34:48 +00:00
|
|
|
} // namespace llarp
|
2018-05-27 14:04:30 +00:00
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
#define LogDebug(x, ...) _Log(llarp::eLogDebug, __FILE__, x, ##__VA_ARGS__)
|
|
|
|
#define LogInfo(x, ...) _Log(llarp::eLogInfo, __FILE__, x, ##__VA_ARGS__)
|
|
|
|
#define LogWarn(x, ...) _Log(llarp::eLogWarn, __FILE__, x, ##__VA_ARGS__)
|
|
|
|
#define LogError(x, ...) _Log(llarp::eLogError, __FILE__, x, ##__VA_ARGS__)
|
2018-06-01 17:47:37 +00:00
|
|
|
|
2018-05-27 14:04:30 +00:00
|
|
|
#endif
|