Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 1 | #include "config.h" |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 2 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 3 | #include "elog_serialize.hpp" |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 4 | |
| 5 | #include <cereal/archives/binary.hpp> |
| 6 | #include <cereal/types/string.hpp> |
| 7 | #include <cereal/types/tuple.hpp> |
| 8 | #include <cereal/types/vector.hpp> |
| 9 | #include <fstream> |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/log.hpp> |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 11 | |
| 12 | // Register class version |
| 13 | // From cereal documentation; |
| 14 | // "This macro should be placed at global scope" |
| 15 | CEREAL_CLASS_VERSION(phosphor::logging::Entry, CLASS_VERSION); |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace logging |
| 20 | { |
| 21 | |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 22 | /** @brief Function required by Cereal to perform serialization. |
| 23 | * @tparam Archive - Cereal archive type (binary in our case). |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 24 | * @param[in] a - reference to Cereal archive. |
| 25 | * @param[in] e - const reference to error entry. |
| 26 | * @param[in] version - Class version that enables handling |
| 27 | * a serialized data across code levels |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 28 | */ |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 29 | template <class Archive> |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 30 | void save(Archive& a, const Entry& e, const std::uint32_t version) |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 31 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 32 | a(e.id(), e.severity(), e.timestamp(), e.message(), e.additionalData(), |
| 33 | e.associations(), e.resolved(), 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 | */ |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 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 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 46 | using namespace sdbusplus::xyz::openbmc_project::Logging::server; |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 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{}; |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 55 | std::string fwVersion{}; |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 56 | |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 57 | if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_FWLEVEL)) |
| 58 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 59 | a(id, severity, timestamp, message, additionalData, associations, |
| 60 | resolved); |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 61 | } |
| 62 | else |
| 63 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 64 | a(id, severity, timestamp, message, additionalData, associations, |
| 65 | resolved, fwVersion); |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 66 | } |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 67 | |
| 68 | e.id(id); |
| 69 | e.severity(severity); |
| 70 | e.timestamp(timestamp); |
| 71 | e.message(message); |
| 72 | e.additionalData(additionalData); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 73 | e.sdbusplus::xyz::openbmc_project::Logging::server::Entry::resolved( |
| 74 | resolved); |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 75 | e.associations(associations); |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 76 | e.version(fwVersion); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 77 | e.purpose(sdbusplus::xyz::openbmc_project::Software::server::Version:: |
| 78 | VersionPurpose::BMC); |
Deepak Kodihalli | f1630ea | 2017-06-25 22:05:47 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 81 | fs::path serialize(const Entry& e, const fs::path& dir) |
| 82 | { |
| 83 | auto path = dir / std::to_string(e.id()); |
| 84 | std::ofstream os(path.c_str(), std::ios::binary); |
| 85 | cereal::BinaryOutputArchive oarchive(os); |
| 86 | oarchive(e); |
| 87 | return path; |
| 88 | } |
| 89 | |
| 90 | bool deserialize(const fs::path& path, Entry& e) |
| 91 | { |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 92 | try |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 93 | { |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 94 | if (fs::exists(path)) |
| 95 | { |
| 96 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 97 | cereal::BinaryInputArchive iarchive(is); |
| 98 | iarchive(e); |
| 99 | return true; |
| 100 | } |
| 101 | return false; |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 102 | } |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 103 | catch (cereal::Exception& e) |
Jayanth Othayoth | 9c7f03a | 2017-09-20 00:04:22 -0500 | [diff] [blame] | 104 | { |
| 105 | log<level::ERR>(e.what()); |
| 106 | fs::remove(path); |
| 107 | return false; |
| 108 | } |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 109 | catch (const std::length_error& e) |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 110 | { |
| 111 | // Running into: USCiLab/cereal#192 |
| 112 | // This may be indicating some other issue in the |
| 113 | // way vector may have been used inside the logging. |
| 114 | // possibly associations ??. But handling it here for |
| 115 | // now since we are anyway tossing the log |
| 116 | // TODO: openbmc/phosphor-logging#8 |
| 117 | log<level::ERR>(e.what()); |
| 118 | fs::remove(path); |
| 119 | return false; |
| 120 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace logging |
| 124 | } // namespace phosphor |