Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <phosphor-logging/elog-errors.hpp> |
| 3 | #include <phosphor-logging/elog.hpp> |
| 4 | #include "callback.hpp" |
| 5 | #include <sdbusplus/exception.hpp> |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 6 | #include <experimental/tuple> |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace dbus |
| 11 | { |
| 12 | namespace monitoring |
| 13 | { |
| 14 | |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 15 | /** @struct ToString |
| 16 | * @brief Convert numbers to strings |
| 17 | */ |
| 18 | template <typename T> struct ToString |
| 19 | { |
| 20 | static auto op(T&& value) |
| 21 | { |
| 22 | return std::to_string(std::forward<T>(value)); |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | template <> struct ToString<std::string> |
| 27 | { |
| 28 | static auto op(const std::string& value) |
| 29 | { |
| 30 | return value; |
| 31 | } |
| 32 | }; |
| 33 | |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 34 | /** @class ElogBase |
| 35 | * @brief Elog callback implementation. |
| 36 | * |
| 37 | * The elog callback logs the elog and |
| 38 | * elog metadata. |
| 39 | */ |
| 40 | class ElogBase : public Callback |
| 41 | { |
| 42 | public: |
| 43 | ElogBase(const ElogBase&) = delete; |
| 44 | ElogBase(ElogBase&&) = default; |
| 45 | ElogBase& operator=(const ElogBase&) = delete; |
| 46 | ElogBase& operator=(ElogBase&&) = default; |
| 47 | virtual ~ElogBase() = default; |
| 48 | ElogBase() : |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 49 | Callback() {} |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 50 | |
| 51 | /** @brief Callback interface implementation. */ |
Ratan Gupta | a45e086 | 2018-02-21 19:03:13 +0530 | [diff] [blame] | 52 | void operator()(Context ctx) override; |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | /** @brief Delegate type specific calls to subclasses. */ |
| 56 | virtual void log() const = 0; |
| 57 | }; |
| 58 | |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 59 | namespace detail |
| 60 | { |
| 61 | |
| 62 | /** @class CallElog |
| 63 | * @brief Provide explicit call forwarding to phosphor::logging::report. |
| 64 | * |
| 65 | * @tparam T - Error log type |
| 66 | * @tparam Args - Metadata fields types. |
| 67 | */ |
| 68 | template <typename T, typename ...Args> |
| 69 | struct CallElog |
| 70 | { |
| 71 | static void op(Args&& ...args) |
| 72 | { |
| 73 | phosphor::logging::report<T>(std::forward<Args>(args)...); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | } // namespace detail |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 78 | |
| 79 | /** @class Elog |
| 80 | * @brief C++ type specific logic for the elog callback. |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 81 | * The elog callback logs the elog and elog metadata. |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 82 | * |
| 83 | * @tparam T - Error log type |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 84 | * @tparam Args - Metadata fields types. |
| 85 | * @param[in] arguments - Metadata fields to be added to the error log |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 86 | */ |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 87 | template <typename T, typename ...Args> |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 88 | class Elog : public ElogBase |
| 89 | { |
| 90 | public: |
| 91 | Elog(const Elog&) = delete; |
| 92 | Elog(Elog&&) = default; |
| 93 | Elog& operator=(const Elog&) = delete; |
| 94 | Elog& operator=(Elog&&) = default; |
| 95 | ~Elog() = default; |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 96 | Elog(Args&& ... arguments) : |
| 97 | ElogBase(), args(std::forward<Args>(arguments)...) {} |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 98 | |
| 99 | private: |
| 100 | /** @brief elog interface implementation. */ |
| 101 | void log() const override |
| 102 | { |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 103 | std::experimental::apply( |
| 104 | detail::CallElog<T, Args...>::op, |
| 105 | std::tuple_cat(args)); |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 106 | } |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 107 | std::tuple<Args...> args; |
| 108 | |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 109 | }; |
| 110 | |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * @class ElogWithMetadataCapture |
| 114 | * |
| 115 | * @brief A callback class that will save the paths, names, and |
| 116 | * current values of certain properties in the metadata of the |
| 117 | * error log it creates. |
| 118 | * |
| 119 | * The intended use case of this class is to create an error log with |
| 120 | * metadata that includes the property names and values that caused |
| 121 | * the condition to issue this callback. When the condition ran, it had |
| 122 | * set the pass/fail field on each property it checked in the properties' |
| 123 | * entries in the Storage array. This class then looks at those pass/fail |
| 124 | * fields to see which properties to log. |
| 125 | * |
| 126 | * Note that it's OK if different conditions and callbacks share the same |
| 127 | * properties because everything runs serially, so another condition can't |
| 128 | * touch those pass/fail fields until all of the first condition's callbacks |
| 129 | * are done. |
| 130 | * |
| 131 | * This class requires that the error log created only have 1 metadata field, |
| 132 | * and it must take a string. |
| 133 | * |
| 134 | * @tparam errorType - Error log type |
| 135 | * @tparam metadataType - The metadata to use |
| 136 | * @tparam propertyType - The data type of the captured properties |
| 137 | */ |
| 138 | template<typename errorType, |
| 139 | typename metadataType, |
| 140 | typename propertyType> |
| 141 | class ElogWithMetadataCapture : public IndexedCallback |
| 142 | { |
| 143 | public: |
| 144 | ElogWithMetadataCapture() = delete; |
| 145 | ElogWithMetadataCapture(const ElogWithMetadataCapture&) = delete; |
| 146 | ElogWithMetadataCapture(ElogWithMetadataCapture&&) = default; |
| 147 | ElogWithMetadataCapture& operator=( |
| 148 | const ElogWithMetadataCapture&) = delete; |
| 149 | ElogWithMetadataCapture& operator=( |
| 150 | ElogWithMetadataCapture&&) = default; |
| 151 | virtual ~ElogWithMetadataCapture() = default; |
| 152 | explicit ElogWithMetadataCapture( |
| 153 | const PropertyIndex& index) : |
| 154 | IndexedCallback(index) {} |
| 155 | |
| 156 | /** |
| 157 | * @brief Callback interface implementation that |
| 158 | * creates an error log |
| 159 | */ |
| 160 | void operator()(Context ctx) override |
| 161 | { |
| 162 | auto data = captureMetadata(); |
| 163 | |
| 164 | phosphor::logging::report<errorType>( |
| 165 | metadataType(data.c_str())); |
| 166 | } |
| 167 | |
| 168 | private: |
| 169 | |
| 170 | /** |
| 171 | * @brief Builds a metadata string with property information |
| 172 | * |
| 173 | * Finds all of the properties in the index that have |
Matt Spinler | 1abcb06 | 2018-02-26 09:14:31 -0600 | [diff] [blame^] | 174 | * their condition pass/fail fields (get<resultIndex>(storage)) |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 175 | * set to true, and then packs those paths, names, and values |
| 176 | * into a metadata string that looks like: |
| 177 | * |
| 178 | * |path1:name1=value1|path2:name2=value2|... |
| 179 | * |
| 180 | * @return The metadata string |
| 181 | */ |
| 182 | std::string captureMetadata() |
| 183 | { |
| 184 | std::string metadata{'|'}; |
| 185 | |
| 186 | for (const auto& n : index) |
| 187 | { |
Matt Spinler | 1abcb06 | 2018-02-26 09:14:31 -0600 | [diff] [blame^] | 188 | const auto& storage = std::get<storageIndex>(n.second).get(); |
| 189 | const auto& result = std::get<resultIndex>(storage); |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 190 | |
| 191 | if (!result.empty() && any_ns::any_cast<bool>(result)) |
| 192 | { |
Matt Spinler | 1abcb06 | 2018-02-26 09:14:31 -0600 | [diff] [blame^] | 193 | const auto& path = std::get<pathIndex>(n.first).get(); |
| 194 | const auto& propertyName = std::get<propertyIndex>( |
| 195 | n.first).get(); |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 196 | auto value = ToString<propertyType>::op( |
| 197 | any_ns::any_cast<propertyType>( |
Matt Spinler | 1abcb06 | 2018-02-26 09:14:31 -0600 | [diff] [blame^] | 198 | std::get<valueIndex>(storage))); |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 199 | |
| 200 | metadata += path + ":" + propertyName + |
| 201 | '=' + value + '|'; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return metadata; |
| 206 | }; |
| 207 | }; |
| 208 | |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 209 | /** @brief Argument type deduction for constructing Elog instances. |
| 210 | * |
| 211 | * @tparam T - Error log type |
| 212 | * @tparam Args - Metadata fields types. |
| 213 | * @param[in] arguments - Metadata fields to be added to the error log |
| 214 | */ |
| 215 | template <typename T, typename ...Args> |
| 216 | auto makeElog(Args&& ... arguments) |
| 217 | { |
| 218 | return std::make_unique<Elog<T, Args...>>( |
| 219 | std::forward<Args>(arguments)...); |
| 220 | } |
| 221 | |
Gunnar Mills | 9679d43 | 2017-08-03 15:54:43 -0500 | [diff] [blame] | 222 | } // namespace monitoring |
| 223 | } // namespace dbus |
| 224 | } // namespace phosphor |