blob: fce433dbcc0a5e39c82f8c1889f41ca3670c62ff [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>
6#include <cereal/types/string.hpp>
7#include <cereal/types/tuple.hpp>
8#include <cereal/types/vector.hpp>
9#include <fstream>
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -050010#include <phosphor-logging/log.hpp>
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053011
12// Register class version
13// From cereal documentation;
14// "This macro should be placed at global scope"
Patrick Williamsf40323d2021-04-16 15:35:17 -050015CEREAL_CLASS_VERSION(phosphor::logging::Entry, CLASS_VERSION)
Deepak Kodihalli72654f12017-06-12 04:33:29 -050016
17namespace phosphor
18{
19namespace logging
20{
21
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050022/** @brief Function required by Cereal to perform serialization.
23 * @tparam Archive - Cereal archive type (binary in our case).
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053024 * @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 Kodihallif1630ea2017-06-25 22:05:47 -050028 */
Patrick Venturef18bf832018-10-26 18:14:00 -070029template <class Archive>
Patrick Williamsf40323d2021-04-16 15:35:17 -050030void save(Archive& a, const Entry& e, const std::uint32_t /*version*/)
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050031{
Patrick Venturef18bf832018-10-26 18:14:00 -070032 a(e.id(), e.severity(), e.timestamp(), e.message(), e.additionalData(),
Vijay Lobod354a392021-06-01 16:21:02 -050033 e.associations(), e.resolved(), e.version(), e.updateTimestamp(),
34 e.eventId());
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050035}
36
37/** @brief Function required by Cereal to perform deserialization.
38 * @tparam Archive - Cereal archive type (binary in our case).
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053039 * @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 Kodihallif1630ea2017-06-25 22:05:47 -050043 */
Patrick Venturef18bf832018-10-26 18:14:00 -070044template <class Archive>
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053045void load(Archive& a, Entry& e, const std::uint32_t version)
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050046{
Patrick Venturef18bf832018-10-26 18:14:00 -070047 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050048
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 Spinler375ac9b2018-05-01 15:20:55 -050056 std::string fwVersion{};
Matt Spinler1e71a4d2020-03-04 13:40:22 -060057 uint64_t updateTimestamp{};
Vijay Lobod354a392021-06-01 16:21:02 -050058 std::string eventId{};
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050059
Matt Spinler375ac9b2018-05-01 15:20:55 -050060 if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_FWLEVEL))
61 {
Patrick Venturef18bf832018-10-26 18:14:00 -070062 a(id, severity, timestamp, message, additionalData, associations,
63 resolved);
Matt Spinler1e71a4d2020-03-04 13:40:22 -060064 updateTimestamp = timestamp;
65 }
66 else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_UPDATE_TS))
67 {
68 a(id, severity, timestamp, message, additionalData, associations,
69 resolved, fwVersion);
70 updateTimestamp = timestamp;
Matt Spinler375ac9b2018-05-01 15:20:55 -050071 }
Vijay Lobod354a392021-06-01 16:21:02 -050072 else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_EVENTID))
Matt Spinler375ac9b2018-05-01 15:20:55 -050073 {
Patrick Venturef18bf832018-10-26 18:14:00 -070074 a(id, severity, timestamp, message, additionalData, associations,
Matt Spinler1e71a4d2020-03-04 13:40:22 -060075 resolved, fwVersion, updateTimestamp);
Matt Spinler375ac9b2018-05-01 15:20:55 -050076 }
Vijay Lobod354a392021-06-01 16:21:02 -050077 else
78 {
79 a(id, severity, timestamp, message, additionalData, associations,
80 resolved, fwVersion, updateTimestamp, eventId);
81 }
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050082
83 e.id(id);
84 e.severity(severity);
85 e.timestamp(timestamp);
86 e.message(message);
87 e.additionalData(additionalData);
Patrick Venturef18bf832018-10-26 18:14:00 -070088 e.sdbusplus::xyz::openbmc_project::Logging::server::Entry::resolved(
89 resolved);
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050090 e.associations(associations);
Matt Spinler375ac9b2018-05-01 15:20:55 -050091 e.version(fwVersion);
Patrick Venturef18bf832018-10-26 18:14:00 -070092 e.purpose(sdbusplus::xyz::openbmc_project::Software::server::Version::
93 VersionPurpose::BMC);
Matt Spinler1e71a4d2020-03-04 13:40:22 -060094 e.updateTimestamp(updateTimestamp);
Vijay Lobod354a392021-06-01 16:21:02 -050095 e.eventId(eventId);
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050096}
97
Deepak Kodihalli72654f12017-06-12 04:33:29 -050098fs::path serialize(const Entry& e, const fs::path& dir)
99{
100 auto path = dir / std::to_string(e.id());
101 std::ofstream os(path.c_str(), std::ios::binary);
102 cereal::BinaryOutputArchive oarchive(os);
103 oarchive(e);
104 return path;
105}
106
107bool deserialize(const fs::path& path, Entry& e)
108{
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500109 try
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500110 {
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500111 if (fs::exists(path))
112 {
113 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
114 cereal::BinaryInputArchive iarchive(is);
115 iarchive(e);
116 return true;
117 }
118 return false;
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500119 }
Patrick Venturef18bf832018-10-26 18:14:00 -0700120 catch (cereal::Exception& e)
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500121 {
122 log<level::ERR>(e.what());
123 fs::remove(path);
124 return false;
125 }
Patrick Venturef18bf832018-10-26 18:14:00 -0700126 catch (const std::length_error& e)
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530127 {
128 // Running into: USCiLab/cereal#192
129 // This may be indicating some other issue in the
130 // way vector may have been used inside the logging.
131 // possibly associations ??. But handling it here for
132 // now since we are anyway tossing the log
133 // TODO: openbmc/phosphor-logging#8
134 log<level::ERR>(e.what());
135 fs::remove(path);
136 return false;
137 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500138}
139
140} // namespace logging
141} // namespace phosphor