hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
logger.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_LOGGER_LOGGER_HPP
7 #define IROHA_LOGGER_LOGGER_HPP
8 
9 #include "logger/logger_fwd.hpp"
10 
11 #include <string>
12 
13 #include <fmt/core.h>
14 #include <fmt/format.h>
15 // Windows includes transitively included by format.h define interface as
16 // struct, leading to compilation issues
17 #undef interface
18 
19 namespace fmt {
22  template <typename T>
23  struct formatter<
24  T,
25  std::enable_if_t<std::is_same<decltype(std::declval<T>().toString()),
26  std::string>::value,
27  char>> {
28  // The following functions are not defined intentionally.
29  template <typename ParseContext>
30  auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
31  return ctx.begin();
32  }
33 
34  template <typename FormatContext>
35  auto format(const T &val, FormatContext &ctx) -> decltype(ctx.out()) {
36  return format_to(ctx.out(), "{}", val.toString());
37  }
38  };
39 } // namespace fmt
40 
41 namespace logger {
42 
43  enum class LogLevel;
44 
45  extern const LogLevel kDefaultLogLevel;
46 
48  enum class LogLevel {
49  kTrace,
50  kDebug,
51  kInfo,
52  kWarn,
53  kError,
54  kCritical,
55  };
56 
57  class Logger {
58  public:
59  using Level = LogLevel;
60 
61  virtual ~Logger() = default;
62 
63  // --- Logging functions ---
64 
65  template <typename... Args>
66  void trace(const std::string &format, const Args &... args) const {
67  log(LogLevel::kTrace, format, args...);
68  }
69 
70  template <typename... Args>
71  void debug(const std::string &format, const Args &... args) const {
72  log(LogLevel::kDebug, format, args...);
73  }
74 
75  template <typename... Args>
76  void info(const std::string &format, const Args &... args) const {
77  log(LogLevel::kInfo, format, args...);
78  }
79 
80  template <typename... Args>
81  void warn(const std::string &format, const Args &... args) const {
82  log(LogLevel::kWarn, format, args...);
83  }
84 
85  template <typename... Args>
86  void error(const std::string &format, const Args &... args) const {
87  log(LogLevel::kError, format, args...);
88  }
89 
90  template <typename... Args>
91  void critical(const std::string &format, const Args &... args) const {
92  log(LogLevel::kCritical, format, args...);
93  }
94 
95  template <typename... Args>
96  void log(Level level,
97  const std::string &format,
98  const Args &... args) const {
99  if (shouldLog(level)) {
100  try {
101  logInternal(level, fmt::format(format, args...));
102  } catch (const std::exception &error) {
103  std::string error_msg("Exception was thrown while logging: ");
104  logInternal(LogLevel::kError, error_msg.append(error.what()));
105  }
106  }
107  }
108 
109  protected:
110  virtual void logInternal(Level level, const std::string &s) const = 0;
111 
114  virtual bool shouldLog(Level level) const = 0;
115  };
116 
122  std::string boolRepr(bool value);
123 
124 } // namespace logger
125 
126 #endif // IROHA_LOGGER_LOGGER_HPP
void trace(const std::string &format, const Args &... args) const
Definition: logger.hpp:66
Definition: peer.hpp:48
void info(const std::string &format, const Args &... args) const
Definition: logger.hpp:76
void critical(const std::string &format, const Args &... args) const
Definition: logger.hpp:91
void error(const std::string &format, const Args &... args) const
Definition: logger.hpp:86
void warn(const std::string &format, const Args &... args) const
Definition: logger.hpp:81
Definition: dummy_logger.hpp:11
void log(Level level, const std::string &format, const Args &... args) const
Definition: logger.hpp:96
auto format(const T &val, FormatContext &ctx) -> decltype(ctx.out())
Definition: logger.hpp:35
const LogLevel kDefaultLogLevel
Definition: logger.cpp:10
Definition: logger.hpp:57
std::string boolRepr(bool value)
Definition: logger.cpp:12
Definition: logger.hpp:19
void debug(const std::string &format, const Args &... args) const
Definition: logger.hpp:71
LogLevel
Log levels.
Definition: logger.hpp:48