Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <tuple> |
| 4 | #include <utility> |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | ebd59fa | 2017-02-06 20:55:39 -0600 | [diff] [blame] | 6 | #include <phosphor-logging/elog-errors.hpp> |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | |
| 11 | namespace logging |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @brief Structure used by callers to indicate they want to use the last value |
| 16 | * put in the journal for input parameter. |
| 17 | */ |
| 18 | template <typename T> |
| 19 | struct prev_entry |
| 20 | { |
| 21 | using type = T; |
| 22 | }; |
| 23 | |
| 24 | |
| 25 | |
| 26 | namespace details |
| 27 | { |
| 28 | /** |
| 29 | * @brief Used to return the generated tuple for the error code meta data |
| 30 | * |
| 31 | * The prev_entry (above) and deduce_entry_type structures below are used |
| 32 | * to verify at compile time the required parameters have been passed to |
| 33 | * the elog interface and then to forward on the appropriate tuple to the |
| 34 | * log interface. |
| 35 | */ |
| 36 | template <typename T> |
| 37 | struct deduce_entry_type |
| 38 | { |
| 39 | |
| 40 | using type = T; |
| 41 | auto get() { return value._entry; } |
| 42 | |
| 43 | T value; |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * @brief Used to return an empty tuple for prev_entry parameters |
| 48 | * |
| 49 | * This is done so we can still call the log() interface with the variable |
| 50 | * arg parameters to elog. The log() interface will simply ignore the empty |
| 51 | * tuples which is what we want for prev_entry parameters. |
| 52 | */ |
| 53 | template <typename T> |
| 54 | struct deduce_entry_type<prev_entry<T>> |
| 55 | { |
| 56 | using type = T; |
| 57 | auto get() { return std::make_tuple(); } |
| 58 | |
| 59 | prev_entry<T> value; |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * @brief Typedef for above structure usage |
| 64 | */ |
| 65 | template <typename T> using deduce_entry_type_t = |
| 66 | typename deduce_entry_type<T>::type; |
| 67 | |
| 68 | } // namespace details |
| 69 | |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 70 | /** |
| 71 | * @brief Error log exception base class |
| 72 | * |
| 73 | * This allows people to capture all error log exceptions if desired |
| 74 | */ |
| 75 | class elogExceptionBase : public std::exception {}; |
| 76 | |
| 77 | /** |
| 78 | * @brief Error log exception class |
| 79 | * |
| 80 | * This is for capturing specific error log exceptions |
| 81 | */ |
| 82 | template <typename T> class elogException : public elogExceptionBase |
| 83 | { |
Adriana Kobylak | 205b113 | 2017-02-06 20:15:29 -0600 | [diff] [blame] | 84 | public: |
| 85 | const char* what() const noexcept override { return T::err_code; } |
| 86 | const char* name() const noexcept { return T::err_code; } |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 89 | /** @fn commit() |
| 90 | * @brief Create an error log entry based on journal |
| 91 | * entry with a specified MSG_ID |
Adriana Kobylak | 205b113 | 2017-02-06 20:15:29 -0600 | [diff] [blame] | 92 | * @param[in] - Exception name |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 93 | */ |
Adriana Kobylak | 205b113 | 2017-02-06 20:15:29 -0600 | [diff] [blame] | 94 | void commit(std::string&& name); |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 95 | |
| 96 | /** @fn elog() |
| 97 | * @brief Create a journal log entry based on predefined |
| 98 | * error log information |
| 99 | * @tparam T - Error log type |
| 100 | * @param[in] i_args - Metadata fields to be added to the journal entry |
| 101 | */ |
| 102 | template <typename T, typename ...Args> |
| 103 | void elog(Args... i_args) |
| 104 | { |
| 105 | // Validate the caller passed in the required parameters |
| 106 | static_assert(std::is_same<typename T::metadata_types, |
| 107 | std::tuple< |
| 108 | details::deduce_entry_type_t<Args>...>> |
| 109 | ::value, |
| 110 | "You are not passing in required arguments for this error"); |
| 111 | |
| 112 | log<T::L>(T::err_msg, |
| 113 | details::deduce_entry_type<Args>{i_args}.get()...); |
Andrew Geissler | 6d910ad | 2016-10-16 20:49:14 -0500 | [diff] [blame] | 114 | |
| 115 | // Now throw an exception for this error |
| 116 | throw elogException<T>(); |
Adriana Kobylak | f855c3e | 2016-09-29 20:48:08 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } // namespace logging |
| 120 | |
| 121 | } // namespace phosphor |
| 122 | |