blob: 823cc4c4ca1e7e4decdf429f354803ace94057d1 [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>
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05005#include <phosphor-logging/log.hpp>
6
Jayanth Othayoth0af74a52021-04-08 03:55:21 -05007#include <fstream>
8
Jayanth Othayoth24964822017-09-04 22:07:06 -05009namespace phosphor
10{
11namespace dump
12{
13namespace elog
14{
15
Jayanth Othayothe4693a72017-09-20 00:01:23 -050016using namespace phosphor::logging;
17
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050018void serialize(const ElogList& list, const std::filesystem::path& dir)
Jayanth Othayoth24964822017-09-04 22:07:06 -050019{
20 std::ofstream os(dir.c_str(), std::ios::binary);
21 cereal::BinaryOutputArchive oarchive(os);
22 oarchive(list);
23}
24
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050025bool deserialize(const std::filesystem::path& path, ElogList& list)
Jayanth Othayoth24964822017-09-04 22:07:06 -050026{
Jayanth Othayothe4693a72017-09-20 00:01:23 -050027 try
Jayanth Othayoth24964822017-09-04 22:07:06 -050028 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050029 if (std::filesystem::exists(path))
Jayanth Othayothe4693a72017-09-20 00:01:23 -050030 {
31 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
32 cereal::BinaryInputArchive iarchive(is);
33 iarchive(list);
34 return true;
35 }
36 return false;
Jayanth Othayoth24964822017-09-04 22:07:06 -050037 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050038 catch (cereal::Exception& e)
Jayanth Othayothe4693a72017-09-20 00:01:23 -050039 {
40 log<level::ERR>(e.what());
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050041 std::filesystem::remove(path);
Jayanth Othayothe4693a72017-09-20 00:01:23 -050042 return false;
43 }
Jayanth Othayoth24964822017-09-04 22:07:06 -050044}
45
46} // namespace elog
47} // namespace dump
48} // namespace phosphor