Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
3 : * http://soramitsu.co.jp
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #ifndef IROHA_SPDLOG_LOGGER_LOGGER_HPP
19 : #define IROHA_SPDLOG_LOGGER_LOGGER_HPP
20 :
21 : #include <spdlog/spdlog.h>
22 : #include <memory>
23 : #include <numeric> // for std::accumulate
24 : #include <string>
25 :
26 : namespace logger {
27 :
28 : using Logger = std::shared_ptr<spdlog::logger>;
29 :
30 : std::string red(const std::string &string);
31 :
32 : std::string yellow(const std::string &string);
33 :
34 : std::string output(const std::string &string);
35 :
36 : std::string input(const std::string &string);
37 :
38 : /**
39 : * Provide logger object
40 : * @param tag - tagging name for identifiing logger
41 : * @return logger object
42 : */
43 : Logger log(const std::string &tag);
44 :
45 : /**
46 : * Provide logger for using in test purposes;
47 : * This logger write data only for console
48 : * @param tag - tagging name for identifiing logger
49 : * @return logger object
50 : */
51 : Logger testLog(const std::string &tag);
52 :
53 : /**
54 : * Convert bool value to human readable string repr
55 : * @param value value for transformation
56 : * @return "true" or "false"
57 : */
58 : std::string boolRepr(bool value);
59 :
60 : /**
61 : * Converts object to bool and provides string repr of it
62 : * @tparam T - type of object, T must implement bool operator
63 : * @param val - value for convertation
64 : * @return string representation of bool object
65 : */
66 : template <typename T>
67 : std::string logBool(T val) {
68 1 : return boolRepr(bool(val));
69 : }
70 :
71 : /**
72 : * Function provide string representation of collection
73 : * @tparam Collection - type should implement for semantic
74 : * @tparam Lambda - function that transform argument to string
75 : * @param collection - bunch of objects
76 : * @param transform - function that convert object to string
77 : * @return string repr of collection
78 : */
79 : template <class Collection, class Lambda>
80 : std::string to_string(const Collection &collection, Lambda transform) {
81 717 : const std::string left_bracket = "{";
82 717 : const std::string right_bracket = "}";
83 717 : const std::string separator = ", ";
84 717 : auto begin = collection.size() == 0 ? collection.begin()
85 717 : : std::next(collection.begin());
86 : auto front =
87 717 : collection.size() == 0 ? std::string{} : transform(*collection.begin());
88 :
89 717 : auto result = std::accumulate(begin,
90 717 : collection.end(),
91 717 : front.insert(0, left_bracket),
92 : [&](auto &acc, const auto &value) {
93 25 : acc += separator;
94 25 : acc += transform(value);
95 25 : return acc;
96 0 : });
97 717 : return result.append(right_bracket);
98 717 : }
99 :
100 : /**
101 : * Function provide string representation of optional value
102 : * @tparam Optional - type of optional
103 : * @tparam Lambda - function that consume value type and return std::string
104 : * @param opt - value wrapped by optional
105 : * @param transform - function that transforming value to std::string
106 : * @return string repr of value
107 : */
108 : template <class Optional, class Lambda>
109 : std::string opt_to_string(const Optional &opt, Lambda transform) {
110 3 : const std::string null_value = "nullopt";
111 3 : return opt ? null_value : transform(*opt);
112 3 : }
113 :
114 : } // namespace logger
115 :
116 : #endif
|