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 <memory>
12 #include <numeric> // for std::accumulate
13 #include <string>
14 
17 template <typename StreamType, typename T>
18 auto operator<<(StreamType &os, const T &object)
19  -> decltype(os << object.toString()) {
20  return os << object.toString();
21 }
22 
23 #include <fmt/format.h>
24 #include <fmt/ostream.h>
25 
26 namespace logger {
27 
28  enum class LogLevel;
29 
30  extern const LogLevel kDefaultLogLevel;
31 
33  enum class LogLevel {
34  kTrace,
35  kDebug,
36  kInfo,
37  kWarn,
38  kError,
39  kCritical,
40  };
41 
42  class Logger {
43  public:
44  using Level = LogLevel;
45 
46  virtual ~Logger() = default;
47 
48  // --- Logging functions ---
49 
50  template <typename... Args>
51  void trace(const std::string &format, const Args &... args) const {
52  log(LogLevel::kTrace, format, args...);
53  }
54 
55  template <typename... Args>
56  void debug(const std::string &format, const Args &... args) const {
57  log(LogLevel::kDebug, format, args...);
58  }
59 
60  template <typename... Args>
61  void info(const std::string &format, const Args &... args) const {
62  log(LogLevel::kInfo, format, args...);
63  }
64 
65  template <typename... Args>
66  void warn(const std::string &format, const Args &... args) const {
67  log(LogLevel::kWarn, format, args...);
68  }
69 
70  template <typename... Args>
71  void error(const std::string &format, const Args &... args) const {
72  log(LogLevel::kError, format, args...);
73  }
74 
75  template <typename... Args>
76  void critical(const std::string &format, const Args &... args) const {
77  log(LogLevel::kCritical, format, args...);
78  }
79 
80  template <typename... Args>
81  void log(Level level,
82  const std::string &format,
83  const Args &... args) const {
84  if (shouldLog(level)) {
85  logInternal(level, fmt::format(format, args...));
86  }
87  }
88 
89  protected:
90  virtual void logInternal(Level level, const std::string &s) const = 0;
91 
94  virtual bool shouldLog(Level level) const = 0;
95  };
96 
102  std::string boolRepr(bool value);
103 
110  template <typename T>
111  std::string logBool(T val) {
112  return boolRepr(bool(val));
113  }
114 
123  template <class Collection, class Lambda>
124  std::string to_string(const Collection &collection, Lambda transform) {
125  const std::string left_bracket = "{";
126  const std::string right_bracket = "}";
127  const std::string separator = ", ";
128  auto begin = collection.size() == 0 ? collection.begin()
129  : std::next(collection.begin());
130  auto front =
131  collection.size() == 0 ? std::string{} : transform(*collection.begin());
132 
133  auto result = std::accumulate(begin,
134  collection.end(),
135  front.insert(0, left_bracket),
136  [&](auto &acc, const auto &value) {
137  acc += separator;
138  acc += transform(value);
139  return acc;
140  });
141  return result.append(right_bracket);
142  }
143 
152  template <class Optional, class Lambda>
153  std::string opt_to_string(const Optional &opt, Lambda transform) {
154  const std::string null_value = "nullopt";
155  return opt ? null_value : transform(*opt);
156  }
157 
158 } // namespace logger
159 
160 #endif // IROHA_LOGGER_LOGGER_HPP
void log(Level level, const std::string &format, const Args &...args) const
Definition: logger.hpp:81
auto operator<<(StreamType &os, const T &object) -> decltype(os<< object.toString())
Definition: logger.hpp:18
std::string to_string(const Collection &collection, Lambda transform)
Definition: logger.hpp:124
std::string opt_to_string(const Optional &opt, Lambda transform)
Definition: logger.hpp:153
void warn(const std::string &format, const Args &...args) const
Definition: logger.hpp:66
void trace(const std::string &format, const Args &...args) const
Definition: logger.hpp:51
void debug(const std::string &format, const Args &...args) const
Definition: logger.hpp:56
Definition: dummy_logger.hpp:11
std::string logBool(T val)
Definition: logger.hpp:111
std::string toString(interface::permissions::Role r)
Definition: permissions.cpp:22
const LogLevel kDefaultLogLevel
Definition: logger.cpp:10
void error(const std::string &format, const Args &...args) const
Definition: logger.hpp:71
Definition: logger.hpp:42
std::string boolRepr(bool value)
Definition: logger.cpp:12
void critical(const std::string &format, const Args &...args) const
Definition: logger.hpp:76
void info(const std::string &format, const Args &...args) const
Definition: logger.hpp:61
LogLevel
Log levels.
Definition: logger.hpp:33