1 #include "config.h"
2 
3 #include "elog_serialize.hpp"
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>
10 #include <phosphor-logging/log.hpp>
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);
16 
17 namespace phosphor
18 {
19 namespace logging
20 {
21 
22 /** @brief Function required by Cereal to perform serialization.
23  *  @tparam Archive - Cereal archive type (binary in our case).
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
28  */
29 template <class Archive>
30 void save(Archive& a, const Entry& e, const std::uint32_t version)
31 {
32     a(e.id(), e.severity(), e.timestamp(), e.message(), e.additionalData(),
33       e.associations(), e.resolved(), e.version(), e.updateTimestamp());
34 }
35 
36 /** @brief Function required by Cereal to perform deserialization.
37  *  @tparam Archive - Cereal archive type (binary in our case).
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
42  */
43 template <class Archive>
44 void load(Archive& a, Entry& e, const std::uint32_t version)
45 {
46     using namespace 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     std::string fwVersion{};
56     uint64_t updateTimestamp{};
57 
58     if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_FWLEVEL))
59     {
60         a(id, severity, timestamp, message, additionalData, associations,
61           resolved);
62         updateTimestamp = timestamp;
63     }
64     else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_UPDATE_TS))
65     {
66         a(id, severity, timestamp, message, additionalData, associations,
67           resolved, fwVersion);
68         updateTimestamp = timestamp;
69     }
70     else
71     {
72         a(id, severity, timestamp, message, additionalData, associations,
73           resolved, fwVersion, updateTimestamp);
74     }
75 
76     e.id(id);
77     e.severity(severity);
78     e.timestamp(timestamp);
79     e.message(message);
80     e.additionalData(additionalData);
81     e.sdbusplus::xyz::openbmc_project::Logging::server::Entry::resolved(
82         resolved);
83     e.associations(associations);
84     e.version(fwVersion);
85     e.purpose(sdbusplus::xyz::openbmc_project::Software::server::Version::
86                   VersionPurpose::BMC);
87     e.updateTimestamp(updateTimestamp);
88 }
89 
90 fs::path serialize(const Entry& e, const fs::path& dir)
91 {
92     auto path = dir / std::to_string(e.id());
93     std::ofstream os(path.c_str(), std::ios::binary);
94     cereal::BinaryOutputArchive oarchive(os);
95     oarchive(e);
96     return path;
97 }
98 
99 bool deserialize(const fs::path& path, Entry& e)
100 {
101     try
102     {
103         if (fs::exists(path))
104         {
105             std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
106             cereal::BinaryInputArchive iarchive(is);
107             iarchive(e);
108             return true;
109         }
110         return false;
111     }
112     catch (cereal::Exception& e)
113     {
114         log<level::ERR>(e.what());
115         fs::remove(path);
116         return false;
117     }
118     catch (const std::length_error& e)
119     {
120         // Running into: USCiLab/cereal#192
121         // This may be indicating some other issue in the
122         // way vector may have been used inside the logging.
123         // possibly associations ??. But handling it here for
124         // now since we are anyway tossing the log
125         // TODO: openbmc/phosphor-logging#8
126         log<level::ERR>(e.what());
127         fs::remove(path);
128         return false;
129     }
130 }
131 
132 } // namespace logging
133 } // namespace phosphor
134