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