blob: 72dc295e470d17d7b44a9a75953e11f33cb89300 [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>
6#include "elog_serialize.hpp"
7
8namespace phosphor
9{
10namespace logging
11{
12
Deepak Kodihallif1630ea2017-06-25 22:05:47 -050013/** @brief Function required by Cereal to perform serialization.
14 * @tparam Archive - Cereal archive type (binary in our case).
15 * @param[in] a - reference to Cereal archive.
16 * @param[in] e - const reference to error entry.
17 */
18template<class Archive>
19void save(Archive& a, const Entry& e)
20{
21 a(e.id(), e.severity(), e.timestamp(),
22 e.message(), e.additionalData(), e.associations(), e.resolved());
23}
24
25/** @brief Function required by Cereal to perform deserialization.
26 * @tparam Archive - Cereal archive type (binary in our case).
27 * @param[in] a - reference to Cereal archive.
28 * @param[in] e - reference to error entry.
29 */
30template<class Archive>
31void load(Archive& a, Entry& e)
32{
33 using namespace
34 sdbusplus::xyz::openbmc_project::Logging::server;
35
36 uint32_t id{};
37 Entry::Level severity{};
38 uint64_t timestamp{};
39 std::string message{};
40 std::vector<std::string> additionalData{};
41 bool resolved{};
42 AssociationList associations{};
43
44 a(id, severity, timestamp, message,
45 additionalData, associations, resolved);
46
47 e.id(id);
48 e.severity(severity);
49 e.timestamp(timestamp);
50 e.message(message);
51 e.additionalData(additionalData);
52 e.sdbusplus::xyz::openbmc_project::
53 Logging::server::Entry::resolved(resolved);
54 e.associations(associations);
55}
56
Deepak Kodihalli72654f12017-06-12 04:33:29 -050057fs::path serialize(const Entry& e, const fs::path& dir)
58{
59 auto path = dir / std::to_string(e.id());
60 std::ofstream os(path.c_str(), std::ios::binary);
61 cereal::BinaryOutputArchive oarchive(os);
62 oarchive(e);
63 return path;
64}
65
66bool deserialize(const fs::path& path, Entry& e)
67{
68 if (fs::exists(path))
69 {
70 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
71 cereal::BinaryInputArchive iarchive(is);
72 iarchive(e);
73 return true;
74 }
75 return false;
76}
77
78} // namespace logging
79} // namespace phosphor