blob: 72534a45d2b85be73c3d467272884cb6203748a2 [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(),
33 e.associations(), e.resolved(), e.version());
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{};
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050056
Matt Spinler375ac9b2018-05-01 15:20:55 -050057 if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_FWLEVEL))
58 {
Patrick Venturef18bf832018-10-26 18:14:00 -070059 a(id, severity, timestamp, message, additionalData, associations,
60 resolved);
Matt Spinler375ac9b2018-05-01 15:20:55 -050061 }
62 else
63 {
Patrick Venturef18bf832018-10-26 18:14:00 -070064 a(id, severity, timestamp, message, additionalData, associations,
65 resolved, fwVersion);
Matt Spinler375ac9b2018-05-01 15:20:55 -050066 }
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050067
68 e.id(id);
69 e.severity(severity);
70 e.timestamp(timestamp);
71 e.message(message);
72 e.additionalData(additionalData);
Patrick Venturef18bf832018-10-26 18:14:00 -070073 e.sdbusplus::xyz::openbmc_project::Logging::server::Entry::resolved(
74 resolved);
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050075 e.associations(associations);
Matt Spinler375ac9b2018-05-01 15:20:55 -050076 e.version(fwVersion);
Patrick Venturef18bf832018-10-26 18:14:00 -070077 e.purpose(sdbusplus::xyz::openbmc_project::Software::server::Version::
78 VersionPurpose::BMC);
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050079}
80
Deepak Kodihalli72654f12017-06-12 04:33:29 -050081fs::path serialize(const Entry& e, const fs::path& dir)
82{
83 auto path = dir / std::to_string(e.id());
84 std::ofstream os(path.c_str(), std::ios::binary);
85 cereal::BinaryOutputArchive oarchive(os);
86 oarchive(e);
87 return path;
88}
89
90bool deserialize(const fs::path& path, Entry& e)
91{
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -050092 try
Deepak Kodihalli72654f12017-06-12 04:33:29 -050093 {
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -050094 if (fs::exists(path))
95 {
96 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
97 cereal::BinaryInputArchive iarchive(is);
98 iarchive(e);
99 return true;
100 }
101 return false;
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500102 }
Patrick Venturef18bf832018-10-26 18:14:00 -0700103 catch (cereal::Exception& e)
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500104 {
105 log<level::ERR>(e.what());
106 fs::remove(path);
107 return false;
108 }
Patrick Venturef18bf832018-10-26 18:14:00 -0700109 catch (const std::length_error& e)
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530110 {
111 // Running into: USCiLab/cereal#192
112 // This may be indicating some other issue in the
113 // way vector may have been used inside the logging.
114 // possibly associations ??. But handling it here for
115 // now since we are anyway tossing the log
116 // TODO: openbmc/phosphor-logging#8
117 log<level::ERR>(e.what());
118 fs::remove(path);
119 return false;
120 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500121}
122
123} // namespace logging
124} // namespace phosphor