blob: 5340a802aedf371e40c169e8ab41ba56a9609bf8 [file] [log] [blame]
Patrick Venture3d6d3182018-08-31 09:33:09 -07001#include "config.h"
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -05002
3#include "event_serialize.hpp"
Patrick Venture3d6d3182018-08-31 09:33:09 -07004
5#include <cereal/archives/binary.hpp>
6#include <cereal/types/string.hpp>
7#include <cereal/types/vector.hpp>
George Liu13e3df62022-06-22 15:03:06 +08008#include <phosphor-logging/lg2.hpp>
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -05009
George Liu3fe976c2022-06-21 09:37:04 +080010#include <fstream>
11
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050012// Register class version
13// From cereal documentation;
14// "This macro should be placed at global scope"
15CEREAL_CLASS_VERSION(phosphor::events::Entry, CLASS_VERSION);
16
17namespace phosphor
18{
19namespace events
20{
21
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050022/** @brief Function required by Cereal to perform serialization.
23 * @tparam Archive - Cereal archive type (binary in our case).
24 * @param[in] archive - reference to Cereal archive.
25 * @param[in] event - const reference to event entry.
26 * @param[in] version - Class version that enables handling
27 * a serialized data across code levels
28 */
29template <class Archive>
George Liu5e6b51d2022-06-21 16:59:39 +080030void save(Archive& archive, const Entry& event,
31 const std::uint32_t /* version */)
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050032{
33 archive(event.timestamp(), event.message(), event.additionalData());
34}
35
36/** @brief Function required by Cereal to perform deserialization.
37 * @tparam Archive - Cereal archive type (binary in our case).
38 * @param[in] archive - reference to Cereal archive.
39 * @param[in] event - reference to event entry.
40 * @param[in] version - Class version that enables handling
41 * a serialized data across code levels
42 */
43template <class Archive>
George Liu5e6b51d2022-06-21 16:59:39 +080044void load(Archive& archive, Entry& event, const std::uint32_t /* version */)
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050045{
46 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
47
48 uint64_t timestamp{};
49 std::string message{};
50 std::vector<std::string> additionalData{};
51
52 archive(timestamp, message, additionalData);
53
54 event.timestamp(timestamp);
55 event.message(message);
56 event.additionalData(additionalData);
57}
58
59fs::path serialize(const Entry& event, const std::string& eventName)
60{
61 fs::path dir(EVENTS_PERSIST_PATH);
62 auto path = dir / eventName;
63 fs::create_directories(path);
64 path /= std::to_string(event.timestamp());
65 std::ofstream os(path.string(), std::ios::binary);
66 cereal::BinaryOutputArchive oarchive(os);
67 oarchive(event);
68 return path;
69}
70
71bool deserialize(const fs::path& path, Entry& event)
72{
73 try
74 {
75 if (fs::exists(path))
76 {
77 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
78 cereal::BinaryInputArchive iarchive(is);
79 iarchive(event);
80 return true;
81 }
82 return false;
83 }
Patrick Williamsa95e8c82021-10-06 12:30:08 -050084 catch (const cereal::Exception& e)
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050085 {
George Liu13e3df62022-06-22 15:03:06 +080086 lg2::error("Failed to deserialize: {ERROR}", "ERROR", e);
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050087 std::error_code ec;
88 fs::remove(path, ec);
89 return false;
90 }
91 catch (const fs::filesystem_error& e)
92 {
93 return false;
94 }
95}
96
Patrick Ventureac803952018-08-31 09:28:31 -070097} // namespace events
Dhruvaraj Subhashchandran6524b9d2017-10-18 01:41:51 -050098} // namespace phosphor