---
title: llarp/util/logging/logger.hpp

---

# llarp/util/logging/logger.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |

## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[llarp::LogContext](Classes/structllarp_1_1LogContext.md)**  |
| struct | **[llarp::LogSilencer](Classes/structllarp_1_1LogSilencer.md)** <br>RAII type to turn logging off logging is suppressed as long as the silencer is in scope.  |
| struct | **[llarp::LogTrace](Classes/structllarp_1_1LogTrace.md)**  |
| struct | **[llarp::LogDebug](Classes/structllarp_1_1LogDebug.md)**  |
| struct | **[llarp::LogInfo](Classes/structllarp_1_1LogInfo.md)**  |
| struct | **[llarp::LogWarn](Classes/structllarp_1_1LogWarn.md)**  |
| struct | **[llarp::LogError](Classes/structllarp_1_1LogError.md)**  |




## Source code

```cpp
#pragma once

#include <memory>
#include <llarp/util/str.hpp>
#include <llarp/util/time.hpp>
#include "logstream.hpp"
#include "logger_internal.hpp"
#include "source_location.hpp"

namespace llarp
{
  enum class LogType
  {
    Unknown = 0,
    File,
    Syslog,
  };
  LogType
  LogTypeFromString(const std::string&);

  struct LogContext
  {
    using IOFunc_t = std::function<void(void)>;

    LogContext();
    LogLevel curLevel = eLogInfo;
    LogLevel startupLevel = eLogInfo;
    LogLevel runtimeLevel = eLogWarn;
    ILogStream_ptr logStream;
    std::string nodeName = "lokinet";

    static LogContext&
    Instance();

    void
    DropToRuntimeLevel();

    void
    RevertRuntimeLevel();

    void
    ImmediateFlush();

    void
    Initialize(
        LogLevel level,
        LogType type,
        const std::string& file,
        const std::string& nickname,
        std::function<void(IOFunc_t)> io);
  };

  struct LogSilencer
  {
    LogSilencer();
    ~LogSilencer();
    explicit LogSilencer(LogContext& ctx);

   private:
    LogContext& parent;
    ILogStream_ptr stream;
  };

  void
  SetLogLevel(LogLevel lvl);

  LogLevel
  GetLogLevel();

  namespace
  {
    template <typename... TArgs>
    inline static void
    _log(LogLevel lvl, const slns::source_location& location, TArgs&&... args) noexcept
    {
      auto& log = LogContext::Instance();
      if (log.curLevel > lvl || log.logStream == nullptr)
        return;
      std::ostringstream ss;
      if constexpr (sizeof...(args) > 0)
        LogAppend(ss, std::forward<TArgs>(args)...);
      log.logStream->AppendLog(
          lvl,
          strip_prefix(location.file_name(), SOURCE_ROOT),
          location.line(),
          log.nodeName,
          ss.str());
    }
  }  // namespace

  template <typename... T>
  struct LogTrace
  {
    LogTrace(T... args, const slns::source_location& location = slns::source_location::current())
    {
#ifdef NDEBUG
      ((void)args, ...);
      (void)location;
#else
      _log(eLogTrace, location, std::forward<T>(args)...);
#endif
    }
  };

  template <typename... T>
  LogTrace(T&&...) -> LogTrace<T...>;

  template <typename... T>
  struct LogDebug
  {
    LogDebug(T... args, const slns::source_location& location = slns::source_location::current())
    {
      _log(eLogDebug, location, std::forward<T>(args)...);
    }
  };

  template <typename... T>
  LogDebug(T&&...) -> LogDebug<T...>;

  template <typename... T>
  struct LogInfo
  {
    LogInfo(T... args, const slns::source_location& location = slns::source_location::current())
    {
      _log(eLogInfo, location, std::forward<T>(args)...);
    }
  };

  template <typename... T>
  LogInfo(T&&...) -> LogInfo<T...>;

  template <typename... T>
  struct LogWarn
  {
    LogWarn(T... args, const slns::source_location& location = slns::source_location::current())
    {
      _log(eLogWarn, location, std::forward<T>(args)...);
    }
  };

  template <typename... T>
  LogWarn(T&&...) -> LogWarn<T...>;

  template <typename... T>
  struct LogError
  {
    LogError(T... args, const slns::source_location& location = slns::source_location::current())
    {
      _log(eLogError, location, std::forward<T>(args)...);
    }
  };

  template <typename... T>
  LogError(T&&...) -> LogError<T...>;

}  // namespace llarp
```


-------------------------------

Updated on 2022-05-20 at 15:34:08 +0000
