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