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 // Windows includes transitively included by format.h define interface as
26 // struct, leading to compilation issues
27 #undef interface
28 
29 namespace logger {
30 
31  enum class LogLevel;
32 
33  extern const LogLevel kDefaultLogLevel;
34 
36  enum class LogLevel {
37  kTrace,
38  kDebug,
39  kInfo,
40  kWarn,
41  kError,
42  kCritical,
43  };
44 
45  class Logger {
46  public:
47  using Level = LogLevel;
48 
49  virtual ~Logger() = default;
50 
51  // --- Logging functions ---
52 
53  template <typename... Args>
54  void trace(const std::string &format, const Args &... args) const {
55  log(LogLevel::kTrace, format, args...);
56  }
57 
58  template <typename... Args>
59  void debug(const std::string &format, const Args &... args) const {
60  log(LogLevel::kDebug, format, args...);
61  }
62 
63  template <typename... Args>
64  void info(const std::string &format, const Args &... args) const {
65  log(LogLevel::kInfo, format, args...);
66  }
67 
68  template <typename... Args>
69  void warn(const std::string &format, const Args &... args) const {
70  log(LogLevel::kWarn, format, args...);
71  }
72 
73  template <typename... Args>
74  void error(const std::string &format, const Args &... args) const {
75  log(LogLevel::kError, format, args...);
76  }
77 
78  template <typename... Args>
79  void critical(const std::string &format, const Args &... args) const {
80  log(LogLevel::kCritical, format, args...);
81  }
82 
83  template <typename... Args>
84  void log(Level level,
85  const std::string &format,
86  const Args &... args) const {
87  if (shouldLog(level)) {
88  try {
89  logInternal(level, fmt::format(format, args...));
90  } catch (const fmt::v5::format_error &error) {
91  std::string error_msg("Exception was thrown while logging: ");
92  logInternal(LogLevel::kError, error_msg.append(error.what()));
93  }
94  }
95  }
96 
97  protected:
98  virtual void logInternal(Level level, const std::string &s) const = 0;
99 
102  virtual bool shouldLog(Level level) const = 0;
103  };
104 
110  std::string boolRepr(bool value);
111 
118  template <typename T>
119  std::string logBool(T val) {
120  return boolRepr(bool(val));
121  }
122 
131  template <class Collection, class Lambda>
132  std::string to_string(const Collection &collection, Lambda transform) {
133  const std::string left_bracket = "{";
134  const std::string right_bracket = "}";
135  const std::string separator = ", ";
136  auto begin = collection.size() == 0 ? collection.begin()
137  : std::next(collection.begin());
138  auto front =
139  collection.size() == 0 ? std::string{} : transform(*collection.begin());
140 
141  auto result = std::accumulate(begin,
142  collection.end(),
143  front.insert(0, left_bracket),
144  [&](auto &acc, const auto &value) {
145  acc += separator;
146  acc += transform(value);
147  return acc;
148  });
149  return result.append(right_bracket);
150  }
151 
160  template <class Optional, class Lambda>
161  std::string opt_to_string(const Optional &opt, Lambda transform) {
162  const std::string null_value = "nullopt";
163  return opt ? null_value : transform(*opt);
164  }
165 
166 } // namespace logger
167 
168 #endif // IROHA_LOGGER_LOGGER_HPP
void log(Level level, const std::string &format, const Args &...args) const
Definition: logger.hpp:84
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:132
std::string opt_to_string(const Optional &opt, Lambda transform)
Definition: logger.hpp:161
void warn(const std::string &format, const Args &...args) const
Definition: logger.hpp:69
void trace(const std::string &format, const Args &...args) const
Definition: logger.hpp:54
void debug(const std::string &format, const Args &...args) const
Definition: logger.hpp:59
Definition: dummy_logger.hpp:11
std::string logBool(T val)
Definition: logger.hpp:119
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:74
Definition: logger.hpp:45
std::string boolRepr(bool value)
Definition: logger.cpp:12
void critical(const std::string &format, const Args &...args) const
Definition: logger.hpp:79
void info(const std::string &format, const Args &...args) const
Definition: logger.hpp:64
LogLevel
Log levels.
Definition: logger.hpp:36