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(), |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 32 | e.message(), e.additionalData(), e.associations(), e.resolved(), |
| 33 | e.version()); |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /** @brief Function required by Cereal to perform deserialization. |
| 37 | * @tparam Archive - Cereal archive type (binary in our case). |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 38 | * @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 Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 42 | */ |
| 43 | template<class Archive> |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 44 | void load(Archive& a, Entry& e, const std::uint32_t version) |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 45 | { |
| 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 Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 56 | std::string fwVersion{}; |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 57 | |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 58 | 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 Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 68 | |
| 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 Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 77 | e.version(fwVersion); |
| 78 | e.purpose(sdbusplus::xyz::openbmc_project::Software:: |
| 79 | server::Version::VersionPurpose::BMC); |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 82 | fs::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 | |
| 91 | bool deserialize(const fs::path& path, Entry& e) |
| 92 | { |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 93 | try |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 94 | { |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 95 | 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 Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 103 | } |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 104 | catch(cereal::Exception& e) |
| 105 | { |
| 106 | log<level::ERR>(e.what()); |
| 107 | fs::remove(path); |
| 108 | return false; |
| 109 | } |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 110 | 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 Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | } // namespace logging |
| 125 | } // namespace phosphor |