2019-04-11 12:58:23 +00:00
|
|
|
#ifndef LLARP_UTIL_LOGGER_HPP
|
|
|
|
#define LLARP_UTIL_LOGGER_HPP
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/time.hpp>
|
2019-04-11 12:58:23 +00:00
|
|
|
#include <util/logstream.hpp>
|
|
|
|
#include <util/logger_internal.hpp>
|
|
|
|
/*
|
2018-12-12 01:47:29 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define VC_EXTRALEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
#ifdef RPI
|
|
|
|
#include <cstdio>
|
|
|
|
#endif
|
|
|
|
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2019-03-03 20:51:47 +00:00
|
|
|
#include <thread>
|
2019-03-07 21:45:37 +00:00
|
|
|
#include <functional>
|
2019-04-11 12:58:23 +00:00
|
|
|
*/
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-12-12 01:47:29 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-04-11 12:58:23 +00:00
|
|
|
/*
|
2018-12-12 01:47:29 +00:00
|
|
|
struct Logger
|
|
|
|
{
|
|
|
|
std::string nodeName;
|
|
|
|
LogLevel minlevel = eLogInfo;
|
2019-04-11 12:58:23 +00:00
|
|
|
ILogStream_ptr m_stream;
|
|
|
|
|
|
|
|
std::unique_ptr< std::ostream > _altOut;
|
2018-12-12 01:47:29 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
bool isConsoleModern =
|
|
|
|
true; // qol fix so oldfag clients don't see ugly escapes
|
|
|
|
HANDLE fd1 = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
|
|
|
|
short old_attrs;
|
|
|
|
#endif
|
2018-12-23 15:33:26 +00:00
|
|
|
Logger() : Logger(std::cout)
|
2018-12-12 01:47:29 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
// Attempt to use ANSI escapes directly
|
|
|
|
// if the modern console is active.
|
|
|
|
DWORD mode_flags;
|
|
|
|
|
|
|
|
GetConsoleMode(fd1, &mode_flags);
|
|
|
|
// since release SDKs don't have ANSI escape support yet
|
|
|
|
// we get all or nothing: if we can't get it, then we wouldn't
|
|
|
|
// be able to get any of them individually
|
|
|
|
mode_flags |= 0x0004 | 0x0008;
|
|
|
|
BOOL t = SetConsoleMode(fd1, mode_flags);
|
|
|
|
if(!t)
|
|
|
|
this->isConsoleModern = false; // fall back to setting colours manually
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-12-23 15:33:26 +00:00
|
|
|
Logger(std::ostream& o) : out(o)
|
2018-12-12 01:47:29 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
/// open logger to file
|
|
|
|
/// return false on failure
|
|
|
|
/// return true on successful open
|
|
|
|
bool
|
|
|
|
OpenLogFile(const std::string& file);
|
2018-12-12 01:47:29 +00:00
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
/// call once to use syslog based logging
|
|
|
|
void
|
|
|
|
UseSyslog();
|
2018-12-12 01:47:29 +00:00
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
virtual void
|
|
|
|
Print(LogLevel lvl, const std::string& msg);
|
|
|
|
};
|
|
|
|
*/
|
2018-12-12 01:47:29 +00:00
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
struct LogContext
|
2018-12-12 01:47:29 +00:00
|
|
|
{
|
2019-04-11 12:58:23 +00:00
|
|
|
LogContext();
|
|
|
|
LogLevel minLevel = eLogInfo;
|
|
|
|
ILogStream_ptr logStream;
|
|
|
|
std::string nodeName;
|
|
|
|
static LogContext&
|
|
|
|
Instance();
|
2018-12-12 01:47:29 +00:00
|
|
|
};
|
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
void
|
|
|
|
SetLogLevel(LogLevel lvl);
|
|
|
|
|
2018-12-12 01:47:29 +00:00
|
|
|
/** internal */
|
|
|
|
template < typename... TArgs >
|
|
|
|
void
|
|
|
|
_Log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept
|
|
|
|
{
|
2019-04-11 12:58:23 +00:00
|
|
|
auto& log = LogContext::Instance();
|
|
|
|
if(log.minLevel > lvl)
|
2018-12-12 01:47:29 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::stringstream ss;
|
2019-04-11 12:58:23 +00:00
|
|
|
log.logStream->PreLog(ss, lvl, fname, lineno);
|
|
|
|
LogAppend(ss, std::forward< TArgs >(args)...);
|
|
|
|
log.logStream->PostLog(ss);
|
|
|
|
log.logStream->Print(lvl, fname, ss.str());
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
std::stringstream ss;
|
2018-12-12 01:47:29 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
int loglev = -1;
|
|
|
|
switch(lvl)
|
|
|
|
{
|
|
|
|
case eLogNone:
|
|
|
|
break;
|
|
|
|
case eLogDebug:
|
|
|
|
ss << "[DBG] ";
|
|
|
|
loglev = ANDROID_LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
case eLogInfo:
|
|
|
|
ss << "[NFO] ";
|
|
|
|
loglev = ANDROID_LOG_INFO;
|
|
|
|
break;
|
|
|
|
case eLogWarn:
|
|
|
|
ss << "[WRN] ";
|
|
|
|
loglev = ANDROID_LOG_WARN;
|
|
|
|
break;
|
|
|
|
case eLogError:
|
|
|
|
ss << "[ERR] ";
|
|
|
|
loglev = ANDROID_LOG_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef _WIN32
|
|
|
|
if(_glog.isConsoleModern)
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
switch(lvl)
|
|
|
|
{
|
|
|
|
case eLogNone:
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
|
|
}
|
|
|
|
else // legacy console
|
|
|
|
{
|
|
|
|
// these _should_ be low white on black
|
|
|
|
GetConsoleScreenBufferInfo(_glog.fd1, &_glog.consoleInfo);
|
|
|
|
_glog.old_attrs = _glog.consoleInfo.wAttributes;
|
|
|
|
switch(lvl)
|
|
|
|
{
|
|
|
|
case eLogNone:
|
|
|
|
break;
|
|
|
|
case eLogDebug:
|
|
|
|
SetConsoleTextAttribute(_glog.fd1,
|
|
|
|
FOREGROUND_RED | FOREGROUND_GREEN
|
|
|
|
| FOREGROUND_BLUE); // low white on black
|
|
|
|
ss << "[DBG] ";
|
|
|
|
break;
|
|
|
|
case eLogInfo:
|
|
|
|
SetConsoleTextAttribute(
|
|
|
|
_glog.fd1,
|
|
|
|
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN
|
|
|
|
| FOREGROUND_BLUE); // high white on black
|
|
|
|
ss << "[NFO] ";
|
|
|
|
break;
|
|
|
|
case eLogWarn:
|
|
|
|
SetConsoleTextAttribute(_glog.fd1,
|
|
|
|
FOREGROUND_RED | FOREGROUND_GREEN
|
|
|
|
| FOREGROUND_INTENSITY); // bright yellow
|
|
|
|
ss << "[WRN] ";
|
|
|
|
break;
|
|
|
|
case eLogError:
|
|
|
|
SetConsoleTextAttribute(
|
|
|
|
_glog.fd1, FOREGROUND_RED | FOREGROUND_INTENSITY); // bright red
|
|
|
|
ss << "[ERR] ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
std::string tag = fname;
|
2018-12-23 15:33:26 +00:00
|
|
|
if(_glog.nodeName.size())
|
|
|
|
ss << _glog.nodeName << " ";
|
2019-01-10 19:41:51 +00:00
|
|
|
ss << "(" << thread_id_string() << ") " << log_timestamp() << " " << tag
|
|
|
|
<< ":" << lineno;
|
2018-12-12 01:47:29 +00:00
|
|
|
ss << "\t";
|
|
|
|
LogAppend(ss, std::forward< TArgs >(args)...);
|
|
|
|
#ifndef ANDROID
|
|
|
|
#ifdef _WIN32
|
|
|
|
if(_glog.isConsoleModern)
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
ss << (char)27 << "[0;0m";
|
|
|
|
_glog.out << ss.str() << std::endl;
|
2019-03-07 22:53:36 +00:00
|
|
|
#ifdef TESTNET
|
|
|
|
_glog.out << std::flush;
|
|
|
|
#endif
|
2018-12-12 01:47:29 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_glog.out << ss.str() << std::endl;
|
|
|
|
SetConsoleTextAttribute(
|
|
|
|
_glog.fd1, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
tag = "LOKINET|" + tag;
|
|
|
|
__android_log_write(loglev, tag.c_str(), ss.str().c_str());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2019-04-11 12:58:23 +00:00
|
|
|
*/
|
2018-12-12 01:47:29 +00:00
|
|
|
} // namespace llarp
|
|
|
|
|
2019-02-11 00:02:20 +00:00
|
|
|
#define LogDebug(...) _Log(llarp::eLogDebug, LOG_TAG, __LINE__, __VA_ARGS__)
|
|
|
|
#define LogInfo(...) _Log(llarp::eLogInfo, LOG_TAG, __LINE__, __VA_ARGS__)
|
|
|
|
#define LogWarn(...) _Log(llarp::eLogWarn, LOG_TAG, __LINE__, __VA_ARGS__)
|
|
|
|
#define LogError(...) _Log(llarp::eLogError, LOG_TAG, __LINE__, __VA_ARGS__)
|
|
|
|
#define LogDebugTag(tag, ...) _Log(llarp::eLogDebug, tag, __LINE__, __VA_ARGS__)
|
|
|
|
#define LogInfoTag(tag, ...) _Log(llarp::eLogInfo, tag, __LINE__, __VA_ARGS__)
|
|
|
|
#define LogWarnTag(tag, ...) _Log(llarp::eLogWarn, tag, __LINE__, __VA_ARGS__)
|
|
|
|
#define LogErrorTag(tag, ...) _Log(llarp::eLogError, tag, __LINE__, __VA_ARGS__)
|
2018-12-12 01:47:29 +00:00
|
|
|
|
|
|
|
#ifndef LOG_TAG
|
|
|
|
#define LOG_TAG "default"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|