-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathelog_serialize.cpp
More file actions
277 lines (249 loc) · 9.44 KB
/
elog_serialize.cpp
File metadata and controls
277 lines (249 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "config.h"
#include "elog_serialize.hpp"
#include "util.hpp"
#include <cereal/archives/binary.hpp>
#include <cereal/types/map.hpp>
#include <cereal/types/string.hpp>
#include <cereal/types/tuple.hpp>
#include <cereal/types/vector.hpp>
#include <nlohmann/json.hpp>
#include <phosphor-logging/lg2.hpp>
#include <fstream>
// Register class version
// From cereal documentation;
// "This macro should be placed at global scope"
CEREAL_CLASS_VERSION(phosphor::logging::Entry, CLASS_VERSION)
namespace phosphor
{
namespace logging
{
/** @brief Function required by Cereal to perform serialization.
* @tparam Archive - Cereal archive type (binary in our case).
* @param[in] a - reference to Cereal archive.
* @param[in] e - const reference to error entry.
* @param[in] version - Class version that enables handling
* a serialized data across code levels
*/
template <class Archive>
void save(Archive& a, const Entry& e, const std::uint32_t /*version*/)
{
a(e.id(), e.severity(), e.timestamp(), e.message(),
util::additional_data::combine(e.additionalData()), e.associations(),
e.resolved(), e.version(), e.updateTimestamp(), e.eventId(),
e.resolution());
}
/** @brief Function required by Cereal to perform deserialization.
* @tparam Archive - Cereal archive type (binary in our case).
* @param[in] a - reference to Cereal archive.
* @param[in] e - reference to error entry.
* @param[in] version - Class version that enables handling
* a serialized data across code levels
*/
template <class Archive>
void load(Archive& a, Entry& e, const std::uint32_t version)
{
using namespace sdbusplus::server::xyz::openbmc_project::logging;
uint32_t id{};
Entry::Level severity{};
uint64_t timestamp{};
std::string message{};
std::map<std::string, std::string> additionalData{};
bool resolved{};
AssociationList associations{};
std::string fwVersion{};
uint64_t updateTimestamp{};
std::string eventId{};
std::string resolution{};
if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_FWLEVEL))
{
std::vector<std::string> additionalData_old{};
a(id, severity, timestamp, message, additionalData_old, associations,
resolved);
updateTimestamp = timestamp;
additionalData = util::additional_data::parse(additionalData_old);
}
else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_UPDATE_TS))
{
std::vector<std::string> additionalData_old{};
a(id, severity, timestamp, message, additionalData_old, associations,
resolved, fwVersion);
updateTimestamp = timestamp;
additionalData = util::additional_data::parse(additionalData_old);
}
else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_EVENTID))
{
std::vector<std::string> additionalData_old{};
a(id, severity, timestamp, message, additionalData_old, associations,
resolved, fwVersion, updateTimestamp);
additionalData = util::additional_data::parse(additionalData_old);
}
else if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_RESOLUTION))
{
std::vector<std::string> additionalData_old{};
a(id, severity, timestamp, message, additionalData_old, associations,
resolved, fwVersion, updateTimestamp, eventId);
additionalData = util::additional_data::parse(additionalData_old);
}
else if (version <
std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_METADATA_DICT))
{
std::vector<std::string> additionalData_old{};
a(id, severity, timestamp, message, additionalData_old, associations,
resolved, fwVersion, updateTimestamp, eventId, resolution);
additionalData = util::additional_data::parse(additionalData_old);
}
else if (version ==
std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_METADATA_DICT))
{
a(id, severity, timestamp, message, additionalData, associations,
resolved, fwVersion, updateTimestamp, eventId, resolution);
}
else
{
// Go back to reading a vector for additionalData
std::vector<std::string> additionalData_old{};
a(id, severity, timestamp, message, additionalData_old, associations,
resolved, fwVersion, updateTimestamp, eventId, resolution);
additionalData = util::additional_data::parse(additionalData_old);
}
e.id(id, true);
e.severity(severity, true);
e.timestamp(timestamp, true);
e.message(message, true);
e.additionalData(additionalData, true);
e.sdbusplus::server::xyz::openbmc_project::logging::Entry::resolved(
resolved, true);
e.associations(associations, true);
e.version(fwVersion, true);
e.purpose(sdbusplus::server::xyz::openbmc_project::software::Version::
VersionPurpose::BMC,
true);
e.updateTimestamp(updateTimestamp, true);
e.eventId(eventId, true);
e.resolution(resolution, true);
}
fs::path getEntrySerializePath(uint32_t id, const fs::path& dir)
{
return dir / std::to_string(id);
}
fs::path serialize(const Entry& e, const fs::path& dir)
{
auto path = getEntrySerializePath(e.id(), dir);
std::ofstream os(path.c_str(), std::ios::binary);
cereal::BinaryOutputArchive oarchive(os);
oarchive(e);
return path;
}
fs::path serializeJSON(const Entry& e, const fs::path& dir)
{
auto path = getEntrySerializePath(e.id(), dir);
path += ".json";
nlohmann::json j;
j["jsonVersion"] = JSON_FORMAT_VERSION;
j["id"] = e.id();
j["severity"] = static_cast<int>(e.severity());
j["timestamp"] = e.timestamp();
j["message"] = e.message();
j["additionalData"] = e.additionalData();
nlohmann::json assocArray = nlohmann::json::array();
for (const auto& [forward, reverse, endpoint] : e.associations())
{
assocArray.push_back({forward, reverse, endpoint});
}
j["associations"] = assocArray;
j["resolved"] = e.resolved();
j["version"] = e.version();
j["updateTimestamp"] = e.updateTimestamp();
j["eventId"] = e.eventId();
j["resolution"] = e.resolution();
std::ofstream os(path.c_str());
os << j.dump(4);
return path;
}
bool deserializeJSON(const fs::path& path, Entry& e)
{
try
{
if (!fs::exists(path))
{
return false;
}
std::ifstream is(path.c_str());
nlohmann::json j = nlohmann::json::parse(is);
// Version 1 is the initial JSON format
size_t fileVersion = j.value("jsonVersion", 1);
if (fileVersion > JSON_FORMAT_VERSION)
{
lg2::error(
"Unsupported JSON version {VER} in {PATH}, max supported: {MAX}",
"VER", fileVersion, "PATH", path, "MAX", JSON_FORMAT_VERSION);
return false;
}
// When JSON_FORMAT_VERSION is bumped, add migration logic here
// to apply defaults for any newly added fields that don't exist
// in older versions. The common fields below are always read.
if (fileVersion < JSON_FORMAT_VERSION)
{}
e.id(j.at("id").get<uint32_t>(), true);
e.severity(static_cast<Entry::Level>(j.at("severity").get<int>()),
true);
e.timestamp(j.at("timestamp").get<uint64_t>(), true);
e.message(j.at("message").get<std::string>(), true);
e.additionalData(
j.at("additionalData").get<std::map<std::string, std::string>>(),
true);
e.sdbusplus::server::xyz::openbmc_project::logging::Entry::resolved(
j.at("resolved").get<bool>(), true);
AssociationList associations;
for (const auto& a : j.at("associations"))
{
associations.emplace_back(a[0].get<std::string>(),
a[1].get<std::string>(),
a[2].get<std::string>());
}
e.associations(associations, true);
e.version(j.at("version").get<std::string>(), true);
e.purpose(sdbusplus::server::xyz::openbmc_project::software::Version::
VersionPurpose::BMC,
true);
e.updateTimestamp(j.at("updateTimestamp").get<uint64_t>(), true);
e.eventId(j.at("eventId").get<std::string>(), true);
e.resolution(j.at("resolution").get<std::string>(), true);
return true;
}
catch (const std::exception& ex)
{
lg2::error("Failed restoring JSON {PATH}: {EXCEPTION}", "PATH", path,
"EXCEPTION", ex);
// Save the corrupt entry file for later debug. Just write over any
// previous ones.
auto saveDir = paths::error().parent_path();
fs::rename(path, saveDir / "corrupt_error_json");
return false;
}
}
bool deserialize(const fs::path& path, Entry& e)
{
try
{
if (fs::exists(path))
{
std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
cereal::BinaryInputArchive iarchive(is);
iarchive(e);
return true;
}
return false;
}
catch (const std::exception& ex)
{
lg2::error("Failed restoring {PATH}: {EXCEPTION}", "PATH", path,
"EXCEPTION", ex);
// Save it for later debug. Just write over any previous ones.
auto saveDir = paths::error().parent_path();
fs::rename(path, saveDir / "corrupt_error");
return false;
}
}
} // namespace logging
} // namespace phosphor