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
|
|
|
|
2020-04-02 17:45:33 +00:00
|
|
|
#include <memory>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/time.hpp>
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logstream.hpp>
|
|
|
|
#include <util/logging/logger_internal.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-12-12 01:47:29 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2020-04-02 17:45:33 +00:00
|
|
|
enum class LogType
|
|
|
|
{
|
|
|
|
Unknown = 0,
|
|
|
|
File,
|
|
|
|
Json,
|
|
|
|
Syslog,
|
|
|
|
};
|
2020-04-07 20:41:11 +00:00
|
|
|
LogType
|
|
|
|
LogTypeFromString(const std::string&);
|
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
|
|
|
{
|
2020-06-11 11:44:02 +00:00
|
|
|
using IOFunc_t = std::function<void(void)>;
|
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
LogContext();
|
2020-04-07 18:38:56 +00:00
|
|
|
LogLevel curLevel = eLogInfo;
|
2019-12-09 13:08:30 +00:00
|
|
|
LogLevel startupLevel = eLogInfo;
|
2020-01-21 17:31:48 +00:00
|
|
|
LogLevel runtimeLevel = eLogInfo;
|
2019-04-11 12:58:23 +00:00
|
|
|
ILogStream_ptr logStream;
|
2019-06-13 13:26:34 +00:00
|
|
|
std::string nodeName = "lokinet";
|
2019-06-10 13:20:48 +00:00
|
|
|
|
|
|
|
const llarp_time_t started;
|
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
static LogContext&
|
|
|
|
Instance();
|
2019-12-09 13:08:30 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
DropToRuntimeLevel();
|
|
|
|
|
|
|
|
void
|
|
|
|
RevertRuntimeLevel();
|
2020-04-02 16:47:56 +00:00
|
|
|
|
|
|
|
/// A blocking call that will not return until any existing log functions have flushed.
|
|
|
|
/// Should only be called in rare circumstances, such as when the program is about to exit.
|
|
|
|
void
|
|
|
|
ImmediateFlush();
|
2020-04-02 17:45:33 +00:00
|
|
|
|
|
|
|
/// Initialize the logging system.
|
|
|
|
///
|
|
|
|
/// @param level is the new log level (below which log statements will be ignored)
|
|
|
|
/// @param type is the type of logger to set up
|
|
|
|
/// @param file is the file to log to (relevant for types File and Json)
|
|
|
|
/// @param nickname is a tag to add to each log statement
|
2020-06-11 11:44:02 +00:00
|
|
|
/// @param io is a callable that queues work that does io, async
|
2020-04-02 17:45:33 +00:00
|
|
|
void
|
2020-04-07 20:41:11 +00:00
|
|
|
Initialize(
|
|
|
|
LogLevel level,
|
|
|
|
LogType type,
|
|
|
|
const std::string& file,
|
|
|
|
const std::string& nickname,
|
2020-06-11 11:44:02 +00:00
|
|
|
std::function<void(IOFunc_t)> io);
|
2018-12-12 01:47:29 +00:00
|
|
|
};
|
|
|
|
|
2020-06-08 13:26:53 +00:00
|
|
|
/// RAII type to turn logging off
|
|
|
|
/// logging is suppressed as long as the silencer is in scope
|
|
|
|
struct LogSilencer
|
|
|
|
{
|
|
|
|
LogSilencer();
|
|
|
|
~LogSilencer();
|
|
|
|
explicit LogSilencer(LogContext& ctx);
|
|
|
|
|
|
|
|
private:
|
2020-06-08 13:31:09 +00:00
|
|
|
LogContext& parent;
|
2020-06-08 13:26:53 +00:00
|
|
|
ILogStream_ptr stream;
|
|
|
|
};
|
2020-06-08 13:07:49 +00:00
|
|
|
|
2019-04-11 12:58:23 +00:00
|
|
|
void
|
|
|
|
SetLogLevel(LogLevel lvl);
|
|
|
|
|
2020-06-08 12:42:10 +00:00
|
|
|
LogLevel
|
|
|
|
GetLogLevel();
|
|
|
|
|
2018-12-12 01:47:29 +00:00
|
|
|
/** internal */
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename... TArgs>
|
2020-01-21 17:31:48 +00:00
|
|
|
inline static void
|
2018-12-12 01:47:29 +00:00
|
|
|
_Log(LogLevel lvl, const char* fname, int lineno, TArgs&&... args) noexcept
|
|
|
|
{
|
2019-04-11 12:58:23 +00:00
|
|
|
auto& log = LogContext::Instance();
|
2020-06-08 13:07:49 +00:00
|
|
|
if (log.curLevel > lvl || log.logStream == nullptr)
|
2018-12-12 01:47:29 +00:00
|
|
|
return;
|
|
|
|
std::stringstream ss;
|
2020-04-07 18:38:56 +00:00
|
|
|
LogAppend(ss, std::forward<TArgs>(args)...);
|
2019-06-13 13:26:34 +00:00
|
|
|
log.logStream->AppendLog(lvl, fname, lineno, log.nodeName, ss.str());
|
2019-04-11 12:58:23 +00:00
|
|
|
}
|
2018-12-12 01:47:29 +00:00
|
|
|
} // namespace llarp
|
|
|
|
|
2019-11-14 21:56:01 +00:00
|
|
|
#define LogTrace(...) _Log(llarp::eLogTrace, LOG_TAG, __LINE__, __VA_ARGS__)
|
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__)
|
2019-11-14 21:56:01 +00:00
|
|
|
|
|
|
|
#define LogTraceTag(tag, ...) _Log(llarp::eLogTrace, tag, __LINE__, __VA_ARGS__)
|
2019-02-11 00:02:20 +00:00
|
|
|
#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
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
#define LogTraceExplicit(tag, line, ...) _Log(llarp::eLogTrace, tag, line, __VA_ARGS__)
|
|
|
|
#define LogDebugExplicit(tag, line, ...) _Log(llarp::eLogDebug, tag, line, __VA_ARGS__)
|
|
|
|
#define LogInfoExplicit(tag, line, ...) _Log(llarp::eLogInfo, tag, line __VA_ARGS__)
|
|
|
|
#define LogWarnExplicit(tag, line, ...) _Log(llarp::eLogWarn, tag, line, __VA_ARGS__)
|
|
|
|
#define LogErrorExplicit(tag, line, ...) _Log(llarp::eLogError, tag, line, __VA_ARGS__)
|
2019-11-14 21:56:01 +00:00
|
|
|
|
2018-12-12 01:47:29 +00:00
|
|
|
#ifndef LOG_TAG
|
|
|
|
#define LOG_TAG "default"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|