Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 1 | #include "dump_serialize.hpp" |
| 2 | |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 3 | #include <fmt/core.h> |
| 4 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 5 | #include <cereal/archives/binary.hpp> |
| 6 | #include <cereal/types/set.hpp> |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
| 8 | |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 9 | #include <fstream> |
| 10 | |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace dump |
| 14 | { |
| 15 | namespace elog |
| 16 | { |
| 17 | |
Jayanth Othayoth | e4693a7 | 2017-09-20 00:01:23 -0500 | [diff] [blame] | 18 | using namespace phosphor::logging; |
| 19 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 20 | void serialize(const ElogList& list, const std::filesystem::path& dir) |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 21 | { |
| 22 | std::ofstream os(dir.c_str(), std::ios::binary); |
| 23 | cereal::BinaryOutputArchive oarchive(os); |
| 24 | oarchive(list); |
| 25 | } |
| 26 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 27 | bool deserialize(const std::filesystem::path& path, ElogList& list) |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 28 | { |
Jayanth Othayoth | e4693a7 | 2017-09-20 00:01:23 -0500 | [diff] [blame] | 29 | try |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 30 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 31 | if (std::filesystem::exists(path)) |
Jayanth Othayoth | e4693a7 | 2017-09-20 00:01:23 -0500 | [diff] [blame] | 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; |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 39 | } |
Patrick Williams | 9d2d722 | 2021-10-06 12:44:44 -0500 | [diff] [blame] | 40 | catch (const cereal::Exception& e) |
Jayanth Othayoth | e4693a7 | 2017-09-20 00:01:23 -0500 | [diff] [blame] | 41 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 42 | log<level::ERR>( |
| 43 | fmt::format("Failed to deserialize, errormsg({})", e.what()) |
| 44 | .c_str()); |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 45 | std::filesystem::remove(path); |
Jayanth Othayoth | e4693a7 | 2017-09-20 00:01:23 -0500 | [diff] [blame] | 46 | return false; |
| 47 | } |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | } // namespace elog |
| 51 | } // namespace dump |
| 52 | } // namespace phosphor |