Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 1 | #include "config.h" |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 2 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 3 | #include "elog_serialize.hpp" |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 4 | |
| 5 | #include <cereal/archives/binary.hpp> |
| 6 | #include <cereal/types/string.hpp> |
| 7 | #include <cereal/types/tuple.hpp> |
| 8 | #include <cereal/types/vector.hpp> |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/log.hpp> |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 10 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 11 | #include <fstream> |
| 12 | |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 13 | // Register class version |
| 14 | // From cereal documentation; |
| 15 | // "This macro should be placed at global scope" |
Patrick Williams | f40323d | 2021-04-16 15:35:17 -0500 | [diff] [blame] | 16 | CEREAL_CLASS_VERSION(phosphor::logging::Entry, CLASS_VERSION) |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 17 | |
| 18 | namespace phosphor |
| 19 | { |
| 20 | namespace logging |
| 21 | { |
| 22 | |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 23 | /** @brief Function required by Cereal to perform serialization. |
| 24 | * @tparam Archive - Cereal archive type (binary in our case). |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 25 | * @param[in] a - reference to Cereal archive. |
| 26 | * @param[in] e - const reference to error entry. |
| 27 | * @param[in] version - Class version that enables handling |
| 28 | * a serialized data across code levels |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 29 | */ |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 30 | template <class Archive> |
Patrick Williams | f40323d | 2021-04-16 15:35:17 -0500 | [diff] [blame] | 31 | void save(Archive& a, const Entry& e, const std::uint32_t /*version*/) |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 32 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 33 | a(e.id(), e.severity(), e.timestamp(), e.message(), e.additionalData(), |
Vijay Lobo | d354a39 | 2021-06-01 16:21:02 -0500 | [diff] [blame] | 34 | e.associations(), e.resolved(), e.version(), e.updateTimestamp(), |
Vijay Lobo | 593a4c6 | 2021-06-16 14:25:26 -0500 | [diff] [blame] | 35 | e.eventId(), e.resolution()); |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** @brief Function required by Cereal to perform deserialization. |
| 39 | * @tparam Archive - Cereal archive type (binary in our case). |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 40 | * @param[in] a - reference to Cereal archive. |
| 41 | * @param[in] e - reference to error entry. |
| 42 | * @param[in] version - Class version that enables handling |
| 43 | * a serialized data across code levels |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 44 | */ |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 45 | template <class Archive> |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 46 | void load(Archive& a, Entry& e, const std::uint32_t version) |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 47 | { |
Willy Tu | 6ddbf69 | 2023-09-05 10:54:16 -0700 | [diff] [blame^] | 48 | using namespace sdbusplus::server::xyz::openbmc_project::logging; |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 49 | |
| 50 | uint32_t id{}; |
| 51 | Entry::Level severity{}; |
| 52 | uint64_t timestamp{}; |
| 53 | std::string message{}; |
| 54 | std::vector<std::string> additionalData{}; |
| 55 | bool resolved{}; |
| 56 | AssociationList associations{}; |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 57 | std::string fwVersion{}; |
Matt Spinler | 1e71a4d | 2020-03-04 13:40:22 -0600 | [diff] [blame] | 58 | uint64_t updateTimestamp{}; |
Vijay Lobo | d354a39 | 2021-06-01 16:21:02 -0500 | [diff] [blame] | 59 | std::string eventId{}; |
Vijay Lobo | 593a4c6 | 2021-06-16 14:25:26 -0500 | [diff] [blame] | 60 | std::string resolution{}; |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 61 | |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 62 | if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_FWLEVEL)) |
| 63 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 64 | a(id, severity, timestamp, message, additionalData, associations, |
| 65 | resolved); |
Matt Spinler | 1e71a4d | 2020-03-04 13:40:22 -0600 | [diff] [blame] | 66 | updateTimestamp = timestamp; |
| 67 | } |
| 68 | else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_UPDATE_TS)) |
| 69 | { |
| 70 | a(id, severity, timestamp, message, additionalData, associations, |
| 71 | resolved, fwVersion); |
| 72 | updateTimestamp = timestamp; |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 73 | } |
Vijay Lobo | d354a39 | 2021-06-01 16:21:02 -0500 | [diff] [blame] | 74 | else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_EVENTID)) |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 75 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 76 | a(id, severity, timestamp, message, additionalData, associations, |
Matt Spinler | 1e71a4d | 2020-03-04 13:40:22 -0600 | [diff] [blame] | 77 | resolved, fwVersion, updateTimestamp); |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 78 | } |
Vijay Lobo | 593a4c6 | 2021-06-16 14:25:26 -0500 | [diff] [blame] | 79 | else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_RESOLUTION)) |
Vijay Lobo | d354a39 | 2021-06-01 16:21:02 -0500 | [diff] [blame] | 80 | { |
| 81 | a(id, severity, timestamp, message, additionalData, associations, |
| 82 | resolved, fwVersion, updateTimestamp, eventId); |
| 83 | } |
Vijay Lobo | 593a4c6 | 2021-06-16 14:25:26 -0500 | [diff] [blame] | 84 | else |
| 85 | { |
| 86 | a(id, severity, timestamp, message, additionalData, associations, |
| 87 | resolved, fwVersion, updateTimestamp, eventId, resolution); |
| 88 | } |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 89 | |
Matt Spinler | ef952af | 2021-08-19 10:23:05 -0500 | [diff] [blame] | 90 | e.id(id, true); |
| 91 | e.severity(severity, true); |
| 92 | e.timestamp(timestamp, true); |
| 93 | e.message(message, true); |
| 94 | e.additionalData(additionalData, true); |
Willy Tu | 6ddbf69 | 2023-09-05 10:54:16 -0700 | [diff] [blame^] | 95 | e.sdbusplus::server::xyz::openbmc_project::logging::Entry::resolved( |
Matt Spinler | ef952af | 2021-08-19 10:23:05 -0500 | [diff] [blame] | 96 | resolved, true); |
| 97 | e.associations(associations, true); |
| 98 | e.version(fwVersion, true); |
Willy Tu | 6ddbf69 | 2023-09-05 10:54:16 -0700 | [diff] [blame^] | 99 | e.purpose(sdbusplus::server::xyz::openbmc_project::software::Version:: |
Matt Spinler | ef952af | 2021-08-19 10:23:05 -0500 | [diff] [blame] | 100 | VersionPurpose::BMC, |
| 101 | true); |
| 102 | e.updateTimestamp(updateTimestamp, true); |
| 103 | e.eventId(eventId, true); |
| 104 | e.resolution(resolution, true); |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Matt Spinler | fb978da | 2022-01-21 08:42:24 -0600 | [diff] [blame] | 107 | fs::path getEntrySerializePath(uint32_t id, const fs::path& dir) |
| 108 | { |
| 109 | return dir / std::to_string(id); |
| 110 | } |
| 111 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 112 | fs::path serialize(const Entry& e, const fs::path& dir) |
| 113 | { |
Matt Spinler | fb978da | 2022-01-21 08:42:24 -0600 | [diff] [blame] | 114 | auto path = getEntrySerializePath(e.id(), dir); |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 115 | std::ofstream os(path.c_str(), std::ios::binary); |
| 116 | cereal::BinaryOutputArchive oarchive(os); |
| 117 | oarchive(e); |
| 118 | return path; |
| 119 | } |
| 120 | |
| 121 | bool deserialize(const fs::path& path, Entry& e) |
| 122 | { |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 123 | try |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 124 | { |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 125 | if (fs::exists(path)) |
| 126 | { |
| 127 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 128 | cereal::BinaryInputArchive iarchive(is); |
| 129 | iarchive(e); |
| 130 | return true; |
| 131 | } |
| 132 | return false; |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 133 | } |
Matt Spinler | 4a37595 | 2022-07-01 11:15:33 -0500 | [diff] [blame] | 134 | catch (const cereal::Exception& ex) |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 135 | { |
Matt Spinler | 4a37595 | 2022-07-01 11:15:33 -0500 | [diff] [blame] | 136 | log<level::ERR>(ex.what()); |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 137 | fs::remove(path); |
| 138 | return false; |
| 139 | } |
Matt Spinler | 4a37595 | 2022-07-01 11:15:33 -0500 | [diff] [blame] | 140 | catch (const std::length_error& ex) |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 141 | { |
| 142 | // Running into: USCiLab/cereal#192 |
| 143 | // This may be indicating some other issue in the |
| 144 | // way vector may have been used inside the logging. |
| 145 | // possibly associations ??. But handling it here for |
| 146 | // now since we are anyway tossing the log |
| 147 | // TODO: openbmc/phosphor-logging#8 |
Matt Spinler | 4a37595 | 2022-07-01 11:15:33 -0500 | [diff] [blame] | 148 | log<level::ERR>(ex.what()); |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 149 | fs::remove(path); |
| 150 | return false; |
| 151 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | } // namespace logging |
| 155 | } // namespace phosphor |