blob: d60cba1ad6eec6e65b39d6fae6facf0a27b6020d [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>
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -05005#include <phosphor-logging/lg2.hpp>
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05006
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 Othayoth3fc6df42021-04-08 03:45:24 -050016void serialize(const ElogList& list, const std::filesystem::path& dir)
Jayanth Othayoth24964822017-09-04 22:07:06 -050017{
18 std::ofstream os(dir.c_str(), std::ios::binary);
19 cereal::BinaryOutputArchive oarchive(os);
20 oarchive(list);
21}
22
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050023bool deserialize(const std::filesystem::path& path, ElogList& list)
Jayanth Othayoth24964822017-09-04 22:07:06 -050024{
Jayanth Othayothe4693a72017-09-20 00:01:23 -050025 try
Jayanth Othayoth24964822017-09-04 22:07:06 -050026 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050027 if (std::filesystem::exists(path))
Jayanth Othayothe4693a72017-09-20 00:01:23 -050028 {
29 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
30 cereal::BinaryInputArchive iarchive(is);
31 iarchive(list);
32 return true;
33 }
34 return false;
Jayanth Othayoth24964822017-09-04 22:07:06 -050035 }
Patrick Williams9d2d7222021-10-06 12:44:44 -050036 catch (const cereal::Exception& e)
Jayanth Othayothe4693a72017-09-20 00:01:23 -050037 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050038 lg2::error("Failed to deserialize, errormsg: {ERROR}", "ERROR", e);
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050039 std::filesystem::remove(path);
Jayanth Othayothe4693a72017-09-20 00:01:23 -050040 return false;
41 }
Jayanth Othayoth24964822017-09-04 22:07:06 -050042}
43
44} // namespace elog
45} // namespace dump
46} // namespace phosphor