1 #pragma once
2 
3 #include "elog_entry.hpp"
4 #include "log_manager.hpp"
5 
6 #include <functional>
7 #include <vector>
8 
9 namespace phosphor
10 {
11 namespace logging
12 {
13 
14 /**
15  * @brief The function type that will be called on start up.
16  * @param[in] internal::Manager& - A reference to the Manager class.
17  */
18 using StartupFunction = std::function<void(internal::Manager&)>;
19 
20 using AdditionalDataArg = std::vector<std::string>;
21 using AssociationEndpointsArg = std::vector<std::string>;
22 /**
23  *  @brief The function type that will be called after an event log
24  *         is created.
25  * @param[in] const std::string& - The Message property
26  * @param[in] uin32_t - The event log ID
27  * @param[in] uint64_t - The event log timestamp
28  * @param[in] Level - The event level
29  * @param[in] const AdditionalDataArg&) - the additional data
30  * @param[in] const AssociationEndpoints& - Association endpoints (callouts)
31  */
32 using CreateFunction = std::function<void(
33     const std::string&, uint32_t, uint64_t, Entry::Level,
34     const AdditionalDataArg&, const AssociationEndpointsArg&)>;
35 
36 /**
37  * @brief The function type that will be called after an event log is deleted.
38  * @param[in] uint32_t - The event log ID
39  */
40 using DeleteFunction = std::function<void(uint32_t)>;
41 
42 /**
43  * @brief The function type that will to check if an event log is prohibited
44  *        from being deleted.
45  * @param[in] uint32_t - The event log ID
46  * @param[out] bool - set to true if the delete is prohibited
47  */
48 using DeleteProhibitedFunction = std::function<void(uint32_t, bool&)>;
49 
50 using StartupFunctions = std::vector<StartupFunction>;
51 using CreateFunctions = std::vector<CreateFunction>;
52 using DeleteFunctions = std::vector<DeleteFunction>;
53 using DeleteProhibitedFunctions = std::vector<DeleteProhibitedFunction>;
54 
55 /**
56  * @brief Register an extension hook function
57  *
58  * Call this macro at global scope to register a hook to call.
59  * Each hook point has a unique function prototype.
60  */
61 #define REGISTER_EXTENSION_FUNCTION(func)                                      \
62     namespace func##_ns                                                        \
63     {                                                                          \
64         Extensions e{func};                                                    \
65     }
66 
67 /**
68  * @brief Disable default error log capping
69  *
70  * Call this macro at global scope to tell phosphor-logging to disable its
71  * default error log capping algorithm, so that an extension can use its own
72  * instead.
73  */
74 #define DISABLE_LOG_ENTRY_CAPS()                                               \
75     namespace disable_caps##_ns                                                \
76     {                                                                          \
77         Extensions e{Extensions::DefaultErrorCaps::disable};                   \
78     }
79 
80 /**
81  * @class Extensions
82  *
83  * This class manages any error log extensions.  Extensions can register
84  * their hook functions with this class with the provided macros so that they
85  * are then able to create their own types of logs based on the native logs.
86  *
87  * The class should only be constructed at a global scope via the macros.
88  */
89 class Extensions
90 {
91   public:
92     Extensions() = delete;
93     ~Extensions() = default;
94     Extensions(const Extensions&) = delete;
95     Extensions& operator=(const Extensions&) = delete;
96     Extensions(Extensions&&) = delete;
97     Extensions& operator=(Extensions&&) = delete;
98 
99     enum class DefaultErrorCaps
100     {
101         disable,
102         enable
103     };
104 
105     /**
106      * @brief Constructor to register a startup function
107      *
108      * Functions registered with this contructor will be called
109      * when phosphor-log-manager starts up.
110      *
111      * @param[in] func - The startup function to register
112      */
113     explicit Extensions(StartupFunction func)
114     {
115         startupFunctions.push_back(func);
116     }
117 
118     /**
119      * @brief Constructor to register a create function
120      *
121      * Functions registered with this contructor will be called
122      * after phosphor-log-manager creates an event log.
123      *
124      * @param[in] func - The create function to register
125      */
126     explicit Extensions(CreateFunction func)
127     {
128         createFunctions.push_back(func);
129     }
130 
131     /**
132      * @brief Constructor to register a delete function
133      *
134      * Functions registered with this contructor will be called
135      * after phosphor-log-manager deletes an event log.
136      *
137      * @param[in] func - The delete function to register
138      */
139     explicit Extensions(DeleteFunction func)
140     {
141         deleteFunctions.push_back(func);
142     }
143 
144     /**
145      * @brief Constructor to register a delete prohibition function
146      *
147      * Functions registered with this contructor will be called
148      * before phosphor-log-manager deletes an event log to ensure
149      * deleting the log is allowed.
150      *
151      * @param[in] func - The function to register
152      */
153     explicit Extensions(DeleteProhibitedFunction func)
154     {
155         deleteProhibitedFunctions.push_back(func);
156     }
157 
158     /**
159      * @brief Constructor to disable event log capping
160      *
161      * This constructor should only be called by the
162      * DISABLE_LOG_ENTRY_CAPS macro to disable the default
163      * event log capping so that the extension can use their own.
164      *
165      * @param[in] defaultCaps - Enable or disable default capping.
166      */
167     explicit Extensions(DefaultErrorCaps defaultCaps)
168     {
169         defaultErrorCaps = defaultCaps;
170     }
171 
172     /**
173      * @brief Returns the Startup functions
174      * @return StartupFunctions - the Startup functions
175      */
176     static StartupFunctions& getStartupFunctions()
177     {
178         return startupFunctions;
179     }
180 
181     /**
182      * @brief Returns the Create functions
183      * @return CreateFunctions - the Create functions
184      */
185     static CreateFunctions& getCreateFunctions()
186     {
187         return createFunctions;
188     }
189 
190     /**
191      * @brief Returns the Delete functions
192      * @return DeleteFunctions - the Delete functions
193      */
194     static DeleteFunctions& getDeleteFunctions()
195     {
196         return deleteFunctions;
197     }
198 
199     /**
200      * @brief Returns the DeleteProhibited functions
201      * @return DeleteProhibitedFunctions - the DeleteProhibited functions
202      */
203     static DeleteProhibitedFunctions& getDeleteProhibitedFunctions()
204     {
205         return deleteProhibitedFunctions;
206     }
207 
208     /**
209      * @brief Say if the default log capping policy should be disabled
210      * @return bool - true if it should be disabled
211      */
212     static bool disableDefaultLogCaps()
213     {
214         return defaultErrorCaps == DefaultErrorCaps::disable;
215     }
216 
217   private:
218     /**
219      * @brief Vector of functions to call on app startup.
220      */
221     static StartupFunctions startupFunctions;
222 
223     /**
224      * @brief Vector of functions to call after creating an event log.
225      */
226     static CreateFunctions createFunctions;
227 
228     /**
229      * @brief Vector of functions to call after deleting an event log.
230      */
231     static DeleteFunctions deleteFunctions;
232 
233     /**
234      * @brief Vector of functions to call to check if deleting a
235      *        particular event log is prohibited.
236      */
237     static DeleteProhibitedFunctions deleteProhibitedFunctions;
238 
239     /**
240      * @brief If default log capping should be disabled.
241      */
242     static DefaultErrorCaps defaultErrorCaps;
243 };
244 
245 } // namespace logging
246 } // namespace phosphor
247