mirror of https://github.com/oxen-io/lokinet
refactor logger
parent
46a7d02c09
commit
9e24557429
@ -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
|
@ -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…
Reference in New Issue