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