blob: 799b1421c71f555056339f9aad14a5e2386f680d [file] [log] [blame]
Jayanth Othayoth24964822017-09-04 22:07:06 -05001#include "dump_serialize.hpp"
2
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05003#include <cereal/archives/binary.hpp>
4#include <cereal/types/set.hpp>
5#include <fstream>
6#include <phosphor-logging/log.hpp>
7
Jayanth Othayoth24964822017-09-04 22:07:06 -05008namespace phosphor
9{
10namespace dump
11{
12namespace elog
13{
14
Jayanth Othayothe4693a72017-09-20 00:01:23 -050015using namespace phosphor::logging;
16
Jayanth Othayoth24964822017-09-04 22:07:06 -050017void serialize(const ElogList& list, const fs::path& dir)
18{
19 std::ofstream os(dir.c_str(), std::ios::binary);
20 cereal::BinaryOutputArchive oarchive(os);
21 oarchive(list);
22}
23
24bool deserialize(const fs::path& path, ElogList& list)
25{
Jayanth Othayothe4693a72017-09-20 00:01:23 -050026 try
Jayanth Othayoth24964822017-09-04 22:07:06 -050027 {
Jayanth Othayothe4693a72017-09-20 00:01:23 -050028 if (fs::exists(path))
29 {
30 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
31 cereal::BinaryInputArchive iarchive(is);
32 iarchive(list);
33 return true;
34 }
35 return false;
Jayanth Othayoth24964822017-09-04 22:07:06 -050036 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050037 catch (cereal::Exception& e)
Jayanth Othayothe4693a72017-09-20 00:01:23 -050038 {
39 log<level::ERR>(e.what());
40 fs::remove(path);
41 return false;
42 }
Jayanth Othayoth24964822017-09-04 22:07:06 -050043}
44
45} // namespace elog
46} // namespace dump
47} // namespace phosphor