Move logger.hpp to llarp/

pull/137/head
Michael 6 years ago
parent 30601720e9
commit a5c3ba0fdd
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -1,12 +1,13 @@
#include <config.hpp> // for ensure_config
#include <fs.hpp>
#include <getopt.h>
#include <libgen.h>
#include <llarp.h>
#include <llarp/logger.hpp>
#include <logger.hpp>
#include <signal.h>
#include <getopt.h>
#include <string>
#include <iostream>
#include <libgen.h>
#include "fs.hpp"
#include "config.hpp" // for ensure_config
#ifdef _WIN32
#define wmin(x, y) (((x) < (y)) ? (x) : (y))

@ -4,7 +4,7 @@
#include <encode.hpp>
#include <llarp/bencode.h>
#include <llarp/crypto.h>
#include <llarp/logger.hpp>
#include <logger.hpp>
#include <iomanip>
#include <iostream>

@ -3,7 +3,7 @@
#include <buffer.hpp>
#include <llarp/bencode.h>
#include <llarp/logger.hpp>
#include <logger.hpp>
#include <llarp/mem.hpp>
#include <set>

@ -1,15 +1,14 @@
#ifndef LLARP_CODEL_QUEUE_HPP
#define LLARP_CODEL_QUEUE_HPP
#include <llarp/time.hpp>
#include <llarp/logger.hpp>
#include <llarp/mem.hpp>
#include <llarp/threading.hpp>
#include <llarp/time.hpp>
#include <logger.hpp>
#include <algorithm>
#include <array>
#include <cmath>
#include <functional>
#include <array>
#include <string>
namespace llarp

@ -1,278 +0,0 @@
#ifndef LLARP_LOGGER_HPP
#define LLARP_LOGGER_HPP
#include <llarp/time.hpp>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <llarp/threading.hpp>
#include <sstream>
#include <string>
#ifdef _WIN32
#define VC_EXTRALEAN
#include <windows.h>
#endif
#ifdef ANDROID
#include <android/log.h>
#endif
#ifdef RPI
#include <cstdio>
#endif
namespace llarp
{
// probably will need to move out of llarp namespace for c api
enum LogLevel
{
eLogDebug,
eLogInfo,
eLogWarn,
eLogError,
eLogNone
};
struct Logger
{
std::string nodeName;
LogLevel minlevel = eLogInfo;
std::ostream& out;
std::function< void(const std::string&) > customLog;
llarp::util::Mutex access;
#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
Logger() : Logger(std::cout, "unnamed")
{
#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
}
Logger(std::ostream& o, const std::string& name) : nodeName(name), out(o)
{
}
};
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)...);
}
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;
#if defined(ANDROID) || defined(RPI)
char buff[8] = {0};
snprintf(buff, sizeof(buff), "%u", id);
return buff;
#else
return std::to_string(id);
#endif
}
struct log_timestamp
{
const char* format;
log_timestamp(const char* fmt = "%c %Z") : format(fmt)
{
}
friend std::ostream&
operator<<(std::ostream& out, const log_timestamp& ts)
{
#if defined(ANDROID) || defined(RPI)
(void)ts;
return out << time_now_ms();
#else
auto now = llarp::Clock_t::to_time_t(llarp::Clock_t::now());
return out << std::put_time(std::localtime(&now), ts.format);
#endif
}
};
/** internal */
template < typename... TArgs >
void
_Log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept
{
if(_glog.minlevel > lvl)
return;
std::stringstream ss;
#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;
ss << _glog.nodeName << " (" << thread_id_string() << ") "
<< log_timestamp() << " " << tag << ":" << lineno;
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;
#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
}
} // namespace llarp
#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__)
#ifndef LOG_TAG
#define LOG_TAG "default"
#endif
#endif

@ -1,6 +1,6 @@
#include <abyss/client.hpp>
#include <abyss/http.hpp>
#include <llarp/logger.hpp>
#include <logger.hpp>
#include <crypto.hpp>
#include <buffer.hpp>

@ -1,7 +1,7 @@
#include <abyss/http.hpp>
#include <abyss/server.hpp>
#include <buffer.hpp>
#include <llarp/logger.hpp>
#include <logger.hpp>
#include <llarp/time.hpp>
#include <algorithm>

@ -1,5 +1,5 @@
#include <llarp/bencode.h>
#include <llarp/logger.hpp>
#include <logger.hpp>
bool
bencode_write_bytestring(llarp_buffer_t* buff, const void* data, size_t sz)

@ -1,6 +1,6 @@
#include <llarp/endian.hpp>
#include <llarp/dnsd.hpp> // for llarp_handle_dnsd_recvfrom, dnsc
#include <llarp/logger.hpp>
#include <logger.hpp>
void
hexDump(const char *buffer, uint16_t size)

@ -1,7 +1,7 @@
#include <buffer.hpp>
#include <dns/message.hpp>
#include <llarp/endian.hpp>
#include <llarp/logger.hpp>
#include <logger.hpp>
namespace llarp
{

@ -1,5 +1,5 @@
#include <dns/question.hpp>
#include <llarp/logger.hpp>
#include <logger.hpp>
namespace llarp
{

@ -1,5 +1,5 @@
#include <dns/rr.hpp>
#include <llarp/logger.hpp>
#include <logger.hpp>
namespace llarp
{

@ -14,7 +14,7 @@
#include <algorithm> // for std::find_if
#include <llarp/net.hpp> // for llarp::Addr
#include <llarp/logger.hpp>
#include <logger.hpp>
#include <stdio.h> // sprintf
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))

@ -15,7 +15,7 @@
#include <cstring>
#include <fstream>
#include <linux/netns.hpp>
#include <llarp/logger.hpp>
#include <logger.hpp>
#ifndef MS_REC
#define MS_REC (16384)
#endif

@ -1 +1,278 @@
#include <llarp/logger.hpp>
#ifndef LLARP_LOGGER_HPP
#define LLARP_LOGGER_HPP
#include <llarp/time.hpp>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <llarp/threading.hpp>
#include <sstream>
#include <string>
#ifdef _WIN32
#define VC_EXTRALEAN
#include <windows.h>
#endif
#ifdef ANDROID
#include <android/log.h>
#endif
#ifdef RPI
#include <cstdio>
#endif
namespace llarp
{
// probably will need to move out of llarp namespace for c api
enum LogLevel
{
eLogDebug,
eLogInfo,
eLogWarn,
eLogError,
eLogNone
};
struct Logger
{
std::string nodeName;
LogLevel minlevel = eLogInfo;
std::ostream& out;
std::function< void(const std::string&) > customLog;
llarp::util::Mutex access;
#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
Logger() : Logger(std::cout, "unnamed")
{
#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
}
Logger(std::ostream& o, const std::string& name) : nodeName(name), out(o)
{
}
};
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)...);
}
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;
#if defined(ANDROID) || defined(RPI)
char buff[8] = {0};
snprintf(buff, sizeof(buff), "%u", id);
return buff;
#else
return std::to_string(id);
#endif
}
struct log_timestamp
{
const char* format;
log_timestamp(const char* fmt = "%c %Z") : format(fmt)
{
}
friend std::ostream&
operator<<(std::ostream& out, const log_timestamp& ts)
{
#if defined(ANDROID) || defined(RPI)
(void)ts;
return out << time_now_ms();
#else
auto now = llarp::Clock_t::to_time_t(llarp::Clock_t::now());
return out << std::put_time(std::localtime(&now), ts.format);
#endif
}
};
/** internal */
template < typename... TArgs >
void
_Log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept
{
if(_glog.minlevel > lvl)
return;
std::stringstream ss;
#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;
ss << _glog.nodeName << " (" << thread_id_string() << ") "
<< log_timestamp() << " " << tag << ":" << lineno;
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;
#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
}
} // namespace llarp
#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__)
#ifndef LOG_TAG
#define LOG_TAG "default"
#endif
#endif

@ -2,7 +2,7 @@
#include <encode.hpp>
#include <llarp/aligned.hpp>
#include <llarp/logger.hpp>
#include <logger.hpp>
struct Base32Test : public ::testing::Test
{

@ -50,7 +50,7 @@ inet_ntop(int af, const void *src, char *dst, size_t size);
#include <stdarg.h>
#include <llarp/logger.hpp>
#include <logger.hpp>
#include "tuntap.h"

@ -23,7 +23,7 @@
#include <stdio.h>
#include <string.h>
#include <llarp/logger.hpp>
#include <logger.hpp>
#include "tuntap.h"
extern "C"

Loading…
Cancel
Save