lokinet/llarp/util/logging/logger.hpp

120 lines
3.5 KiB
C++
Raw Normal View History

2019-04-11 12:58:23 +00:00
#ifndef LLARP_UTIL_LOGGER_HPP
#define LLARP_UTIL_LOGGER_HPP
#include <memory>
#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 01:47:29 +00:00
namespace llarp
{
enum class LogType
{
Unknown = 0,
File,
Json,
Syslog,
};
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
{
using IOFunc_t = std::function<void(void)>;
2019-04-11 12:58:23 +00:00
LogContext();
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();
/// 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();
/// 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
/// @param io is a callable that queues work that does io, async
void
Initialize(
LogLevel level,
LogType type,
const std::string& file,
const std::string& nickname,
std::function<void(IOFunc_t)> io);
2018-12-12 01:47:29 +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;
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 */
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;
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
#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__)
#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
#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__)
2018-12-12 01:47:29 +00:00
#ifndef LOG_TAG
#define LOG_TAG "default"
#endif
#endif