xref: /openbmc/ibm-logging/callout.hpp (revision 8123a713)
1 #pragma once
2 
3 #include "dbus.hpp"
4 #include "interfaces.hpp"
5 
6 #include <experimental/filesystem>
7 #include <string>
8 
9 namespace ibm
10 {
11 namespace logging
12 {
13 
14 namespace fs = std::experimental::filesystem;
15 
16 /**
17  *  @class Callout
18  *
19  *  This class provides information about a callout by utilizing the
20  *  xyz.openbmc_project.Inventory.Decorator.Asset and
21  *  xyz.openbmc_project.Common.ObjectPath interfaces.
22  *
23  *  It also has the ability to persist and restore its data.
24  */
25 class Callout : public CalloutObject
26 {
27   public:
28     Callout() = delete;
29     Callout(const Callout&) = delete;
30     Callout& operator=(const Callout&) = delete;
31     Callout(Callout&&) = default;
32     Callout& operator=(Callout&&) = default;
33     ~Callout() = default;
34 
35     /**
36      * Constructor
37      *
38      * Populates the Asset D-Bus properties with data from the property map.
39      *
40      * @param[in] bus - D-Bus object
41      * @param[in] objectPath - object path
42      * @param[in] inventoryPath - inventory path of the callout
43      * @param[in] id - which callout this is
44      * @param[in] timestamp - timestamp when the log was created
45      * @param[in] properties - the properties for the Asset interface.
46      */
47     Callout(sdbusplus::bus_t& bus, const std::string& objectPath,
48             const std::string& inventoryPath, size_t id, uint64_t timestamp,
49             const DbusPropertyMap& properties);
50     /**
51      * Constructor
52      *
53      * This version is for when the object is being restored and does
54      * not take the properties map.
55      *
56      * @param[in] bus - D-Bus object
57      * @param[in] objectPath - object path
58      * @param[in] id - which callout this is
59      * @param[in] timestamp - timestamp when the log was created
60      * @param[in] properties - the properties for the Asset interface.
61      */
62     Callout(sdbusplus::bus_t& bus, const std::string& objectPath, size_t id,
63             uint64_t timestamp);
64 
65     /**
66      * Returns the callout ID
67      *
68      * @return id - the ID
69      */
id() const70     inline auto id() const
71     {
72         return entryID;
73     }
74 
75     /**
76      * Sets the callout ID
77      *
78      * @param[in] id - the ID
79      */
id(uint32_t id)80     inline void id(uint32_t id)
81     {
82         entryID = id;
83     }
84 
85     /**
86      * Returns the timestamp
87      *
88      * @return timestamp
89      */
ts() const90     inline auto ts() const
91     {
92         return timestamp;
93     }
94 
95     /**
96      * Sets the timestamp
97      *
98      * @param[in] ts - the timestamp
99      */
ts(uint64_t ts)100     inline void ts(uint64_t ts)
101     {
102         timestamp = ts;
103     }
104 
105     /**
106      * Serializes the class instance into a file in the
107      * directory passed in.  The filename will match the
108      * ID value passed into the constructor.
109      *
110      * @param[in] - the directory to save the file  in.
111      */
112     void serialize(const fs::path& dir);
113 
114     /**
115      * Loads the class members in from a file written by a previous
116      * call to serialize().  The filename it uses is the ID
117      * value passed into the constructor in the directory
118      * passed to this function.
119      *
120      * @param[in] dir - the directory to look for the file in
121      *
122      * @return bool - true if the deserialization was successful,
123      *                false if it wasn't
124      */
125     bool deserialize(const fs::path& dir);
126 
127   private:
128     /**
129      * Returns the fully qualified filename to use for the serialization
130      * data.  The file is the ID value, like "0", in the base directory
131      * passed in.
132      *
133      * @param[in] baseDir - the directory the file will be in
134      *
135      * @return path - the filename
136      */
getFilePath(const fs::path & baseDir)137     inline auto getFilePath(const fs::path& baseDir)
138     {
139         return baseDir / std::to_string(entryID);
140     }
141 
142     /**
143      * The unique identifier for the callout, as error logs can have
144      * multiple callouts.  They start at 0.
145      */
146     size_t entryID;
147 
148     /**
149      * The timestamp of when the error log was created.
150      * Used for ensuring the callout data is being restored for
151      * the correct error log.
152      */
153     uint64_t timestamp;
154 };
155 } // namespace logging
156 } // namespace ibm
157