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> |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 6 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 7 | #include "elog_serialize.hpp" |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 9 | #include "config.h" |
| 10 | |
| 11 | // Register class version |
| 12 | // From cereal documentation; |
| 13 | // "This macro should be placed at global scope" |
| 14 | CEREAL_CLASS_VERSION(phosphor::logging::Entry, CLASS_VERSION); |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace logging |
| 19 | { |
| 20 | |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 21 | /** @brief Function required by Cereal to perform serialization. |
| 22 | * @tparam Archive - Cereal archive type (binary in our case). |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 23 | * @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 Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 27 | */ |
| 28 | template<class Archive> |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 29 | void save(Archive& a, const Entry& e, const std::uint32_t version) |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 30 | { |
| 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 Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 37 | * @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 Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 41 | */ |
| 42 | template<class Archive> |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 43 | void load(Archive& a, Entry& e, const std::uint32_t version) |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 44 | { |
| 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 Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 69 | fs::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 | |
| 78 | bool deserialize(const fs::path& path, Entry& e) |
| 79 | { |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 80 | try |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 81 | { |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 82 | 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 Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 90 | } |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 91 | catch(cereal::Exception& e) |
| 92 | { |
| 93 | log<level::ERR>(e.what()); |
| 94 | fs::remove(path); |
| 95 | return false; |
| 96 | } |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 97 | 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 Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | } // namespace logging |
| 112 | } // namespace phosphor |