blob: 1af19468f8710f70f20d9fc93202a9e37c68586f [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(),
32 e.message(), e.additionalData(), e.associations(), e.resolved());
33}
34
35/** @brief Function required by Cereal to perform deserialization.
36 * @tparam Archive - Cereal archive type (binary in our case).
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053037 * @param[in] a - reference to Cereal archive.
38 * @param[in] e - reference to error entry.
39 * @param[in] version - Class version that enables handling
40 * a serialized data across code levels
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050041 */
42template<class Archive>
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053043void load(Archive& a, Entry& e, const std::uint32_t version)
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050044{
45 using namespace
46 sdbusplus::xyz::openbmc_project::Logging::server;
47
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{};
55
56 a(id, severity, timestamp, message,
57 additionalData, associations, resolved);
58
59 e.id(id);
60 e.severity(severity);
61 e.timestamp(timestamp);
62 e.message(message);
63 e.additionalData(additionalData);
64 e.sdbusplus::xyz::openbmc_project::
65 Logging::server::Entry::resolved(resolved);
66 e.associations(associations);
67}
68
Deepak Kodihalli72654f12017-06-12 04:33:29 -050069fs::path serialize(const Entry& e, const fs::path& dir)
70{
71 auto path = dir / std::to_string(e.id());
72 std::ofstream os(path.c_str(), std::ios::binary);
73 cereal::BinaryOutputArchive oarchive(os);
74 oarchive(e);
75 return path;
76}
77
78bool deserialize(const fs::path& path, Entry& e)
79{
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -050080 try
Deepak Kodihalli72654f12017-06-12 04:33:29 -050081 {
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -050082 if (fs::exists(path))
83 {
84 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
85 cereal::BinaryInputArchive iarchive(is);
86 iarchive(e);
87 return true;
88 }
89 return false;
Deepak Kodihalli72654f12017-06-12 04:33:29 -050090 }
Jayanth Othayoth9c7f03a2017-09-20 00:04:22 -050091 catch(cereal::Exception& e)
92 {
93 log<level::ERR>(e.what());
94 fs::remove(path);
95 return false;
96 }
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +053097 catch(const std::length_error& e)
98 {
99 // Running into: USCiLab/cereal#192
100 // This may be indicating some other issue in the
101 // way vector may have been used inside the logging.
102 // possibly associations ??. But handling it here for
103 // now since we are anyway tossing the log
104 // TODO: openbmc/phosphor-logging#8
105 log<level::ERR>(e.what());
106 fs::remove(path);
107 return false;
108 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500109}
110
111} // namespace logging
112} // namespace phosphor