Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 1 | #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 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace logging |
| 11 | { |
| 12 | |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 13 | /** @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 | */ |
| 18 | template<class Archive> |
| 19 | void 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 | */ |
| 30 | template<class Archive> |
| 31 | void 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 Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 57 | fs::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 | |
| 66 | bool 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 |