1 #pragma once 2 3 #include "constants.hpp" 4 #include "types.hpp" 5 6 #include <iostream> 7 #include <optional> 8 #include <string> 9 #include <unordered_map> 10 11 namespace vpd 12 { 13 /** 14 * @brief Class for logging events. 15 * 16 * Class handles logging PEL under 'logging' service. 17 * Provide separate async API's for calling out inventory_path, device_path and 18 * i2c bus. 19 */ 20 class EventLogger 21 { 22 public: 23 /** 24 * @brief API to get Error type. 25 * 26 * @param[in] i_exception - Exception object. 27 * 28 * @return Error type set for the exception. 29 * types::ErrorType::InternalFailure otherwise. 30 */ 31 static types::ErrorType getErrorType(const std::exception& i_exception); 32 33 /** 34 * @brief API to get Error msg. 35 * 36 * @param[in] i_exception - Exception object. 37 * 38 * @return Error msg set for the specific exception. Default error msg 39 * otherwise. 40 */ 41 static std::string getErrorMsg(const std::exception& i_exception); 42 43 /** 44 * @brief API to get string representation of a Error type enum. 45 * 46 * @param[in] i_exception - Exception object. 47 * 48 * @return Error msg set for the specific error type. Default error msg 49 * otherwise. 50 */ 51 static std::string getErrorTypeString( 52 const types::ErrorType& i_errorType) noexcept; 53 54 /** 55 * @brief An API to create a PEL with inventory path callout. 56 * 57 * This API calls an async method to create PEL, and also handles inventory 58 * path callout. 59 * 60 * Note: If inventory path callout info is not provided, it will create a 61 * PEL without any callout. Currently only one callout is handled in this 62 * API. 63 * 64 * @todo: Symbolic FRU and procedure callout needs to be handled in this 65 * API. 66 * 67 * @param[in] i_errorType - Enum to map with event message name. 68 * @param[in] i_severity - Severity of the event. 69 * @param[in] i_callouts - Callout information, list of tuple having 70 * inventory path and priority as input [optional]. 71 * @param[in] i_fileName - File name. 72 * @param[in] i_funcName - Function name. 73 * @param[in] i_internalRc - Internal return code. 74 * @param[in] i_description - Error description. 75 * @param[in] i_userData1 - Additional user data [optional]. 76 * @param[in] i_userData2 - Additional user data [optional]. 77 * @param[in] i_symFru - Symblolic FRU callout data [optional]. 78 * @param[in] i_procedure - Procedure callout data [optional]. 79 * 80 * @throw exception in case of error. 81 */ 82 static void createAsyncPelWithInventoryCallout( 83 const types::ErrorType& i_errorType, 84 const types::SeverityType& i_severity, 85 const std::vector<types::InventoryCalloutData>& i_callouts, 86 const std::string& i_fileName, const std::string& i_funcName, 87 const uint8_t i_internalRc, const std::string& i_description, 88 const std::optional<std::string> i_userData1, 89 const std::optional<std::string> i_userData2, 90 const std::optional<std::string> i_symFru, 91 const std::optional<std::string> i_procedure); 92 93 /** 94 * @brief An API to create a PEL with device path callout. 95 * 96 * @param[in] i_errorType - Enum to map with event message name. 97 * @param[in] i_severity - Severity of the event. 98 * @param[in] i_callouts - Callout information, list of tuple having device 99 * path and error number as input. 100 * @param[in] i_fileName - File name. 101 * @param[in] i_funcName - Function name. 102 * @param[in] i_internalRc - Internal return code. 103 * @param[in] i_userData1 - Additional user data [optional]. 104 * @param[in] i_userData2 - Additional user data [optional]. 105 */ 106 static void createAsyncPelWithI2cDeviceCallout( 107 const types::ErrorType i_errorType, 108 const types::SeverityType i_severity, 109 const std::vector<types::DeviceCalloutData>& i_callouts, 110 const std::string& i_fileName, const std::string& i_funcName, 111 const uint8_t i_internalRc, 112 const std::optional<std::pair<std::string, std::string>> i_userData1, 113 const std::optional<std::pair<std::string, std::string>> i_userData2); 114 115 /** 116 * @brief An API to create a PEL with I2c bus callout. 117 * 118 * @param[in] i_errorType - Enum to map with event message name. 119 * @param[in] i_severity - Severity of the event. 120 * @param[in] i_callouts - Callout information, list of tuple having i2c 121 * bus, i2c address and error number as input. 122 * @param[in] i_fileName - File name. 123 * @param[in] i_funcName - Function name. 124 * @param[in] i_internalRc - Internal return code. 125 * @param[in] i_userData1 - Additional user data [optional]. 126 * @param[in] i_userData2 - Additional user data [optional]. 127 */ 128 static void createAsyncPelWithI2cBusCallout( 129 const types::ErrorType i_errorType, 130 const types::SeverityType i_severity, 131 const std::vector<types::I2cBusCalloutData>& i_callouts, 132 const std::string& i_fileName, const std::string& i_funcName, 133 const uint8_t i_internalRc, 134 const std::optional<std::pair<std::string, std::string>> i_userData1, 135 const std::optional<std::pair<std::string, std::string>> i_userData2); 136 137 /** 138 * @brief An API to create a PEL. 139 * 140 * @param[in] i_errorType - Enum to map with event message name. 141 * @param[in] i_severity - Severity of the event. 142 * @param[in] i_fileName - File name. 143 * @param[in] i_funcName - Function name. 144 * @param[in] i_internalRc - Internal return code. 145 * @param[in] i_description - Error description. 146 * @param[in] i_userData1 - Additional user data [optional]. 147 * @param[in] i_userData2 - Additional user data [optional]. 148 * @param[in] i_symFru - Symblolic FRU callout data [optional]. 149 * @param[in] i_procedure - Procedure callout data [optional]. 150 * 151 * @todo: Symbolic FRU and procedure callout needs to be handled in this 152 * API. 153 */ 154 static void createAsyncPel( 155 const types::ErrorType& i_errorType, 156 const types::SeverityType& i_severity, const std::string& i_fileName, 157 const std::string& i_funcName, const uint8_t i_internalRc, 158 const std::string& i_description, 159 const std::optional<std::string> i_userData1, 160 const std::optional<std::string> i_userData2, 161 const std::optional<std::string> i_symFru, 162 const std::optional<std::string> i_procedure); 163 164 /** 165 * @brief An API to create PEL. 166 * 167 * This API makes synchronous call to phosphor-logging Create method. 168 * 169 * @param[in] i_errorType - Enum to map with event message name. 170 * @param[in] i_severity - Severity of the event. 171 * @param[in] i_fileName - File name. 172 * @param[in] i_funcName - Function name. 173 * @param[in] i_internalRc - Internal return code. 174 * @param[in] i_description - Error description. 175 * @param[in] i_userData1 - Additional user data [optional]. 176 * @param[in] i_userData2 - Additional user data [optional]. 177 * @param[in] i_symFru - Symblolic FRU callout data [optional].s 178 * @param[in] i_procedure - Procedure callout data [optional]. 179 * 180 * @todo: Symbolic FRU and procedure callout needs to be handled in this 181 * API. 182 */ 183 static void createSyncPel( 184 const types::ErrorType& i_errorType, 185 const types::SeverityType& i_severity, const std::string& i_fileName, 186 const std::string& i_funcName, const uint8_t i_internalRc, 187 const std::string& i_description, 188 const std::optional<std::string> i_userData1, 189 const std::optional<std::string> i_userData2, 190 const std::optional<std::string> i_symFru, 191 const std::optional<std::string> i_procedure); 192 193 private: 194 /** 195 * @brief API to get error info based on the exception. 196 * 197 * @param[in] i_exception - Exception object. 198 * 199 * @return - Valid ExceptionDataMap on success, otherwise map having default 200 * value. 201 */ 202 static types::ExceptionDataMap getExceptionData( 203 const std::exception& i_exception); 204 205 static const std::unordered_map<types::SeverityType, std::string> 206 m_severityMap; 207 static const std::unordered_map<types::ErrorType, std::string> 208 m_errorMsgMap; 209 static const std::unordered_map<types::CalloutPriority, std::string> 210 m_priorityMap; 211 }; 212 } // namespace vpd 213