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