1 #pragma once
2 
3 #include <list>
4 #include <sdbusplus/bus.hpp>
5 #include <phosphor-logging/log.hpp>
6 #include "elog_entry.hpp"
7 #include "xyz/openbmc_project/Logging/Internal/Manager/server.hpp"
8 #include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
9 
10 namespace phosphor
11 {
12 namespace logging
13 {
14 
15 extern const std::map<std::string,std::vector<std::string>> g_errMetaMap;
16 extern const std::map<std::string,level> g_errLevelMap;
17 
18 using DeleteAllIface = sdbusplus::server::object::object <
19                        sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll >;
20 
21 namespace details
22 {
23 
24 template <typename T>
25 using ServerObject = typename sdbusplus::server::object::object<T>;
26 
27 using ManagerIface =
28     sdbusplus::xyz::openbmc_project::Logging::Internal::server::Manager;
29 
30 } // namespace details
31 
32 namespace internal
33 {
34 
35 /** @class Manager
36  *  @brief OpenBMC logging manager implementation.
37  *  @details A concrete implementation for the
38  *  xyz.openbmc_project.Logging.Internal.Manager DBus API.
39  */
40 class Manager : public details::ServerObject<details::ManagerIface>
41 {
42     public:
43         Manager() = delete;
44         Manager(const Manager&) = delete;
45         Manager& operator=(const Manager&) = delete;
46         Manager(Manager&&) = delete;
47         Manager& operator=(Manager&&) = delete;
48         virtual ~Manager() = default;
49 
50         /** @brief Constructor to put object onto bus at a dbus path.
51          *  @param[in] bus - Bus to attach to.
52          *  @param[in] path - Path to attach at.
53          */
54         Manager(sdbusplus::bus::bus& bus, const char* objPath) :
55                 details::ServerObject<details::ManagerIface>(bus, objPath),
56                 busLog(bus),
57                 entryId(0){};
58 
59         /*
60          * @fn commit()
61          * @brief sd_bus Commit method implementation callback.
62          * @details Create an error/event log based on transaction id and
63          *          error message.
64          * @param[in] transactionId - Unique identifier of the journal entries
65          *                            to be committed.
66          * @param[in] errMsg - The error exception message associated with the
67          *                     error log to be committed.
68          */
69         void commit(uint64_t transactionId, std::string errMsg) override;
70 
71 
72         /** @brief Erase specified entry d-bus object
73          *
74          * @param[in] entryId - unique identifier of the entry
75          */
76         void erase(uint32_t entryId);
77 
78         /** @brief Construct error d-bus objects from their persisted
79          *         representations.
80          */
81         void restore();
82 
83         /** @brief  Erase all error log entries
84          *
85          */
86         void eraseAll()
87         {
88             auto iter = entries.begin();
89             while (iter != entries.end())
90             {
91                 auto entry = iter->first;
92                 ++iter;
93                 erase(entry);
94             }
95         }
96 
97     private:
98         /** @brief Call metadata handler(s), if any. Handlers may create
99          *         associations.
100          *  @param[in] errorName - name of the error
101          *  @param[in] additionalData - list of metadata (in key=value format)
102          *  @param[out] objects - list of error's association objects
103          */
104         void processMetadata(const std::string& errorName,
105                              const std::vector<std::string>& additionalData,
106                              AssociationList& objects) const;
107 
108         /** @brief Persistent sdbusplus DBus bus connection. */
109         sdbusplus::bus::bus& busLog;
110 
111         /** @brief Persistent map of Entry dbus objects and their ID */
112         std::map<uint32_t, std::unique_ptr<Entry>> entries;
113 
114         /** @brief List of error ids for high severity errors */
115         std::list<uint32_t> realErrors;
116 
117         /** @brief List of error ids for Info(and below) severity */
118         std::list<uint32_t> infoErrors;
119 
120         /** @brief Id of last error log entry */
121         uint32_t entryId;
122 };
123 
124 } //namespace internal
125 
126 /** @class Manager
127  *  @brief Implementation for delete all error log entries.
128  *  @details A concrete implementation for the
129  *  xyz.openbmc_project.Collection.DeleteAll
130  */
131 class Manager : public DeleteAllIface
132 {
133     public:
134         Manager() = delete;
135         Manager(const Manager&) = delete;
136         Manager& operator=(const Manager&) = delete;
137         Manager(Manager&&) = delete;
138         Manager& operator=(Manager&&) = delete;
139         virtual ~Manager() = default;
140 
141         /** @brief Constructor to put object onto bus at a dbus path.
142          *         Defer signal registration (pass true for deferSignal to the
143          *         base class) until after the properties are set.
144          *  @param[in] bus - Bus to attach to.
145          *  @param[in] path - Path to attach at.
146          *  @param[in] manager - Reference to internal manager object.
147          */
148         Manager(sdbusplus::bus::bus& bus,
149                    const std::string& path,
150                    internal::Manager& manager) :
151             DeleteAllIface(bus, path.c_str(), true),
152             manager(manager) {};
153 
154         /** @brief Delete all d-bus objects.
155          */
156         void deleteAll()
157         {
158             manager.eraseAll();
159         }
160     private:
161         /** @brief This is a reference to manager object */
162         internal::Manager& manager;
163 };
164 
165 } // namespace logging
166 } // namespace phosphor
167