Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 1 | #pragma once |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 2 | #include <tuple> |
| 3 | #include <utility> |
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> |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 6 | namespace phosphor |
| 7 | { |
| 8 | |
| 9 | namespace logging |
| 10 | { |
| 11 | |
| 12 | /** |
| 13 | * @brief Structure used by callers to indicate they want to use the last value |
| 14 | * put in the journal for input parameter. |
| 15 | */ |
| 16 | template <typename T> |
| 17 | struct prev_entry |
| 18 | { |
| 19 | using type = T; |
| 20 | }; |
| 21 | |
| 22 | |
| 23 | |
| 24 | namespace details |
| 25 | { |
| 26 | /** |
| 27 | * @brief Used to return the generated tuple for the error code meta data |
| 28 | * |
| 29 | * The prev_entry (above) and deduce_entry_type structures below are used |
| 30 | * to verify at compile time the required parameters have been passed to |
| 31 | * the elog interface and then to forward on the appropriate tuple to the |
| 32 | * log interface. |
| 33 | */ |
| 34 | template <typename T> |
| 35 | struct deduce_entry_type |
| 36 | { |
| 37 | |
| 38 | using type = T; |
| 39 | auto get() { return value._entry; } |
| 40 | |
| 41 | T value; |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * @brief Used to return an empty tuple for prev_entry parameters |
| 46 | * |
| 47 | * This is done so we can still call the log() interface with the variable |
| 48 | * arg parameters to elog. The log() interface will simply ignore the empty |
| 49 | * tuples which is what we want for prev_entry parameters. |
| 50 | */ |
| 51 | template <typename T> |
| 52 | struct deduce_entry_type<prev_entry<T>> |
| 53 | { |
| 54 | using type = T; |
| 55 | auto get() { return std::make_tuple(); } |
| 56 | |
| 57 | prev_entry<T> value; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * @brief Typedef for above structure usage |
| 62 | */ |
| 63 | template <typename T> using deduce_entry_type_t = |
| 64 | typename deduce_entry_type<T>::type; |
| 65 | |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 66 | /** |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 67 | * @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] | 68 | * |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 69 | * Users log errors via the sdbusplus error name, and the execption that's |
| 70 | * thrown is the corresponding sdbusplus exception. However, there's a need |
| 71 | * to map the sdbusplus error name to the phosphor-logging error name, in order |
| 72 | * to verify the error metadata at compile-time. |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 73 | */ |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 74 | template <typename T> |
| 75 | struct map_exception_type |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 76 | { |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 77 | using type = T; |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 78 | }; |
| 79 | |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 80 | /** |
| 81 | * @brief Typedef for above structure usage |
| 82 | */ |
| 83 | template <typename T> using map_exception_type_t = |
| 84 | typename map_exception_type<T>::type; |
| 85 | |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame^] | 86 | /** @fn commit() |
| 87 | * @brief Create an error log entry based on journal |
| 88 | * entry with a specified exception name |
| 89 | * @param[in] name - name of the error exception |
| 90 | */ |
| 91 | void commit(const char* name); |
| 92 | |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 93 | } // namespace details |
| 94 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 95 | /** @fn commit() |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame^] | 96 | * \deprecated use commit<T>() |
| 97 | * @brief Create an error log entry based on journal |
| 98 | * entry with a specified MSG_ID |
| 99 | * @param[in] name - name of the error exception |
| 100 | */ |
| 101 | void commit(std::string&& name); |
| 102 | |
| 103 | /** @fn commit() |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 104 | * @brief Create an error log entry based on journal |
| 105 | * entry with a specified MSG_ID |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 106 | */ |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame^] | 107 | template <typename T> |
| 108 | void commit() |
| 109 | { |
| 110 | // Validate if the exception is derived from sdbusplus::exception. |
| 111 | static_assert( |
| 112 | std::is_base_of<sdbusplus::exception::exception, T>::value, |
| 113 | "T must be a descendant of sdbusplus::exception::exception" |
| 114 | ); |
| 115 | details::commit(details::map_exception_type_t<T>::errName); |
| 116 | } |
| 117 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 118 | |
| 119 | /** @fn elog() |
| 120 | * @brief Create a journal log entry based on predefined |
| 121 | * error log information |
| 122 | * @tparam T - Error log type |
| 123 | * @param[in] i_args - Metadata fields to be added to the journal entry |
| 124 | */ |
| 125 | template <typename T, typename ...Args> |
| 126 | void elog(Args... i_args) |
| 127 | { |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame^] | 128 | // Validate if the exception is derived from sdbusplus::exception. |
| 129 | static_assert( |
| 130 | std::is_base_of<sdbusplus::exception::exception, T>::value, |
| 131 | "T must be a descendant of sdbusplus::exception::exception" |
| 132 | ); |
| 133 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 134 | // Validate the caller passed in the required parameters |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 135 | static_assert(std::is_same<typename details:: |
| 136 | map_exception_type_t<T>::metadata_types, |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 137 | std::tuple< |
| 138 | details::deduce_entry_type_t<Args>...>> |
| 139 | ::value, |
| 140 | "You are not passing in required arguments for this error"); |
| 141 | |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 142 | log<details::map_exception_type_t<T>::L>( |
Marri Devender Rao | 7d0a07b | 2017-04-04 03:43:00 -0500 | [diff] [blame^] | 143 | T::errDesc, |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 144 | details::deduce_entry_type<Args>{i_args}.get()...); |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 145 | |
| 146 | // Now throw an exception for this error |
Deepak Kodihalli | 1533110 | 2017-03-09 23:50:43 -0600 | [diff] [blame] | 147 | throw T(); |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | } // namespace logging |
| 151 | |
| 152 | } // namespace phosphor |
| 153 | |