xref: /openbmc/phosphor-dbus-monitor/src/event_serialize.cpp (revision 13e3df602acb51a4505fce2fab1cedd04f010e7b)
13d6d3182SPatrick Venture #include "config.h"
26524b9d8SDhruvaraj Subhashchandran 
36524b9d8SDhruvaraj Subhashchandran #include "event_serialize.hpp"
43d6d3182SPatrick Venture 
53d6d3182SPatrick Venture #include <cereal/archives/binary.hpp>
63d6d3182SPatrick Venture #include <cereal/types/string.hpp>
73d6d3182SPatrick Venture #include <cereal/types/vector.hpp>
8*13e3df60SGeorge Liu #include <phosphor-logging/lg2.hpp>
96524b9d8SDhruvaraj Subhashchandran 
103fe976ccSGeorge Liu #include <fstream>
113fe976ccSGeorge Liu 
126524b9d8SDhruvaraj Subhashchandran // Register class version
136524b9d8SDhruvaraj Subhashchandran // From cereal documentation;
146524b9d8SDhruvaraj Subhashchandran // "This macro should be placed at global scope"
156524b9d8SDhruvaraj Subhashchandran CEREAL_CLASS_VERSION(phosphor::events::Entry, CLASS_VERSION);
166524b9d8SDhruvaraj Subhashchandran 
176524b9d8SDhruvaraj Subhashchandran namespace phosphor
186524b9d8SDhruvaraj Subhashchandran {
196524b9d8SDhruvaraj Subhashchandran namespace events
206524b9d8SDhruvaraj Subhashchandran {
216524b9d8SDhruvaraj Subhashchandran 
226524b9d8SDhruvaraj Subhashchandran /** @brief Function required by Cereal to perform serialization.
236524b9d8SDhruvaraj Subhashchandran  *  @tparam Archive - Cereal archive type (binary in our case).
246524b9d8SDhruvaraj Subhashchandran  *  @param[in] archive - reference to Cereal archive.
256524b9d8SDhruvaraj Subhashchandran  *  @param[in] event - const reference to event entry.
266524b9d8SDhruvaraj Subhashchandran  *  @param[in] version - Class version that enables handling
276524b9d8SDhruvaraj Subhashchandran  *                       a serialized data across code levels
286524b9d8SDhruvaraj Subhashchandran  */
296524b9d8SDhruvaraj Subhashchandran template <class Archive>
save(Archive & archive,const Entry & event,const std::uint32_t)305e6b51d7SGeorge Liu void save(Archive& archive, const Entry& event,
315e6b51d7SGeorge Liu           const std::uint32_t /* version */)
326524b9d8SDhruvaraj Subhashchandran {
336524b9d8SDhruvaraj Subhashchandran     archive(event.timestamp(), event.message(), event.additionalData());
346524b9d8SDhruvaraj Subhashchandran }
356524b9d8SDhruvaraj Subhashchandran 
366524b9d8SDhruvaraj Subhashchandran /** @brief Function required by Cereal to perform deserialization.
376524b9d8SDhruvaraj Subhashchandran  *  @tparam Archive - Cereal archive type (binary in our case).
386524b9d8SDhruvaraj Subhashchandran  *  @param[in] archive - reference to Cereal archive.
396524b9d8SDhruvaraj Subhashchandran  *  @param[in] event - reference to event entry.
406524b9d8SDhruvaraj Subhashchandran  *  @param[in] version - Class version that enables handling
416524b9d8SDhruvaraj Subhashchandran  *                       a serialized data across code levels
426524b9d8SDhruvaraj Subhashchandran  */
436524b9d8SDhruvaraj Subhashchandran template <class Archive>
load(Archive & archive,Entry & event,const std::uint32_t)445e6b51d7SGeorge Liu void load(Archive& archive, Entry& event, const std::uint32_t /* version */)
456524b9d8SDhruvaraj Subhashchandran {
466524b9d8SDhruvaraj Subhashchandran     using namespace sdbusplus::xyz::openbmc_project::Logging::server;
476524b9d8SDhruvaraj Subhashchandran 
486524b9d8SDhruvaraj Subhashchandran     uint64_t timestamp{};
496524b9d8SDhruvaraj Subhashchandran     std::string message{};
506524b9d8SDhruvaraj Subhashchandran     std::vector<std::string> additionalData{};
516524b9d8SDhruvaraj Subhashchandran 
526524b9d8SDhruvaraj Subhashchandran     archive(timestamp, message, additionalData);
536524b9d8SDhruvaraj Subhashchandran 
546524b9d8SDhruvaraj Subhashchandran     event.timestamp(timestamp);
556524b9d8SDhruvaraj Subhashchandran     event.message(message);
566524b9d8SDhruvaraj Subhashchandran     event.additionalData(additionalData);
576524b9d8SDhruvaraj Subhashchandran }
586524b9d8SDhruvaraj Subhashchandran 
serialize(const Entry & event,const std::string & eventName)596524b9d8SDhruvaraj Subhashchandran fs::path serialize(const Entry& event, const std::string& eventName)
606524b9d8SDhruvaraj Subhashchandran {
616524b9d8SDhruvaraj Subhashchandran     fs::path dir(EVENTS_PERSIST_PATH);
626524b9d8SDhruvaraj Subhashchandran     auto path = dir / eventName;
636524b9d8SDhruvaraj Subhashchandran     fs::create_directories(path);
646524b9d8SDhruvaraj Subhashchandran     path /= std::to_string(event.timestamp());
656524b9d8SDhruvaraj Subhashchandran     std::ofstream os(path.string(), std::ios::binary);
666524b9d8SDhruvaraj Subhashchandran     cereal::BinaryOutputArchive oarchive(os);
676524b9d8SDhruvaraj Subhashchandran     oarchive(event);
686524b9d8SDhruvaraj Subhashchandran     return path;
696524b9d8SDhruvaraj Subhashchandran }
706524b9d8SDhruvaraj Subhashchandran 
deserialize(const fs::path & path,Entry & event)716524b9d8SDhruvaraj Subhashchandran bool deserialize(const fs::path& path, Entry& event)
726524b9d8SDhruvaraj Subhashchandran {
736524b9d8SDhruvaraj Subhashchandran     try
746524b9d8SDhruvaraj Subhashchandran     {
756524b9d8SDhruvaraj Subhashchandran         if (fs::exists(path))
766524b9d8SDhruvaraj Subhashchandran         {
776524b9d8SDhruvaraj Subhashchandran             std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
786524b9d8SDhruvaraj Subhashchandran             cereal::BinaryInputArchive iarchive(is);
796524b9d8SDhruvaraj Subhashchandran             iarchive(event);
806524b9d8SDhruvaraj Subhashchandran             return true;
816524b9d8SDhruvaraj Subhashchandran         }
826524b9d8SDhruvaraj Subhashchandran         return false;
836524b9d8SDhruvaraj Subhashchandran     }
84a95e8c81SPatrick Williams     catch (const cereal::Exception& e)
856524b9d8SDhruvaraj Subhashchandran     {
86*13e3df60SGeorge Liu         lg2::error("Failed to deserialize: {ERROR}", "ERROR", e);
876524b9d8SDhruvaraj Subhashchandran         std::error_code ec;
886524b9d8SDhruvaraj Subhashchandran         fs::remove(path, ec);
896524b9d8SDhruvaraj Subhashchandran         return false;
906524b9d8SDhruvaraj Subhashchandran     }
916524b9d8SDhruvaraj Subhashchandran     catch (const fs::filesystem_error& e)
926524b9d8SDhruvaraj Subhashchandran     {
936524b9d8SDhruvaraj Subhashchandran         return false;
946524b9d8SDhruvaraj Subhashchandran     }
956524b9d8SDhruvaraj Subhashchandran }
966524b9d8SDhruvaraj Subhashchandran 
97ac803959SPatrick Venture } // namespace events
986524b9d8SDhruvaraj Subhashchandran } // namespace phosphor
99