-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdump_serialize.cpp
52 lines (44 loc) · 1.1 KB
/
dump_serialize.cpp
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
#include "dump_serialize.hpp"
#include <fmt/core.h>
#include <cereal/archives/binary.hpp>
#include <cereal/types/set.hpp>
#include <phosphor-logging/log.hpp>
#include <fstream>
namespace phosphor
{
namespace dump
{
namespace elog
{
using namespace phosphor::logging;
void serialize(const ElogList& list, const std::filesystem::path& dir)
{
std::ofstream os(dir.c_str(), std::ios::binary);
cereal::BinaryOutputArchive oarchive(os);
oarchive(list);
}
bool deserialize(const std::filesystem::path& path, ElogList& list)
{
try
{
if (std::filesystem::exists(path))
{
std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
cereal::BinaryInputArchive iarchive(is);
iarchive(list);
return true;
}
return false;
}
catch (const cereal::Exception& e)
{
log<level::ERR>(
fmt::format("Failed to deserialize, errormsg({})", e.what())
.c_str());
std::filesystem::remove(path);
return false;
}
}
} // namespace elog
} // namespace dump
} // namespace phosphor