blob: 5c0f9cbaca5352298cb534ded013ba764cd85b9f [file] [log] [blame]
Patrick Venturef18bf832018-10-26 18:14:00 -07001#include "config.h"
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -05002
Deepak Kodihalli72654f12017-06-12 04:33:29 -05003#include "elog_serialize.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -07004
5#include <cereal/archives/binary.hpp>
Patrick Williams5402fa62024-11-22 14:42:52 -05006#include <cereal/types/map.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -07007#include <cereal/types/string.hpp>
8#include <cereal/types/tuple.hpp>
9#include <cereal/types/vector.hpp>
Arya K Padman5bc26532024-04-10 06:19:25 -050010#include <phosphor-logging/lg2.hpp>
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053011
Patrick Williams2544b412022-10-04 08:41:06 -050012#include <fstream>
13
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053014// Register class version
15// From cereal documentation;
16// "This macro should be placed at global scope"
Patrick Williamsf40323d2021-04-16 15:35:17 -050017CEREAL_CLASS_VERSION(phosphor::logging::Entry, CLASS_VERSION)
Deepak Kodihalli72654f12017-06-12 04:33:29 -050018
19namespace phosphor
20{
21namespace logging
22{
23
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050024/** @brief Function required by Cereal to perform serialization.
25 * @tparam Archive - Cereal archive type (binary in our case).
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053026 * @param[in] a - reference to Cereal archive.
27 * @param[in] e - const reference to error entry.
28 * @param[in] version - Class version that enables handling
29 * a serialized data across code levels
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050030 */
Patrick Venturef18bf832018-10-26 18:14:00 -070031template <class Archive>
Patrick Williamsf40323d2021-04-16 15:35:17 -050032void save(Archive& a, const Entry& e, const std::uint32_t /*version*/)
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050033{
Matt Spinler3d079c62025-03-26 12:57:18 -050034 a(e.id(), e.severity(), e.timestamp(), e.message(),
35 util::additional_data::combine(e.additionalData()), e.associations(),
36 e.resolved(), e.version(), e.updateTimestamp(), e.eventId(),
37 e.resolution());
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050038}
39
40/** @brief Function required by Cereal to perform deserialization.
41 * @tparam Archive - Cereal archive type (binary in our case).
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053042 * @param[in] a - reference to Cereal archive.
43 * @param[in] e - reference to error entry.
44 * @param[in] version - Class version that enables handling
45 * a serialized data across code levels
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050046 */
Patrick Venturef18bf832018-10-26 18:14:00 -070047template <class Archive>
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053048void load(Archive& a, Entry& e, const std::uint32_t version)
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050049{
Willy Tu6ddbf692023-09-05 10:54:16 -070050 using namespace sdbusplus::server::xyz::openbmc_project::logging;
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050051
52 uint32_t id{};
53 Entry::Level severity{};
54 uint64_t timestamp{};
55 std::string message{};
Patrick Williams5402fa62024-11-22 14:42:52 -050056 std::map<std::string, std::string> additionalData{};
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050057 bool resolved{};
58 AssociationList associations{};
Matt Spinler375ac9b2018-05-01 15:20:55 -050059 std::string fwVersion{};
Matt Spinler1e71a4d2020-03-04 13:40:22 -060060 uint64_t updateTimestamp{};
Vijay Lobod354a392021-06-01 16:21:02 -050061 std::string eventId{};
Vijay Lobo593a4c62021-06-16 14:25:26 -050062 std::string resolution{};
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050063
Matt Spinler375ac9b2018-05-01 15:20:55 -050064 if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_FWLEVEL))
65 {
Patrick Williams5402fa62024-11-22 14:42:52 -050066 std::vector<std::string> additionalData_old{};
67 a(id, severity, timestamp, message, additionalData_old, associations,
Patrick Venturef18bf832018-10-26 18:14:00 -070068 resolved);
Matt Spinler1e71a4d2020-03-04 13:40:22 -060069 updateTimestamp = timestamp;
Patrick Williams5402fa62024-11-22 14:42:52 -050070 additionalData = util::additional_data::parse(additionalData_old);
Matt Spinler1e71a4d2020-03-04 13:40:22 -060071 }
72 else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_UPDATE_TS))
73 {
Patrick Williams5402fa62024-11-22 14:42:52 -050074 std::vector<std::string> additionalData_old{};
75 a(id, severity, timestamp, message, additionalData_old, associations,
Matt Spinler1e71a4d2020-03-04 13:40:22 -060076 resolved, fwVersion);
77 updateTimestamp = timestamp;
Patrick Williams5402fa62024-11-22 14:42:52 -050078 additionalData = util::additional_data::parse(additionalData_old);
Matt Spinler375ac9b2018-05-01 15:20:55 -050079 }
Vijay Lobod354a392021-06-01 16:21:02 -050080 else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_EVENTID))
Matt Spinler375ac9b2018-05-01 15:20:55 -050081 {
Patrick Williams5402fa62024-11-22 14:42:52 -050082 std::vector<std::string> additionalData_old{};
83 a(id, severity, timestamp, message, additionalData_old, associations,
Matt Spinler1e71a4d2020-03-04 13:40:22 -060084 resolved, fwVersion, updateTimestamp);
Patrick Williams5402fa62024-11-22 14:42:52 -050085 additionalData = util::additional_data::parse(additionalData_old);
Matt Spinler375ac9b2018-05-01 15:20:55 -050086 }
Vijay Lobo593a4c62021-06-16 14:25:26 -050087 else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_RESOLUTION))
Vijay Lobod354a392021-06-01 16:21:02 -050088 {
Patrick Williams5402fa62024-11-22 14:42:52 -050089 std::vector<std::string> additionalData_old{};
90 a(id, severity, timestamp, message, additionalData_old, associations,
Vijay Lobod354a392021-06-01 16:21:02 -050091 resolved, fwVersion, updateTimestamp, eventId);
Patrick Williams5402fa62024-11-22 14:42:52 -050092 additionalData = util::additional_data::parse(additionalData_old);
93 }
94 else if (version <
95 std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_METADATA_DICT))
96 {
97 std::vector<std::string> additionalData_old{};
98 a(id, severity, timestamp, message, additionalData_old, associations,
99 resolved, fwVersion, updateTimestamp, eventId, resolution);
100 additionalData = util::additional_data::parse(additionalData_old);
Vijay Lobod354a392021-06-01 16:21:02 -0500101 }
Matt Spinler3d079c62025-03-26 12:57:18 -0500102 else if (version ==
103 std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_METADATA_DICT))
Vijay Lobo593a4c62021-06-16 14:25:26 -0500104 {
105 a(id, severity, timestamp, message, additionalData, associations,
106 resolved, fwVersion, updateTimestamp, eventId, resolution);
107 }
Matt Spinler3d079c62025-03-26 12:57:18 -0500108 else
109 {
110 // Go back to reading a vector for additionalData
111 std::vector<std::string> additionalData_old{};
112 a(id, severity, timestamp, message, additionalData_old, associations,
113 resolved, fwVersion, updateTimestamp, eventId, resolution);
114 additionalData = util::additional_data::parse(additionalData_old);
115 }
Deepak Kodihallif1630ea2017-06-25 22:05:47 -0500116
Matt Spinleref952af2021-08-19 10:23:05 -0500117 e.id(id, true);
118 e.severity(severity, true);
119 e.timestamp(timestamp, true);
120 e.message(message, true);
Patrick Williamsea6d9c42024-12-11 14:58:21 -0500121 e.additionalData(additionalData, true);
Willy Tu6ddbf692023-09-05 10:54:16 -0700122 e.sdbusplus::server::xyz::openbmc_project::logging::Entry::resolved(
Matt Spinleref952af2021-08-19 10:23:05 -0500123 resolved, true);
124 e.associations(associations, true);
125 e.version(fwVersion, true);
Willy Tu6ddbf692023-09-05 10:54:16 -0700126 e.purpose(sdbusplus::server::xyz::openbmc_project::software::Version::
Matt Spinleref952af2021-08-19 10:23:05 -0500127 VersionPurpose::BMC,
128 true);
129 e.updateTimestamp(updateTimestamp, true);
130 e.eventId(eventId, true);
131 e.resolution(resolution, true);
Deepak Kodihallif1630ea2017-06-25 22:05:47 -0500132}
133
Matt Spinlerfb978da2022-01-21 08:42:24 -0600134fs::path getEntrySerializePath(uint32_t id, const fs::path& dir)
135{
136 return dir / std::to_string(id);
137}
138
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500139fs::path serialize(const Entry& e, const fs::path& dir)
140{
Matt Spinlerfb978da2022-01-21 08:42:24 -0600141 auto path = getEntrySerializePath(e.id(), dir);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500142 std::ofstream os(path.c_str(), std::ios::binary);
143 cereal::BinaryOutputArchive oarchive(os);
144 oarchive(e);
145 return path;
146}
147
148bool deserialize(const fs::path& path, Entry& e)
149{
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500150 try
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500151 {
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500152 if (fs::exists(path))
153 {
154 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
155 cereal::BinaryInputArchive iarchive(is);
156 iarchive(e);
157 return true;
158 }
159 return false;
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500160 }
Matt Spinler991e2b02025-03-19 10:54:46 -0500161 catch (const std::exception& ex)
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500162 {
Matt Spinler991e2b02025-03-19 10:54:46 -0500163 lg2::error("Failed restoring {PATH}: {EXCEPTION}", "PATH", path,
164 "EXCEPTION", ex);
165 // Save it for later debug. Just write over any previous ones.
166 auto saveDir = paths::error().parent_path();
167 fs::rename(path, saveDir / "corrupt_error");
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530168 return false;
169 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500170}
171
172} // namespace logging
173} // namespace phosphor