blob: c10e6a0a73b32ae251e58efad0a48d73da22e27a [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 {
William A. Kennington IIIaeccabc2021-05-19 16:39:10 -0700118 getStartupFunctions().push_back(func);
Matt Spinler99c2b402019-05-23 14:29:16 -0500119 }
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 {
William A. Kennington IIIaeccabc2021-05-19 16:39:10 -0700131 getCreateFunctions().push_back(func);
Matt Spinler99c2b402019-05-23 14:29:16 -0500132 }
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 {
William A. Kennington IIIaeccabc2021-05-19 16:39:10 -0700144 getDeleteFunctions().push_back(func);
Matt Spinler99c2b402019-05-23 14:29:16 -0500145 }
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 {
William A. Kennington IIIaeccabc2021-05-19 16:39:10 -0700158 getDeleteProhibitedFunctions().push_back(func);
Matt Spinler99c2b402019-05-23 14:29:16 -0500159 }
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 {
William A. Kennington IIIaeccabc2021-05-19 16:39:10 -0700172 getDefaultErrorCaps() = defaultCaps;
Matt Spinler99c2b402019-05-23 14:29:16 -0500173 }
174
175 /**
176 * @brief Returns the Startup functions
177 * @return StartupFunctions - the Startup functions
178 */
William A. Kennington IIIaeccabc2021-05-19 16:39:10 -0700179 static StartupFunctions& getStartupFunctions();
Matt Spinler99c2b402019-05-23 14:29:16 -0500180
181 /**
182 * @brief Returns the Create functions
183 * @return CreateFunctions - the Create functions
184 */
William A. Kennington IIIaeccabc2021-05-19 16:39:10 -0700185 static CreateFunctions& getCreateFunctions();
Matt Spinler99c2b402019-05-23 14:29:16 -0500186
187 /**
188 * @brief Returns the Delete functions
189 * @return DeleteFunctions - the Delete functions
190 */
William A. Kennington IIIaeccabc2021-05-19 16:39:10 -0700191 static DeleteFunctions& getDeleteFunctions();
Matt Spinler99c2b402019-05-23 14:29:16 -0500192
193 /**
194 * @brief Returns the DeleteProhibited functions
195 * @return DeleteProhibitedFunctions - the DeleteProhibited functions
196 */
William A. Kennington IIIaeccabc2021-05-19 16:39:10 -0700197 static DeleteProhibitedFunctions& getDeleteProhibitedFunctions();
198
199 /**
200 * @brief Returns the DefaultErrorCaps value
201 * @return DefaultErrorCaps - the DefaultErrorCaps value
202 */
203 static DefaultErrorCaps& getDefaultErrorCaps();
Matt Spinler99c2b402019-05-23 14:29:16 -0500204
205 /**
206 * @brief Say if the default log capping policy should be disabled
207 * @return bool - true if it should be disabled
208 */
209 static bool disableDefaultLogCaps()
210 {
William A. Kennington IIIaeccabc2021-05-19 16:39:10 -0700211 return getDefaultErrorCaps() == DefaultErrorCaps::disable;
Matt Spinler99c2b402019-05-23 14:29:16 -0500212 }
Matt Spinler99c2b402019-05-23 14:29:16 -0500213};
214
215} // namespace logging
216} // namespace phosphor