1 #pragma once 2 3 #include "utility.hpp" 4 5 #include <nlohmann/json.hpp> 6 #include <xyz/openbmc_project/Logging/Entry/server.hpp> 7 8 #include <filesystem> 9 #include <string> 10 #include <tuple> 11 12 namespace phosphor::fan::monitor 13 { 14 15 /** 16 * @class FFDCFile 17 * 18 * This class holds a file that is used for event log FFDC 19 * which needs a file descriptor as input. The file is 20 * deleted upon destruction. 21 */ 22 class FFDCFile 23 { 24 public: 25 FFDCFile() = delete; 26 FFDCFile(const FFDCFile&) = delete; 27 FFDCFile& operator=(const FFDCFile&) = delete; 28 FFDCFile(FFDCFile&&) = delete; 29 FFDCFile& operator=(FFDCFile&&) = delete; 30 31 /** 32 * @brief Constructor 33 * 34 * Opens the file and saves the descriptor 35 * 36 * @param[in] name - The filename 37 */ 38 explicit FFDCFile(const std::filesystem::path& name); 39 40 /** 41 * @brief Destructor - Deletes the file 42 */ 43 ~FFDCFile() 44 { 45 std::filesystem::remove(_name); 46 } 47 48 /** 49 * @brief Returns the file descriptor 50 * 51 * @return int - The descriptor 52 */ 53 int fd() 54 { 55 return _fd(); 56 } 57 58 private: 59 /** 60 * @brief The file descriptor holder 61 */ 62 util::FileDescriptor _fd; 63 64 /** 65 * @brief The filename 66 */ 67 const std::filesystem::path _name; 68 }; 69 70 /** 71 * @class FanError 72 * 73 * This class represents a fan error. It has a commit() interface 74 * that will create the event log with certain FFDC. 75 */ 76 class FanError 77 { 78 public: 79 FanError() = delete; 80 ~FanError() = default; 81 FanError(const FanError&) = delete; 82 FanError& operator=(const FanError&) = delete; 83 FanError(FanError&&) = delete; 84 FanError& operator=(FanError&&) = delete; 85 86 /** 87 * @brief Constructor 88 * 89 * @param[in] error - The error name, like 90 * xyz.openbmc_project.Fan.Error.Fault 91 * @param[in] fan - The failing fan's inventory path 92 * @param[in] sensor - The failing sensor's inventory path. Can be empty 93 * if the error is for the FRU and not the sensor. 94 * @param[in] severity - The severity of the error 95 */ 96 FanError(const std::string& error, const std::string& fan, 97 const std::string& sensor, 98 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level 99 severity) : 100 _errorName(error), 101 _fanName(fan), _sensorName(sensor), 102 _severity( 103 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage( 104 severity)) 105 {} 106 107 /** 108 * @brief Commits the error by calling the D-Bus method to create 109 * the event log. 110 * 111 * The FFDC is passed in here so that if an error is committed 112 * more than once it can have up to date FFDC. 113 * 114 * @param[in] jsonFFDC - Free form JSON data that should be sent in as 115 * FFDC. 116 * @param[in] isPowerOffError - If this is committed at the time of the 117 * power off. 118 */ 119 void commit(const nlohmann::json& jsonFFDC, bool isPowerOffError = false); 120 121 private: 122 /** 123 * @brief Returns an FFDCFile holding the Logger contents 124 * 125 * @return std::unique_ptr<FFDCFile> - The file object 126 */ 127 std::unique_ptr<FFDCFile> makeLogFFDCFile(); 128 129 /** 130 * @brief Returns an FFDCFile holding the contents of the JSON FFDC 131 * 132 * @param[in] ffdcData - The JSON data to write to a file 133 * 134 * @return std::unique_ptr<FFDCFile> - The file object 135 */ 136 std::unique_ptr<FFDCFile> makeJsonFFDCFile(const nlohmann::json& ffdcData); 137 138 /** 139 * @brief Create and returns the AdditionalData property to use for the 140 * event log. 141 * 142 * @param[in] isPowerOffError - If this is committed at the time of the 143 * power off. 144 * @return map<string, string> - The AdditionalData contents 145 */ 146 std::map<std::string, std::string> getAdditionalData(bool isPowerOffError); 147 148 /** 149 * @brief The error name (The event log's 'Message' property) 150 */ 151 const std::string _errorName; 152 153 /** 154 * @brief The inventory name of the failing fan 155 */ 156 const std::string _fanName; 157 158 /** 159 * @brief The inventory name of the failing sensor, if there is one. 160 */ 161 const std::string _sensorName; 162 163 /** 164 * @brief The severity of the event log. This is the string 165 * representation of the Entry::Level property. 166 */ 167 const std::string _severity; 168 }; 169 170 } // namespace phosphor::fan::monitor 171