blob: d807a93db93623e7ab959051348404b9ce0d8875 [file] [log] [blame]
Deepak Kodihalli72654f12017-06-12 04:33:29 -05001#include <cereal/types/string.hpp>
2#include <cereal/types/vector.hpp>
3#include <cereal/types/tuple.hpp>
4#include <cereal/archives/binary.hpp>
5#include <fstream>
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -05006
Deepak Kodihalli72654f12017-06-12 04:33:29 -05007#include "elog_serialize.hpp"
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -05008#include <phosphor-logging/log.hpp>
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +05309#include "config.h"
10
11// Register class version
12// From cereal documentation;
13// "This macro should be placed at global scope"
14CEREAL_CLASS_VERSION(phosphor::logging::Entry, CLASS_VERSION);
Deepak Kodihalli72654f12017-06-12 04:33:29 -050015
16namespace phosphor
17{
18namespace logging
19{
20
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050021/** @brief Function required by Cereal to perform serialization.
22 * @tparam Archive - Cereal archive type (binary in our case).
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053023 * @param[in] a - reference to Cereal archive.
24 * @param[in] e - const reference to error entry.
25 * @param[in] version - Class version that enables handling
26 * a serialized data across code levels
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050027 */
28template<class Archive>
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053029void save(Archive& a, const Entry& e, const std::uint32_t version)
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050030{
31 a(e.id(), e.severity(), e.timestamp(),
Matt Spinler375ac9b2018-05-01 15:20:55 -050032 e.message(), e.additionalData(), e.associations(), e.resolved(),
33 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 */
43template<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{
46 using namespace
47 sdbusplus::xyz::openbmc_project::Logging::server;
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 Spinler375ac9b2018-05-01 15:20:55 -050056 std::string fwVersion{};
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 {
60 a(id, severity, timestamp, message,
61 additionalData, associations, resolved);
62 }
63 else
64 {
65 a(id, severity, timestamp, message,
66 additionalData, associations, resolved, fwVersion);
67 }
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050068
69 e.id(id);
70 e.severity(severity);
71 e.timestamp(timestamp);
72 e.message(message);
73 e.additionalData(additionalData);
74 e.sdbusplus::xyz::openbmc_project::
75 Logging::server::Entry::resolved(resolved);
76 e.associations(associations);
Matt Spinler375ac9b2018-05-01 15:20:55 -050077 e.version(fwVersion);
78 e.purpose(sdbusplus::xyz::openbmc_project::Software::
79 server::Version::VersionPurpose::BMC);
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050080}
81
Deepak Kodihalli72654f12017-06-12 04:33:29 -050082fs::path serialize(const Entry& e, const fs::path& dir)
83{
84 auto path = dir / std::to_string(e.id());
85 std::ofstream os(path.c_str(), std::ios::binary);
86 cereal::BinaryOutputArchive oarchive(os);
87 oarchive(e);
88 return path;
89}
90
91bool deserialize(const fs::path& path, Entry& e)
92{
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -050093 try
Deepak Kodihalli72654f12017-06-12 04:33:29 -050094 {
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -050095 if (fs::exists(path))
96 {
97 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
98 cereal::BinaryInputArchive iarchive(is);
99 iarchive(e);
100 return true;
101 }
102 return false;
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500103 }
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -0500104 catch(cereal::Exception& e)
105 {
106 log<level::ERR>(e.what());
107 fs::remove(path);
108 return false;
109 }
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530110 catch(const std::length_error& e)
111 {
112 // Running into: USCiLab/cereal#192
113 // This may be indicating some other issue in the
114 // way vector may have been used inside the logging.
115 // possibly associations ??. But handling it here for
116 // now since we are anyway tossing the log
117 // TODO: openbmc/phosphor-logging#8
118 log<level::ERR>(e.what());
119 fs::remove(path);
120 return false;
121 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500122}
123
124} // namespace logging
125} // namespace phosphor