blob: 4d1a4858d5d88428003a56b974b6c89292228798 [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#include <phosphor-logging/log.hpp>
7
Jayanth Othayoth0af74a52021-04-08 03:55:21 -05008#include <fstream>
9
Jayanth Othayoth24964822017-09-04 22:07:06 -050010namespace phosphor
11{
12namespace dump
13{
14namespace elog
15{
16
Jayanth Othayothe4693a72017-09-20 00:01:23 -050017using namespace phosphor::logging;
18
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050019void serialize(const ElogList& list, const std::filesystem::path& dir)
Jayanth Othayoth24964822017-09-04 22:07:06 -050020{
21 std::ofstream os(dir.c_str(), std::ios::binary);
22 cereal::BinaryOutputArchive oarchive(os);
23 oarchive(list);
24}
25
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050026bool deserialize(const std::filesystem::path& path, ElogList& list)
Jayanth Othayoth24964822017-09-04 22:07:06 -050027{
Jayanth Othayothe4693a72017-09-20 00:01:23 -050028 try
Jayanth Othayoth24964822017-09-04 22:07:06 -050029 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050030 if (std::filesystem::exists(path))
Jayanth Othayothe4693a72017-09-20 00:01:23 -050031 {
32 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
33 cereal::BinaryInputArchive iarchive(is);
34 iarchive(list);
35 return true;
36 }
37 return false;
Jayanth Othayoth24964822017-09-04 22:07:06 -050038 }
Patrick Williams9d2d7222021-10-06 12:44:44 -050039 catch (const cereal::Exception& e)
Jayanth Othayothe4693a72017-09-20 00:01:23 -050040 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050041 lg2::error("Failed to deserialize, errormsg: {ERROR}", "ERROR", e);
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050042 std::filesystem::remove(path);
Jayanth Othayothe4693a72017-09-20 00:01:23 -050043 return false;
44 }
Jayanth Othayoth24964822017-09-04 22:07:06 -050045}
46
47} // namespace elog
48} // namespace dump
49} // namespace phosphor