Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 1 | #include "dump_serialize.hpp" |
| 2 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 3 | #include <cereal/archives/binary.hpp> |
| 4 | #include <cereal/types/set.hpp> |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 6 | |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 7 | #include <fstream> |
| 8 | |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 9 | namespace phosphor |
| 10 | { |
| 11 | namespace dump |
| 12 | { |
| 13 | namespace elog |
| 14 | { |
| 15 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 16 | void serialize(const ElogList& list, const std::filesystem::path& dir) |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 17 | { |
| 18 | std::ofstream os(dir.c_str(), std::ios::binary); |
| 19 | cereal::BinaryOutputArchive oarchive(os); |
| 20 | oarchive(list); |
| 21 | } |
| 22 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 23 | bool deserialize(const std::filesystem::path& path, ElogList& list) |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 24 | { |
Jayanth Othayoth | e4693a7 | 2017-09-20 00:01:23 -0500 | [diff] [blame] | 25 | try |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 26 | { |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 27 | if (std::filesystem::exists(path)) |
Jayanth Othayoth | e4693a7 | 2017-09-20 00:01:23 -0500 | [diff] [blame] | 28 | { |
| 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 Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 35 | } |
Patrick Williams | 9d2d722 | 2021-10-06 12:44:44 -0500 | [diff] [blame] | 36 | catch (const cereal::Exception& e) |
Jayanth Othayoth | e4693a7 | 2017-09-20 00:01:23 -0500 | [diff] [blame] | 37 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 38 | lg2::error("Failed to deserialize, errormsg: {ERROR}", "ERROR", e); |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 39 | std::filesystem::remove(path); |
Jayanth Othayoth | e4693a7 | 2017-09-20 00:01:23 -0500 | [diff] [blame] | 40 | return false; |
| 41 | } |
Jayanth Othayoth | 2496482 | 2017-09-04 22:07:06 -0500 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | } // namespace elog |
| 45 | } // namespace dump |
| 46 | } // namespace phosphor |