Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 1 | #pragma once |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 2 | #include "xyz/openbmc_project/Logging/Entry/server.hpp" |
| 3 | |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 5 | #include <sdbusplus/exception.hpp> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 6 | #include <tuple> |
| 7 | #include <utility> |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 8 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 9 | namespace phosphor |
| 10 | { |
| 11 | |
| 12 | namespace logging |
| 13 | { |
| 14 | |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 15 | using namespace sdbusplus::xyz::openbmc_project::Logging::server; |
| 16 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 17 | /** |
| 18 | * @brief Structure used by callers to indicate they want to use the last value |
| 19 | * put in the journal for input parameter. |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 20 | */ |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 21 | template <typename T> |
| 22 | struct prev_entry |
| 23 | { |
| 24 | using type = T; |
| 25 | }; |
| 26 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 27 | namespace details |
| 28 | { |
| 29 | /** |
| 30 | * @brief Used to return the generated tuple for the error code meta data |
| 31 | * |
| 32 | * The prev_entry (above) and deduce_entry_type structures below are used |
| 33 | * to verify at compile time the required parameters have been passed to |
| 34 | * the elog interface and then to forward on the appropriate tuple to the |
| 35 | * log interface. |
| 36 | */ |
| 37 | template <typename T> |
| 38 | struct deduce_entry_type |
| 39 | { |
| 40 | |
| 41 | using type = T; |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 42 | auto get() |
| 43 | { |
| 44 | return value._entry; |
| 45 | } |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 46 | |
| 47 | T value; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * @brief Used to return an empty tuple for prev_entry parameters |
| 52 | * |
| 53 | * This is done so we can still call the log() interface with the variable |
| 54 | * arg parameters to elog. The log() interface will simply ignore the empty |
| 55 | * tuples which is what we want for prev_entry parameters. |
| 56 | */ |
| 57 | template <typename T> |
| 58 | struct deduce_entry_type<prev_entry<T>> |
| 59 | { |
| 60 | using type = T; |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 61 | auto get() |
| 62 | { |
| 63 | return std::make_tuple(); |
| 64 | } |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 65 | |
| 66 | prev_entry<T> value; |
| 67 | }; |
| 68 | |
| 69 | /** |
| 70 | * @brief Typedef for above structure usage |
| 71 | */ |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 72 | template <typename T> |
| 73 | using deduce_entry_type_t = typename deduce_entry_type<T>::type; |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 74 | |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 75 | /** |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 76 | * @brief Used to map an sdbusplus error to a phosphor-logging error type |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 77 | * |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 78 | * Users log errors via the sdbusplus error name, and the execption that's |
| 79 | * thrown is the corresponding sdbusplus exception. However, there's a need |
| 80 | * to map the sdbusplus error name to the phosphor-logging error name, in order |
| 81 | * to verify the error metadata at compile-time. |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 82 | */ |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 83 | template <typename T> |
| 84 | struct map_exception_type |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 85 | { |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 86 | using type = T; |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 89 | /** |
| 90 | * @brief Typedef for above structure usage |
| 91 | */ |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 92 | template <typename T> |
| 93 | using map_exception_type_t = typename map_exception_type<T>::type; |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 94 | |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 95 | /** @fn commit() |
| 96 | * @brief Create an error log entry based on journal |
| 97 | * entry with a specified exception name |
| 98 | * @param[in] name - name of the error exception |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 99 | * |
| 100 | * @return The entry ID |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 101 | */ |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 102 | uint32_t commit(const char* name); |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 103 | |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 104 | /** @fn commit() - override that accepts error level |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 105 | * |
| 106 | * @return The entry ID |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 107 | */ |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 108 | uint32_t commit(const char* name, Entry::Level level); |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 109 | |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 110 | } // namespace details |
| 111 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 112 | /** @fn commit() |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 113 | * \deprecated use commit<T>() |
| 114 | * @brief Create an error log entry based on journal |
| 115 | * entry with a specified MSG_ID |
| 116 | * @param[in] name - name of the error exception |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 117 | * |
| 118 | * @return The entry ID |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 119 | */ |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 120 | uint32_t commit(std::string&& name); |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 121 | |
| 122 | /** @fn commit() |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 123 | * @brief Create an error log entry based on journal |
| 124 | * entry with a specified MSG_ID |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 125 | * |
| 126 | * @return The entry ID |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 127 | */ |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 128 | template <typename T> |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 129 | uint32_t commit() |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 130 | { |
| 131 | // Validate if the exception is derived from sdbusplus::exception. |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 132 | static_assert(std::is_base_of<sdbusplus::exception::exception, T>::value, |
| 133 | "T must be a descendant of sdbusplus::exception::exception"); |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 134 | return details::commit(T::errName); |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 137 | /** @fn commit() |
| 138 | * @brief Create an error log entry based on journal |
| 139 | * entry with a specified MSG_ID. This override accepts error level. |
| 140 | * @param[in] level - level of the error |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 141 | * |
| 142 | * @return The entry ID |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 143 | */ |
| 144 | template <typename T> |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 145 | uint32_t commit(Entry::Level level) |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 146 | { |
| 147 | // Validate if the exception is derived from sdbusplus::exception. |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 148 | static_assert(std::is_base_of<sdbusplus::exception::exception, T>::value, |
| 149 | "T must be a descendant of sdbusplus::exception::exception"); |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 150 | return details::commit(T::errName, level); |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 153 | /** @fn elog() |
| 154 | * @brief Create a journal log entry based on predefined |
| 155 | * error log information |
| 156 | * @tparam T - Error log type |
| 157 | * @param[in] i_args - Metadata fields to be added to the journal entry |
| 158 | */ |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 159 | template <typename T, typename... Args> |
Matt Spinler | d8f3c65 | 2019-07-01 08:39:03 -0500 | [diff] [blame] | 160 | [[noreturn]] void elog(Args... i_args) |
| 161 | { |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 162 | // Validate if the exception is derived from sdbusplus::exception. |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 163 | static_assert(std::is_base_of<sdbusplus::exception::exception, T>::value, |
| 164 | "T must be a descendant of sdbusplus::exception::exception"); |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame] | 165 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 166 | // Validate the caller passed in the required parameters |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 167 | static_assert( |
| 168 | std::is_same<typename details::map_exception_type_t<T>::metadata_types, |
| 169 | std::tuple<details::deduce_entry_type_t<Args>...>>::value, |
| 170 | "You are not passing in required arguments for this error"); |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 171 | |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 172 | log<details::map_exception_type_t<T>::L>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 173 | T::errDesc, details::deduce_entry_type<Args>{i_args}.get()...); |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 174 | |
| 175 | // Now throw an exception for this error |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 176 | throw T(); |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Marri Devender Rao | d41e995 | 2017-04-04 04:09:02 -0500 | [diff] [blame] | 179 | /** @fn report() |
| 180 | * @brief Create a journal log entry based on predefined |
| 181 | * error log information and commit the error |
| 182 | * @tparam T - exception |
| 183 | * @param[in] i_args - Metadata fields to be added to the journal entry |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 184 | * |
| 185 | * @return The entry ID |
Marri Devender Rao | d41e995 | 2017-04-04 04:09:02 -0500 | [diff] [blame] | 186 | */ |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 187 | template <typename T, typename... Args> |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 188 | uint32_t report(Args... i_args) |
Marri Devender Rao | d41e995 | 2017-04-04 04:09:02 -0500 | [diff] [blame] | 189 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 190 | // validate if the exception is derived from sdbusplus::exception. |
| 191 | static_assert(std::is_base_of<sdbusplus::exception::exception, T>::value, |
| 192 | "T must be a descendant of sdbusplus::exception::exception"); |
Marri Devender Rao | d41e995 | 2017-04-04 04:09:02 -0500 | [diff] [blame] | 193 | |
| 194 | // Validate the caller passed in the required parameters |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 195 | static_assert( |
| 196 | std::is_same<typename details::map_exception_type_t<T>::metadata_types, |
| 197 | std::tuple<details::deduce_entry_type_t<Args>...>>::value, |
| 198 | "You are not passing in required arguments for this error"); |
Marri Devender Rao | d41e995 | 2017-04-04 04:09:02 -0500 | [diff] [blame] | 199 | |
| 200 | log<details::map_exception_type_t<T>::L>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 201 | T::errDesc, details::deduce_entry_type<Args>{i_args}.get()...); |
Marri Devender Rao | d41e995 | 2017-04-04 04:09:02 -0500 | [diff] [blame] | 202 | |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 203 | return commit<T>(); |
Marri Devender Rao | d41e995 | 2017-04-04 04:09:02 -0500 | [diff] [blame] | 204 | } |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 205 | |
| 206 | /** @fn report() |
| 207 | * @brief Create a journal log entry based on predefined |
| 208 | * error log information and commit the error. Accepts error |
| 209 | * level. |
| 210 | * @tparam T - exception |
| 211 | * @param[in] level - level of the error |
| 212 | * @param[in] i_args - Metadata fields to be added to the journal entry |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 213 | * |
| 214 | * @return The entry ID |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 215 | */ |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 216 | template <typename T, typename... Args> |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 217 | uint32_t report(Entry::Level level, Args... i_args) |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 218 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 219 | // validate if the exception is derived from sdbusplus::exception. |
| 220 | static_assert(std::is_base_of<sdbusplus::exception::exception, T>::value, |
| 221 | "T must be a descendant of sdbusplus::exception::exception"); |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 222 | |
| 223 | // Validate the caller passed in the required parameters |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 224 | static_assert( |
| 225 | std::is_same<typename details::map_exception_type_t<T>::metadata_types, |
| 226 | std::tuple<details::deduce_entry_type_t<Args>...>>::value, |
| 227 | "You are not passing in required arguments for this error"); |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 228 | |
| 229 | log<details::map_exception_type_t<T>::L>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 230 | T::errDesc, details::deduce_entry_type<Args>{i_args}.get()...); |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 231 | |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 232 | return commit<T>(level); |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 233 | } |
| 234 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 235 | } // namespace logging |
| 236 | |
| 237 | } // namespace phosphor |