blob: 0104b833dfe2d8e9226352ec6a522a5ac5b3bf6b [file] [log] [blame]
Jayanth Othayoth24964822017-09-04 22:07:06 -05001#include "dump_serialize.hpp"
2
George Liu858fbb22021-07-01 12:25:44 +08003#include <fmt/core.h>
4
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05005#include <cereal/archives/binary.hpp>
6#include <cereal/types/set.hpp>
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05007#include <phosphor-logging/log.hpp>
8
Jayanth Othayoth0af74a52021-04-08 03:55:21 -05009#include <fstream>
10
Jayanth Othayoth24964822017-09-04 22:07:06 -050011namespace phosphor
12{
13namespace dump
14{
15namespace elog
16{
17
Jayanth Othayothe4693a72017-09-20 00:01:23 -050018using namespace phosphor::logging;
19
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050020void serialize(const ElogList& list, const std::filesystem::path& dir)
Jayanth Othayoth24964822017-09-04 22:07:06 -050021{
22 std::ofstream os(dir.c_str(), std::ios::binary);
23 cereal::BinaryOutputArchive oarchive(os);
24 oarchive(list);
25}
26
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050027bool deserialize(const std::filesystem::path& path, ElogList& list)
Jayanth Othayoth24964822017-09-04 22:07:06 -050028{
Jayanth Othayothe4693a72017-09-20 00:01:23 -050029 try
Jayanth Othayoth24964822017-09-04 22:07:06 -050030 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050031 if (std::filesystem::exists(path))
Jayanth Othayothe4693a72017-09-20 00:01:23 -050032 {
33 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
34 cereal::BinaryInputArchive iarchive(is);
35 iarchive(list);
36 return true;
37 }
38 return false;
Jayanth Othayoth24964822017-09-04 22:07:06 -050039 }
Patrick Williams9d2d7222021-10-06 12:44:44 -050040 catch (const cereal::Exception& e)
Jayanth Othayothe4693a72017-09-20 00:01:23 -050041 {
George Liu858fbb22021-07-01 12:25:44 +080042 log<level::ERR>(
43 fmt::format("Failed to deserialize, errormsg({})", e.what())
44 .c_str());
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050045 std::filesystem::remove(path);
Jayanth Othayothe4693a72017-09-20 00:01:23 -050046 return false;
47 }
Jayanth Othayoth24964822017-09-04 22:07:06 -050048}
49
50} // namespace elog
51} // namespace dump
52} // namespace phosphor