1 #include "dump_serialize.hpp" 2 3 #include <fmt/core.h> 4 5 #include <cereal/archives/binary.hpp> 6 #include <cereal/types/set.hpp> 7 #include <phosphor-logging/log.hpp> 8 9 #include <fstream> 10 11 namespace phosphor 12 { 13 namespace dump 14 { 15 namespace elog 16 { 17 18 using namespace phosphor::logging; 19 20 void serialize(const ElogList& list, const std::filesystem::path& dir) 21 { 22 std::ofstream os(dir.c_str(), std::ios::binary); 23 cereal::BinaryOutputArchive oarchive(os); 24 oarchive(list); 25 } 26 27 bool deserialize(const std::filesystem::path& path, ElogList& list) 28 { 29 try 30 { 31 if (std::filesystem::exists(path)) 32 { 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; 39 } 40 catch (const cereal::Exception& e) 41 { 42 log<level::ERR>( 43 fmt::format("Failed to deserialize, errormsg({})", e.what()) 44 .c_str()); 45 std::filesystem::remove(path); 46 return false; 47 } 48 } 49 50 } // namespace elog 51 } // namespace dump 52 } // namespace phosphor 53