mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-02 03:40:12 +00:00
9816fd65e9
* don't inline bencode * refactor initial start of dht code a bit
97 lines
2.1 KiB
C++
97 lines
2.1 KiB
C++
#ifndef LLARP_LOGGER_HPP
|
|
#define LLARP_LOGGER_HPP
|
|
|
|
#include <ctime>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
namespace llarp
|
|
{
|
|
enum LogLevel
|
|
{
|
|
eLogDebug,
|
|
eLogInfo,
|
|
eLogWarn,
|
|
eLogError
|
|
};
|
|
|
|
struct Logger
|
|
{
|
|
LogLevel minlevel = eLogDebug;
|
|
std::ostream& out = std::cout;
|
|
};
|
|
|
|
extern Logger _glog;
|
|
|
|
void
|
|
SetLogLevel(LogLevel lvl);
|
|
|
|
/** internal */
|
|
template < typename TArg >
|
|
void
|
|
LogAppend(std::stringstream& ss, TArg&& arg) noexcept
|
|
{
|
|
ss << std::forward< TArg >(arg);
|
|
}
|
|
/** internal */
|
|
template < typename TArg, typename... TArgs >
|
|
void
|
|
LogAppend(std::stringstream& ss, TArg&& arg, TArgs&&... args) noexcept
|
|
{
|
|
LogAppend(ss, std::forward< TArg >(arg));
|
|
LogAppend(ss, std::forward< TArgs >(args)...);
|
|
}
|
|
|
|
/** internal */
|
|
template < typename... TArgs >
|
|
void
|
|
_Log(LogLevel lvl, const char* fname, TArgs&&... args) noexcept
|
|
{
|
|
if(_glog.minlevel > lvl)
|
|
return;
|
|
|
|
std::stringstream ss("");
|
|
switch(lvl)
|
|
{
|
|
case eLogDebug:
|
|
ss << (char)27 << "[0m";
|
|
ss << "[DBG] ";
|
|
break;
|
|
case eLogInfo:
|
|
ss << (char)27 << "[1m";
|
|
ss << "[NFO] ";
|
|
break;
|
|
case eLogWarn:
|
|
ss << (char)27 << "[1;33m";
|
|
ss << "[WRN] ";
|
|
break;
|
|
case eLogError:
|
|
ss << (char)27 << "[1;31m";
|
|
ss << "[ERR] ";
|
|
break;
|
|
}
|
|
auto t = std::time(nullptr);
|
|
auto now = std::localtime(&t);
|
|
std::string tag = fname;
|
|
auto pos = tag.rfind('/');
|
|
if(pos != std::string::npos)
|
|
tag = tag.substr(pos + 1);
|
|
|
|
while(tag.size() % 8)
|
|
tag += " ";
|
|
ss << std::put_time(now, "%F %T") << " " << tag << "\t";
|
|
LogAppend(ss, std::forward< TArgs >(args)...);
|
|
ss << (char)27 << "[0;0m";
|
|
_glog.out << ss.str() << std::endl;
|
|
}
|
|
}
|
|
|
|
#define Debug(x, ...) _Log(llarp::eLogDebug, __FILE__, x, ##__VA_ARGS__)
|
|
#define Info(x, ...) _Log(llarp::eLogInfo, __FILE__, x, ##__VA_ARGS__)
|
|
#define Warn(x, ...) _Log(llarp::eLogWarn, __FILE__, x, ##__VA_ARGS__)
|
|
#define Error(x, ...) _Log(llarp::eLogError, __FILE__, x, ##__VA_ARGS__)
|
|
|
|
#endif
|