blob: 40c6b7439f63fc09d4ec185b958f4fb082e90346 [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"
15CEREAL_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>
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053030void 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(),
Matt Spinler1e71a4d2020-03-04 13:40:22 -060033 e.associations(), e.resolved(), e.version(), e.updateTimestamp());
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050034}
35
36/** @brief Function required by Cereal to perform deserialization.
37 * @tparam Archive - Cereal archive type (binary in our case).
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053038 * @param[in] a - reference to Cereal archive.
39 * @param[in] e - reference to error entry.
40 * @param[in] version - Class version that enables handling
41 * a serialized data across code levels
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050042 */
Patrick Venturef18bf832018-10-26 18:14:00 -070043template <class Archive>
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053044void load(Archive& a, Entry& e, const std::uint32_t version)
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050045{
Patrick Venturef18bf832018-10-26 18:14:00 -070046 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050047
48 uint32_t id{};
49 Entry::Level severity{};
50 uint64_t timestamp{};
51 std::string message{};
52 std::vector<std::string> additionalData{};
53 bool resolved{};
54 AssociationList associations{};
Matt Spinler375ac9b2018-05-01 15:20:55 -050055 std::string fwVersion{};
Matt Spinler1e71a4d2020-03-04 13:40:22 -060056 uint64_t updateTimestamp{};
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050057
Matt Spinler375ac9b2018-05-01 15:20:55 -050058 if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_FWLEVEL))
59 {
Patrick Venturef18bf832018-10-26 18:14:00 -070060 a(id, severity, timestamp, message, additionalData, associations,
61 resolved);
Matt Spinler1e71a4d2020-03-04 13:40:22 -060062 updateTimestamp = timestamp;
63 }
64 else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_UPDATE_TS))
65 {
66 a(id, severity, timestamp, message, additionalData, associations,
67 resolved, fwVersion);
68 updateTimestamp = timestamp;
Matt Spinler375ac9b2018-05-01 15:20:55 -050069 }
70 else
71 {
Patrick Venturef18bf832018-10-26 18:14:00 -070072 a(id, severity, timestamp, message, additionalData, associations,
Matt Spinler1e71a4d2020-03-04 13:40:22 -060073 resolved, fwVersion, updateTimestamp);
Matt Spinler375ac9b2018-05-01 15:20:55 -050074 }
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050075
76 e.id(id);
77 e.severity(severity);
78 e.timestamp(timestamp);
79 e.message(message);
80 e.additionalData(additionalData);
Patrick Venturef18bf832018-10-26 18:14:00 -070081 e.sdbusplus::xyz::openbmc_project::Logging::server::Entry::resolved(
82 resolved);
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050083 e.associations(associations);
Matt Spinler375ac9b2018-05-01 15:20:55 -050084 e.version(fwVersion);
Patrick Venturef18bf832018-10-26 18:14:00 -070085 e.purpose(sdbusplus::xyz::openbmc_project::Software::server::Version::
86 VersionPurpose::BMC);
Matt Spinler1e71a4d2020-03-04 13:40:22 -060087 e.updateTimestamp(updateTimestamp);
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050088}
89
Deepak Kodihalli72654f12017-06-12 04:33:29 -050090fs::path serialize(const Entry& e, const fs::path& dir)
91{
92 auto path = dir / std::to_string(e.id());
93 std::ofstream os(path.c_str(), std::ios::binary);
94 cereal::BinaryOutputArchive oarchive(os);
95 oarchive(e);
96 return path;
97}
98
99bool deserialize(const fs::path& path, Entry& e)
100{
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500101 try
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500102 {
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500103 if (fs::exists(path))
104 {
105 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
106 cereal::BinaryInputArchive iarchive(is);
107 iarchive(e);
108 return true;
109 }
110 return false;
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500111 }
Patrick Venturef18bf832018-10-26 18:14:00 -0700112 catch (cereal::Exception& e)
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500113 {
114 log<level::ERR>(e.what());
115 fs::remove(path);
116 return false;
117 }
Patrick Venturef18bf832018-10-26 18:14:00 -0700118 catch (const std::length_error& e)
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530119 {
120 // Running into: USCiLab/cereal#192
121 // This may be indicating some other issue in the
122 // way vector may have been used inside the logging.
123 // possibly associations ??. But handling it here for
124 // now since we are anyway tossing the log
125 // TODO: openbmc/phosphor-logging#8
126 log<level::ERR>(e.what());
127 fs::remove(path);
128 return false;
129 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500130}
131
132} // namespace logging
133} // namespace phosphor