Kagome
Polkadot Runtime Engine in C++17
logger.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_LOGGER_HPP
7 #define KAGOME_LOGGER_HPP
8 
9 #include <sstream>
10 
11 #include <boost/assert.hpp>
12 #include <optional>
13 #include <soralog/level.hpp>
14 #include <soralog/logger.hpp>
15 #include <soralog/logging_system.hpp>
16 #include <soralog/macro.hpp>
17 
18 #include "common/hexutil.hpp"
19 
20 namespace kagome::log {
21 
23  using Logger = std::shared_ptr<soralog::Logger>;
24 
25  enum class Error : uint8_t { WRONG_LEVEL = 1, WRONG_GROUP, WRONG_LOGGER };
26 
27  outcome::result<Level> str2lvl(std::string_view str);
28 
29  void setLoggingSystem(std::shared_ptr<soralog::LoggingSystem> logging_system);
30 
31  void tuneLoggingSystem(const std::vector<std::string> &cfg);
32 
33  static const std::string defaultGroupName("kagome");
34 
35  [[nodiscard]] Logger createLogger(const std::string &tag);
36 
37  [[nodiscard]] Logger createLogger(const std::string &tag,
38  const std::string &group);
39 
40  [[nodiscard]] Logger createLogger(const std::string &tag,
41  const std::string &group,
42  Level level);
43 
44  bool setLevelOfGroup(const std::string &group_name, Level level);
45  bool resetLevelOfGroup(const std::string &group_name);
46 
47  bool setLevelOfLogger(const std::string &logger_name, Level level);
48  bool resetLevelOfLogger(const std::string &logger_name);
49 
50  template <typename T, typename Ret>
51  Ret format_arg(T const &t) {
52  return static_cast<Ret>(t);
53  }
54 
55  template <typename T>
56  auto format_arg(T const *t) {
57  return fmt::ptr(t);
58  }
59  inline std::string_view format_arg(std::string_view s) {
60  return s;
61  }
62 
63  template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
64  T const &format_arg(T const &arg) {
65  return arg;
66  }
67 
68  template <typename T,
69  typename = std::enable_if_t<std::is_same_v<
70  std::decay_t<decltype(*std::declval<T>().begin())>,
71  uint8_t>>>
72  std::string format_arg(T const &buffer) {
73  if (buffer.size() == 0) return "";
74  std::string res;
75  if (std::all_of(buffer.begin(), buffer.end(), isalnum)) {
76  res.resize(buffer.size());
77  std::copy_n(buffer.begin(), buffer.size(), res.begin());
78  } else {
79  res = ::kagome::common::hex_lower(buffer);
80  }
81  if (res.size() > 256) {
82  return res.substr(0, 256) + "...";
83  }
84  return res;
85  }
86 
87  template <typename T>
88  auto format_arg(std::reference_wrapper<T> t) {
89  return format_arg(t.get());
90  }
91 
92  template <typename T>
93  auto format_arg(const std::optional<T> &t) {
94  return t ? format_arg(t.value()) : "none";
95  }
96 
97  template <typename Ret, typename... Args>
99  std::string_view func_name,
100  Ret &&ret,
101  Args &&...args) {
102  if (sizeof...(args) > 0) {
103  std::string ss;
104  (fmt::format_to(std::back_inserter(ss),
105  "{}, ",
106  format_arg(std::forward<Args>(args))),
107  ...);
108  logger->trace("call '{}', args: {}-> ret: {}",
109  func_name,
110  ss,
111  format_arg(std::forward<Ret>(ret)));
112  } else {
113  logger->trace("call '{}' -> ret: {}",
114  func_name,
115  format_arg(std::forward<Ret>(ret)));
116  }
117  }
118 
119  template <typename... Args>
121  std::string_view func_name,
122  Args &&...args) {
123  if (sizeof...(args) > 0) {
124  std::string ss;
125  (fmt::format_to(std::back_inserter(ss),
126  "{}, ",
127  format_arg(std::forward<Args>(args))),
128  ...);
129  logger->trace("call '{}', args: {}", func_name, ss);
130  } else {
131  logger->trace("call '{}'", func_name);
132  }
133  }
134 
135 #ifdef NDEBUG
136 
137 #define SL_TRACE_FUNC_CALL(logger, ret, ...)
138 #define SL_TRACE_VOID_FUNC_CALL(logger, ...)
139 
140 #else
141 
142 #define SL_TRACE_FUNC_CALL(logger, ret, ...) \
143  ::kagome::log::trace_function_call( \
144  (logger), __FUNCTION__, (ret), ##__VA_ARGS__)
145 
146 #define SL_TRACE_VOID_FUNC_CALL(logger, ...) \
147  ::kagome::log::trace_void_function_call((logger), __FUNCTION__, ##__VA_ARGS__)
148 
149 #endif
150 
151 } // namespace kagome::log
152 
154 
155 #endif // KAGOME_LOGGER_HPP
bool resetLevelOfGroup(const std::string &group_name)
Definition: logger.cpp:136
void trace_function_call(Logger const &logger, std::string_view func_name, Ret &&ret, Args &&...args)
Definition: logger.hpp:98
void tuneLoggingSystem(const std::vector< std::string > &cfg)
Definition: logger.cpp:71
std::string hex_lower(const gsl::span< const uint8_t > bytes) noexcept
Converts bytes to hex representation.
Definition: hexutil.cpp:52
bool resetLevelOfLogger(const std::string &logger_name)
Definition: logger.cpp:145
soralog::Level Level
Definition: logger.hpp:22
bool setLevelOfLogger(const std::string &logger_name, Level level)
Definition: logger.cpp:141
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
void setLoggingSystem(std::shared_ptr< soralog::LoggingSystem > logging_system)
Definition: logger.cpp:63
void trace_void_function_call(Logger const &logger, std::string_view func_name, Args &&...args)
Definition: logger.hpp:120
Ret format_arg(T const &t)
Definition: logger.hpp:51
OUTCOME_HPP_DECLARE_ERROR(kagome::log, Error)
static const std::string defaultGroupName("kagome")
outcome::result< Level > str2lvl(std::string_view str)
Definition: logger.cpp:41
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
bool setLevelOfGroup(const std::string &group_name, Level level)
Definition: logger.cpp:132