lokinet/include/llarp/logger.hpp

195 lines
4.6 KiB
C++
Raw Normal View History

2018-05-27 14:04:30 +00:00
#ifndef LLARP_LOGGER_HPP
#define LLARP_LOGGER_HPP
#include <llarp/time.hpp>
#include <ctime>
#include <iomanip>
2018-05-27 14:04:30 +00:00
#include <iostream>
#include <llarp/threading.hpp>
2018-05-27 14:04:30 +00:00
#include <sstream>
#include <string>
#ifdef _WIN32
#define VC_EXTRALEAN
#include <windows.h>
#endif
2018-08-06 04:35:00 +00:00
#ifdef ANDROID
#include <android/log.h>
#endif
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,
2018-10-09 12:39:14 +00:00
eLogError,
eLogNone
2018-05-27 14:04:30 +00:00
};
struct Logger
{
std::string nodeName;
2018-06-06 21:23:57 +00:00
LogLevel minlevel = eLogInfo;
2018-07-24 06:20:05 +00:00
std::ostream& out;
2018-08-12 17:22:29 +00:00
llarp::util::Mutex access;
Logger() : Logger(std::cout, "unnamed")
2018-07-24 06:20:05 +00:00
{
#ifdef _WIN32
DWORD mode_flags;
HANDLE fd1 = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(fd1, &mode_flags);
// since release SDKs don't have ANSI escape support yet
mode_flags |= 0x0004;
SetConsoleMode(fd1, mode_flags);
#endif
2018-07-24 06:20:05 +00:00
}
Logger(std::ostream& o, const std::string& name) : nodeName(name), out(o)
2018-07-24 06:20:05 +00:00
{
}
};
extern Logger _glog;
void
SetLogLevel(LogLevel lvl);
2018-05-27 14:04:30 +00:00
/** internal */
template < typename TArg >
void
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
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)...);
}
2018-08-18 14:01:21 +00:00
static inline std::string
thread_id_string()
{
auto tid = std::this_thread::get_id();
std::hash< std::thread::id > h;
uint16_t id = h(tid) % 1000;
return std::to_string(id);
}
struct log_timestamp
{
std::string format = "%c %Z";
friend
std::ostream & operator << (std::ostream & out, const log_timestamp & ts)
{
auto now = llarp::Clock_t::to_time_t(llarp::Clock_t::now());
return out << std::put_time(std::localtime(&now), ts.format.c_str());
}
};
2018-05-27 14:04:30 +00:00
/** internal */
template < typename... TArgs >
void
_Log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept
2018-05-27 14:04:30 +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-08-06 04:42:22 +00:00
#ifdef ANDROID
2018-08-12 17:22:29 +00:00
int loglev = -1;
2018-08-06 04:42:22 +00:00
switch(lvl)
{
2018-10-09 12:39:14 +00:00
case eLogNone:
break;
2018-08-06 04:42:22 +00:00
case eLogDebug:
ss << "[DBG] ";
2018-08-12 17:22:29 +00:00
loglev = ANDROID_LOG_DEBUG;
2018-08-06 04:42:22 +00:00
break;
case eLogInfo:
ss << "[NFO] ";
2018-08-12 17:22:29 +00:00
loglev = ANDROID_LOG_INFO;
2018-08-06 04:42:22 +00:00
break;
case eLogWarn:
ss << "[WRN] ";
2018-08-12 17:22:29 +00:00
loglev = ANDROID_LOG_WARN;
2018-08-06 04:42:22 +00:00
break;
case eLogError:
ss << "[ERR] ";
2018-08-12 17:22:29 +00:00
loglev = ANDROID_LOG_ERROR;
2018-08-06 04:42:22 +00:00
break;
}
#else
2018-05-27 14:04:30 +00:00
switch(lvl)
{
2018-10-09 12:39:14 +00:00
case eLogNone:
break;
2018-05-27 14:04:30 +00:00
case eLogDebug:
ss << (char)27 << "[0m";
2018-05-27 14:04:30 +00:00
ss << "[DBG] ";
break;
case eLogInfo:
ss << (char)27 << "[1m";
2018-05-27 14:04:30 +00:00
ss << "[NFO] ";
break;
case eLogWarn:
ss << (char)27 << "[1;33m";
2018-05-27 14:04:30 +00:00
ss << "[WRN] ";
break;
case eLogError:
ss << (char)27 << "[1;31m";
2018-05-27 14:04:30 +00:00
ss << "[ERR] ";
break;
}
2018-08-06 04:42:22 +00:00
#endif
std::string tag = fname;
2018-08-18 14:01:21 +00:00
ss << _glog.nodeName << " (" << thread_id_string() << ") "
<< log_timestamp() << " " << tag << ":" << lineno;
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-08-06 04:42:22 +00:00
#ifndef ANDROID
ss << (char)27 << "[0;0m";
2018-08-08 09:49:47 +00:00
_glog.out << ss.str() << std::endl;
2018-08-06 04:42:22 +00:00
#else
{
std::unique_lock< std::mutex > lock(_glog.access);
2018-08-12 17:22:29 +00:00
tag = "LOKINET|" + tag;
__android_log_write(ANDROID_LOG_INFO, tag.c_str(), ss.str().c_str());
//__android_log_write(ANDROID_LOG_INFO, "LOKINET", ss.str().c_str());
2018-08-12 17:22:29 +00:00
llarp::util::Lock lock(_glog.access);
2018-08-08 09:49:47 +00:00
_glog.out << ss.str() << std::endl;
}
2018-08-08 09:49:47 +00:00
#endif
#ifdef SHADOW_TESTNET
_glog.out << "\n" << std::flush;
#endif
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
#define LogDebug(x, ...) \
_Log(llarp::eLogDebug, LOG_TAG, __LINE__, x, ##__VA_ARGS__)
#define LogInfo(x, ...) \
_Log(llarp::eLogInfo, LOG_TAG, __LINE__, x, ##__VA_ARGS__)
#define LogWarn(x, ...) \
_Log(llarp::eLogWarn, LOG_TAG, __LINE__, x, ##__VA_ARGS__)
#define LogError(x, ...) \
_Log(llarp::eLogError, LOG_TAG, __LINE__, x, ##__VA_ARGS__)
#define LogDebugTag(tag, x, ...) \
_Log(llarp::eLogDebug, tag, __LINE__, x, ##__VA_ARGS__)
#define LogInfoTag(tag, x, ...) \
_Log(llarp::eLogInfo, tag, __LINE__, x, ##__VA_ARGS__)
#define LogWarnTag(tag, x, ...) \
_Log(llarp::eLogWarn, tag, __LINE__, x, ##__VA_ARGS__)
#define LogErrorTag(tag, x, ...) \
_Log(llarp::eLogError, tag, __LINE__, x, ##__VA_ARGS__)
2018-07-30 22:35:54 +00:00
2018-08-01 22:10:38 +00:00
#ifndef LOG_TAG
#define LOG_TAG "default"
#endif
2018-05-27 14:04:30 +00:00
#endif