hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
logger.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_SPDLOG_LOGGER_LOGGER_HPP
7 #define IROHA_SPDLOG_LOGGER_LOGGER_HPP
8 
9 #include <memory>
10 #include <numeric> // for std::accumulate
11 #include <string>
12 
15 template <typename StreamType, typename T>
16 auto operator<<(StreamType &os, const T &object)
17  -> decltype(os << object.toString()) {
18  return os << object.toString();
19 }
20 
21 #include <spdlog/fmt/ostr.h>
22 #include <spdlog/spdlog.h>
23 
24 namespace logger {
25 
26  using Logger = std::shared_ptr<spdlog::logger>;
27 
28  std::string red(const std::string &string);
29 
30  std::string yellow(const std::string &string);
31 
32  std::string output(const std::string &string);
33 
34  std::string input(const std::string &string);
35 
41  Logger log(const std::string &tag);
42 
49  Logger testLog(const std::string &tag);
50 
56  std::string boolRepr(bool value);
57 
64  template <typename T>
66  return boolRepr(bool(val));
67  }
68 
77  template <class Collection, class Lambda>
78  std::string to_string(const Collection &collection, Lambda transform) {
79  const std::string left_bracket = "{";
80  const std::string right_bracket = "}";
81  const std::string separator = ", ";
82  auto begin = collection.size() == 0 ? collection.begin()
83  : std::next(collection.begin());
84  auto front =
85  collection.size() == 0 ? std::string{} : transform(*collection.begin());
86 
87  auto result = std::accumulate(begin,
88  collection.end(),
89  front.insert(0, left_bracket),
90  [&](auto &acc, const auto &value) {
91  acc += separator;
92  acc += transform(value);
93  return acc;
94  });
95  return result.append(right_bracket);
96  }
97 
106  template <class Optional, class Lambda>
107  std::string opt_to_string(const Optional &opt, Lambda transform) {
108  const std::string null_value = "nullopt";
109  return opt ? null_value : transform(*opt);
110  }
111 
112 } // namespace logger
113 
114 #endif
auto operator<<(StreamType &os, const T &object) -> decltype(os<< object.toString())
Definition: logger.hpp:16
std::string to_string(const Collection &collection, Lambda transform)
Definition: logger.hpp:78
std::string opt_to_string(const Optional &opt, Lambda transform)
Definition: logger.hpp:107
std::string input(const std::string &string)
Definition: logger.cpp:25
string(REPLACE".""/"CMAKE_SWIG_OUTDIR ${SWIG_JAVA_PKG}) else() set(CMAKE_SWIG_FLAGS"") endif() myswig_add_library(irohajava LANGUAGE java SOURCES bindings.i) unset(CMAKE_SWIG_FLAGS) swig_link_libraries(irohajava $
Definition: CMakeLists.txt:54
std::shared_ptr< spdlog::logger > Logger
Definition: logger.hpp:26
std::string output(const std::string &string)
Definition: logger.cpp:21
Definition: logger.cpp:8
Logger log(const std::string &tag)
Definition: logger.cpp:48
std::string logBool(T val)
Definition: logger.hpp:65
std::string toString(interface::permissions::Role r)
Definition: permissions.cpp:22
Logger testLog(const std::string &tag)
Definition: logger.cpp:58
std::string boolRepr(bool value)
Definition: logger.cpp:62
std::string red(const std::string &string)
Definition: logger.cpp:11
std::string yellow(const std::string &string)
Definition: logger.cpp:16