refactor logger

pull/502/head
Jeff Becker 5 years ago
parent 46a7d02c09
commit 9e24557429
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -15,6 +15,9 @@ set(LIB_UTIL_SRC
util/ini.cpp
util/json.cpp
util/logger.cpp
util/android_logger.cpp
util/ostream_logger.cpp
util/win32_logger.cpp
util/logic.cpp
util/mem.cpp
util/metrics_core.cpp

@ -52,6 +52,7 @@ namespace llarp
api = find_section(parser, "api", section_t{});
lokid = find_section(parser, "lokid", section_t{});
bootstrap = find_section(parser, "bootstrap", section_t{});
logging = find_section(parser, "logging", section_t{});
return true;
};
@ -66,6 +67,7 @@ namespace llarp
{"metrics", metrics},
{"netdb", netdb},
{"api", api},
{"logging", logging},
{"services", services}};
auto visitor = [&](const char *name, const auto &item) {
@ -178,9 +180,18 @@ llarp_generic_ensure_config(std::ofstream &f, std::string basepath)
f << "# nickname=lokinet" << std::endl;
f << std::endl << std::endl;
// logging
f << "[logging]" << std::endl;
f << "level=info" << std::endl;
f << "# uncomment for logging to file" << std::endl;
f << "#type=file" << std::endl;
f << "#file=/path/to/logfile" << std::endl;
f << "# uncomment for syslog logging" << std::endl;
f << "#type=syslog" << std::endl;
// metrics
f << "[metrics]\n";
f << "json-metrics-path=" << basepath << "metrics.json\n";
f << "[metrics]" << std::endl;
f << "json-metrics-path=" << basepath << "metrics.json" << std::endl;
f << std::endl << std::endl;

@ -24,6 +24,7 @@ namespace llarp
section_t api;
section_t lokid;
section_t bootstrap;
section_t logging;
bool
Load(const char *fname);

@ -944,7 +944,7 @@ namespace llarp
{
_rc.SetNick(val);
// set logger name here
_glog.nodeName = rc().Nick();
LogContext::Instance().nodeName = rc().Nick();
}
if(StrEq(key, "encryption-privkey"))
{

@ -0,0 +1,62 @@
#if defined(ANDROID)
#include <util/android_logger.hpp>
#include <util/logger_internal.hpp>
#include <android/log.h>
namespace llarp
{
void
AndroidLogStream::PreLog(std::stringstream& ss, LogLevel lvl,
const char* fname, int lineno) const
{
switch(lvl)
{
case eLogNone:
break;
case eLogDebug:
ss << "[DBG] ";
break;
case eLogInfo:
ss << "[NFO] ";
break;
case eLogWarn:
ss << "[WRN] ";
break;
case eLogError:
ss << "[ERR] ";
break;
}
ss << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname
<< ":" << lineno << "\t";
}
void
AndroidLogStream::PostLog(std::stingstream&) const
{
}
void
AndroidLogStream::Print(LogLevel lvl, const char* tag,
const std::string& msg) const
{
std::string str("lokinet|");
str += tag;
switch(lvl)
{
case eLogDebug:
__android_log_write(ANDROID_LOG_DEBUG, str.c_str(), msg.c_str());
return;
case eLogInfo:
__android_log_write(ANDROID_LOG_INFO, str.c_str(), msg.c_str());
return;
case eLogWarn:
__android_log_write(ANDROID_LOG_WARN, str.c_str(), msg.c_str());
return;
case eLogError:
__android_log_write(ANDROID_LOG_ERROR, str.c_str(), msg.c_str());
return;
}
} // namespace llarp
} // namespace llarp
#endif

@ -0,0 +1,20 @@
#ifndef LLARP_UTIL_OSTREAM_LOGGER_HPP
#define LLARP_UTIL_OSTREAM_LOGGER_HPP
#include <util/logstream.hpp>
#include <iostream>
namespace llarp
{
struct AndroidLogStream : public ILogStream
{
void
PreLog(std::stringstream& s, LogLevel lvl, const char* fname,
int lineno) const override;
void
Log(LogLevel lvl, const std::string& msg) const override;
};
} // namespace llarp
#endif

@ -1,14 +1,44 @@
#include <util/logger.hpp>
#include <util/logger.h>
#include <util/ostream_logger.hpp>
#if defined(_WIN32)
#include <util/win32_logger.hpp>
#endif
#if defined(ANDROID)
#include <util/android_logger.hpp>
#endif
namespace llarp
{
Logger _glog;
#if defined(_WIN32)
using Stream_t = Win32LogStream;
#define _LOGSTREAM_INIT std::cout
#else
#if defined(ANDROID)
using Stream_t = AndroidLogStream;
#define _LOGSTREAM_INIT
#else
using Stream_t = OStreamLogStream;
#define _LOGSTREAM_INIT std::cout
#endif
#endif
LogContext::LogContext()
: logStream(std::make_unique< Stream_t >(_LOGSTREAM_INIT))
{
}
LogContext&
LogContext::Instance()
{
static LogContext ctx;
return ctx;
}
void
SetLogLevel(LogLevel lvl)
{
_glog.minlevel = lvl;
LogContext::Instance().minLevel = lvl;
}
} // namespace llarp
@ -23,6 +53,6 @@ extern "C"
void
cSetLogNodeName(const char* name)
{
llarp::_glog.nodeName = name;
llarp::LogContext::Instance().nodeName = name;
}
}

@ -1,8 +1,10 @@
#ifndef LLARP_LOGGER_HPP
#define LLARP_LOGGER_HPP
#ifndef LLARP_UTIL_LOGGER_HPP
#define LLARP_UTIL_LOGGER_HPP
#include <util/time.hpp>
#include <util/logstream.hpp>
#include <util/logger_internal.hpp>
/*
#ifdef _WIN32
#define VC_EXTRALEAN
#include <windows.h>
@ -21,26 +23,19 @@
#include <string>
#include <thread>
#include <functional>
*/
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;
ILogStream_ptr m_stream;
std::unique_ptr< std::ostream > _altOut;
std::function< void(const std::string&) > customLog;
#ifdef _WIN32
bool isConsoleModern =
true; // qol fix so oldfag clients don't see ugly escapes
@ -69,74 +64,52 @@ namespace llarp
Logger(std::ostream& o) : out(o)
{
}
};
extern Logger _glog;
/// open logger to file
/// return false on failure
/// return true on successful open
bool
OpenLogFile(const std::string& file);
void
SetLogLevel(LogLevel lvl);
/// call once to use syslog based logging
void
UseSyslog();
/** 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
}
virtual void
Print(LogLevel lvl, const std::string& msg);
};
*/
struct log_timestamp
struct LogContext
{
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
}
LogContext();
LogLevel minLevel = eLogInfo;
ILogStream_ptr logStream;
std::string nodeName;
static LogContext&
Instance();
};
void
SetLogLevel(LogLevel lvl);
/** internal */
template < typename... TArgs >
void
_Log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept
{
if(_glog.minlevel > lvl)
auto& log = LogContext::Instance();
if(log.minLevel > lvl)
return;
std::stringstream ss;
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;
#ifdef ANDROID
int loglev = -1;
switch(lvl)
@ -259,6 +232,7 @@ namespace llarp
}
#endif
}
*/
} // namespace llarp
#define LogDebug(...) _Log(llarp::eLogDebug, LOG_TAG, __LINE__, __VA_ARGS__)

@ -0,0 +1,66 @@
#ifndef LLARP_UTIL_LOGGER_INTERNAL_HPP
#define LLARP_UTIL_LOGGER_INTERNAL_HPP
#include <util/time.hpp>
#include <sstream>
#include <ctime>
#include <iomanip>
#include <thread>
namespace llarp
{
/** 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
}
};
} // namespace llarp
#endif

@ -0,0 +1,18 @@
#ifndef LLARP_UTIL_LOG_LEVEL_HPP
#define LLARP_UTIL_LOG_LEVEL_HPP
namespace llarp
{
// probably will need to move out of llarp namespace for c api
enum LogLevel
{
eLogDebug,
eLogInfo,
eLogWarn,
eLogError,
eLogNone
};
} // namespace llarp
#endif

@ -0,0 +1,27 @@
#ifndef LLARP_UTIL_LOG_STREAM_HPP
#define LLARP_UTIL_LOG_STREAM_HPP
#include <memory>
#include <string>
#include <util/loglevel.hpp>
#include <sstream>
namespace llarp
{
/// logger stream interface
struct ILogStream
{
virtual ~ILogStream(){};
virtual void
PreLog(std::stringstream& out, LogLevel lvl, const char* fname,
int lineno) const = 0;
virtual void
Print(LogLevel lvl, const char* filename, const std::string& msg) const = 0;
virtual void
PostLog(std::stringstream& out) const = 0;
};
using ILogStream_ptr = std::unique_ptr< ILogStream >;
} // namespace llarp
#endif

@ -0,0 +1,52 @@
#include <util/ostream_logger.hpp>
#include <util/logger_internal.hpp>
namespace llarp
{
OStreamLogStream::OStreamLogStream(std::ostream& out) : m_Out(out)
{
}
void
OStreamLogStream::PreLog(std::stringstream& ss, LogLevel lvl,
const char* fname, int lineno) const
{
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;
}
ss << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname
<< ":" << lineno << "\t";
}
void
OStreamLogStream::PostLog(std::stringstream& ss) const
{
ss << (char)27 << "[0;0m" << std::endl;
}
void
OStreamLogStream::Print(LogLevel, const char*, const std::string& msg) const
{
m_Out << msg;
}
} // namespace llarp

@ -0,0 +1,32 @@
#ifndef LLARP_UTIL_OSTREAM_LOGGER_HPP
#define LLARP_UTIL_OSTREAM_LOGGER_HPP
#include <util/logstream.hpp>
#include <iostream>
namespace llarp
{
struct OStreamLogStream : public ILogStream
{
OStreamLogStream(std::ostream& out);
~OStreamLogStream()
{
}
virtual void
PreLog(std::stringstream& s, LogLevel lvl, const char* fname,
int lineno) const override;
void
Print(LogLevel lvl, const char* tag, const std::string& msg) const override;
virtual void
PostLog(std::stringstream& ss) const override;
private:
std::ostream& m_Out;
};
} // namespace llarp
#endif

@ -0,0 +1,62 @@
#if defined(_WIN32)
#include <util/win32_logger.hpp>
#include <util/logger_internal.hpp>
namespace llarp
{
Win32LogStream::Win32LogStream(std::ostream& out) : OStreamLogStream(out)
{
// 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
}
void
Win32LogStream::PreLog(std::stringstream& ss, LogLevel lvl, const char* fname,
int lineno) const
{
if(!isConsoleModern)
{
switch(lvl)
{
case eLogNone:
break;
case eLogDebug:
ss << "[DBG] ";
break;
case eLogInfo:
ss << "[NFO] ";
break;
case eLogWarn:
ss << "[WRN] ";
break;
case eLogError:
ss << "[ERR] ";
break;
}
ss << "(" << thread_id_string() << ") " << log_timestamp() << " " << fname
<< ":" << lineno << "\t";
}
else
OStreamLogStream::PreLog(ss, lvl, fname, lineno);
}
void
Win32LogStream::PostLog(std::stringstream& ss) const
{
if(isConsoleModern)
OStreamLogStream::PostLog(ss);
else
ss << std::endl;
}
} // namespace llarp
#endif

@ -0,0 +1,29 @@
#ifndef LLARP_UTIL_WIN32_LOGGER_HPP
#define LLARP_UTIL_WIN32_LOGGER_HPP
#if defined(_WIN32)
#include <util/ostream_logger.hpp>
#define VC_EXTRALEAN
#include <windows.h>
namespace llarp
{
struct Win32LogStream : public OStreamLogStream
{
Win32LogStream(std::ostream& out);
void
PreLog(std::stringstream& s, LogLevel lvl, const char* fname,
int lineno) const override;
void
PostLog(std::stringstream& s) const override;
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;
};
} // namespace llarp
#endif
#endif
Loading…
Cancel
Save