/src/logging-log4cxx/src/main/include/log4cxx/hierarchy.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #ifndef _LOG4CXX_HIERARCHY_H |
19 | | #define _LOG4CXX_HIERARCHY_H |
20 | | |
21 | | |
22 | | #include <log4cxx/spi/loggerrepository.h> |
23 | | #include <log4cxx/spi/loggerfactory.h> |
24 | | #include <log4cxx/provisionnode.h> |
25 | | #include <log4cxx/spi/hierarchyeventlistener.h> |
26 | | |
27 | | namespace LOG4CXX_NS |
28 | | { |
29 | | |
30 | | class Hierarchy; |
31 | | LOG4CXX_PTR_DEF(Hierarchy); |
32 | | |
33 | | /** |
34 | | This class is specialized in retrieving loggers by name and also |
35 | | maintaining the logger hierarchy. |
36 | | |
37 | | <p><em>The casual user does not have to deal with this class |
38 | | directly.</em> |
39 | | |
40 | | <p>The structure of the logger hierarchy is maintained by the |
41 | | #getLogger method. The hierarchy is such that children link |
42 | | to their parent but parents do not have any pointers to their |
43 | | children. Moreover, loggers can be instantiated in any order, in |
44 | | particular descendant before ancestor. |
45 | | |
46 | | <p>In case a descendant is created before a particular ancestor, |
47 | | then it creates a provision node for the ancestor and adds itself |
48 | | to the provision node. Other descendants of the same ancestor add |
49 | | themselves to the previously created provision node. |
50 | | */ |
51 | | class LOG4CXX_EXPORT Hierarchy : public spi::LoggerRepository |
52 | | { |
53 | | private: |
54 | | LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(HierarchyPrivate, m_priv) |
55 | | |
56 | | public: |
57 | | DECLARE_ABSTRACT_LOG4CXX_OBJECT(Hierarchy) |
58 | 0 | BEGIN_LOG4CXX_CAST_MAP() |
59 | | #if 15 < LOG4CXX_ABI_VERSION |
60 | | LOG4CXX_CAST_ENTRY(Hierarchy) |
61 | | LOG4CXX_CAST_ENTRY_CHAIN(spi::LoggerRepository) |
62 | | #else |
63 | 0 | LOG4CXX_CAST_ENTRY(spi::LoggerRepository) |
64 | 0 | #endif |
65 | 0 | END_LOG4CXX_CAST_MAP() |
66 | | |
67 | | private: |
68 | | /** |
69 | | Create a new logger hierarchy. |
70 | | */ |
71 | | Hierarchy(); |
72 | | |
73 | | public: |
74 | | static HierarchyPtr create(); |
75 | | |
76 | | ~Hierarchy(); |
77 | | |
78 | | void addHierarchyEventListener(const spi::HierarchyEventListenerPtr& listener) override; |
79 | | |
80 | | /** |
81 | | * Remove a previously added HierarchyEventListener. |
82 | | * |
83 | | */ |
84 | | #if LOG4CXX_ABI_VERSION <= 15 |
85 | | void removeHierarchyEventListener(const spi::HierarchyEventListenerPtr& listener); |
86 | | #else |
87 | | void removeHierarchyEventListener(const spi::HierarchyEventListenerPtr& listener) override; |
88 | | #endif |
89 | | /** |
90 | | * Call \c configurator if not yet configured. |
91 | | */ |
92 | | void ensureIsConfigured(std::function<void()> configurator) override; |
93 | | |
94 | | /** |
95 | | This call will clear all logger definitions from the internal |
96 | | hashtable. Invoking this method will irrevocably mess up the |
97 | | logger hierarchy. |
98 | | |
99 | | <p>You should <em>really</em> know what you are doing before |
100 | | invoking this method. |
101 | | */ |
102 | | void clear(); |
103 | | |
104 | | void emitNoAppenderWarning(const Logger* logger) override; |
105 | | |
106 | | /** |
107 | | Check if the named logger exists in the hierarchy. If so return |
108 | | its reference, otherwise returns <code>null</code>. |
109 | | |
110 | | @param name The name of the logger to search for. |
111 | | |
112 | | */ |
113 | | LoggerPtr exists(const LogString& name) override; |
114 | | |
115 | | /** |
116 | | The string form of {@link #setThreshold(const LevelPtr&) setThreshold}. |
117 | | */ |
118 | | void setThreshold(const LogString& levelStr) override; |
119 | | |
120 | | /** |
121 | | Enable logging for logging requests with level <code>newLevel</code> or |
122 | | higher. By default all levels are enabled. |
123 | | |
124 | | @param newLevel The minimum level of logging requests that are sent to appenders. |
125 | | */ |
126 | | void setThreshold(const LevelPtr& newLevel) override; |
127 | | |
128 | | void fireAddAppenderEvent(const Logger* logger, const Appender* appender) override; |
129 | | |
130 | | void fireRemoveAppenderEvent(const Logger* logger, const Appender* appender) override; |
131 | | |
132 | | /** |
133 | | Returns a Level representation of the <code>enable</code> |
134 | | state. |
135 | | */ |
136 | | LevelPtr getThreshold() const override; |
137 | | |
138 | | /** |
139 | | Retrieve the \c name Logger instance using |
140 | | the default factory to create it if required. |
141 | | |
142 | | If a logger of that name already exists, then it will be |
143 | | returned. Otherwise, a new logger will be instantiated and |
144 | | then linked with its existing ancestors as well as children. |
145 | | |
146 | | @param name The name of the logger to retrieve. |
147 | | |
148 | | */ |
149 | | LoggerPtr getLogger(const LogString& name) override; |
150 | | |
151 | | /** |
152 | | Retrieve the \c name Logger instance using |
153 | | <code>factory</code> to create it if required. |
154 | | |
155 | | If a logger of that name already exists, then it will be |
156 | | returned. Otherwise, a new logger will be instantiated by the |
157 | | <code>factory</code> parameter and linked with its existing |
158 | | ancestors as well as children. |
159 | | |
160 | | @param name The name of the logger to retrieve. |
161 | | @param factory The factory that will make the new logger instance. |
162 | | |
163 | | */ |
164 | | LoggerPtr getLogger(const LogString& name, |
165 | | const spi::LoggerFactoryPtr& factory) override; |
166 | | |
167 | | /** |
168 | | Returns all the currently defined loggers in this hierarchy as |
169 | | a LoggerList. |
170 | | |
171 | | <p>The root logger is <em>not</em> included in the returned |
172 | | LoggerList. */ |
173 | | LoggerList getCurrentLoggers() const override; |
174 | | |
175 | | /** |
176 | | Get the root of this hierarchy. |
177 | | */ |
178 | | LoggerPtr getRootLogger() const override; |
179 | | |
180 | | /** |
181 | | This method will return <code>true</code> if this repository is |
182 | | disabled for <code>level</code> object passed as parameter and |
183 | | <code>false</code> otherwise. See also the |
184 | | {@link #setThreshold(const LevelPtr&) setThreshold} method. */ |
185 | | bool isDisabled(int level) const override; |
186 | | |
187 | | /** |
188 | | Reset all values contained in this hierarchy instance to their |
189 | | default. This removes all appenders from all categories, sets |
190 | | the level of all non-root categories to <code>null</code>, |
191 | | sets their additivity flag to <code>true</code> and sets the level |
192 | | of the root logger to DEBUG. Moreover, |
193 | | message disabling is set its default "off" value. |
194 | | |
195 | | <p>Existing categories are not removed. They are just reset. |
196 | | |
197 | | <p>This method should be used sparingly and with care as it will |
198 | | block all logging until it is completed.</p> |
199 | | */ |
200 | | void resetConfiguration() override; |
201 | | |
202 | | /** |
203 | | Used by subclasses to add a renderer to the hierarchy passed as parameter. |
204 | | */ |
205 | | /** |
206 | | Shutting down a hierarchy will <em>safely</em> close and remove |
207 | | all appenders in all categories including the root logger. |
208 | | |
209 | | <p>Some appenders such as {@link net::XMLSocketAppender XMLSocketAppender} |
210 | | and AsyncAppender need to be closed before the |
211 | | application exists. Otherwise, pending logging events might be |
212 | | lost. |
213 | | |
214 | | <p>The <code>shutdown</code> method is careful to close nested |
215 | | appenders before closing regular appenders. This is allows |
216 | | configurations where a regular appender is attached to a logger |
217 | | and again to a nested appender. |
218 | | */ |
219 | | void shutdown() override; |
220 | | |
221 | | |
222 | | virtual bool isConfigured() override; |
223 | | virtual void setConfigured(bool configured) override; |
224 | | |
225 | | /** |
226 | | Refresh the threshold in children of parent |
227 | | */ |
228 | | void updateChildren(const Logger* parent); |
229 | | |
230 | | void clearAppenders(); |
231 | | |
232 | | void addAppender(AppenderPtr appender); |
233 | | |
234 | | /** |
235 | | Remove the \c name Logger from the hierarchy. |
236 | | |
237 | | Note: The \c name Logger must be retrieved from the hierarchy |
238 | | \b after any subsequent configuration file change |
239 | | for the newly loaded settings to be used. |
240 | | |
241 | | @param name The logger to remove. |
242 | | @param ifNotUsed If true and use_count() indicates there are other references, do not remove the Logger and return false. |
243 | | @returns true if \c name Logger was removed from the hierarchy. |
244 | | */ |
245 | | #if LOG4CXX_ABI_VERSION <= 15 |
246 | | bool removeLogger(const LogString& name, bool ifNotUsed = true); |
247 | | #else |
248 | | bool removeLogger(const LogString& name, bool ifNotUsed = true) override; |
249 | | #endif |
250 | | |
251 | | private: |
252 | | |
253 | | /** |
254 | | * Set the threshold. The mutex must already be locked. |
255 | | */ |
256 | | void setThresholdInternal(const LevelPtr& l); |
257 | | |
258 | | /** |
259 | | * Internal shutdown. The mutex must already be locked. |
260 | | */ |
261 | | void shutdownInternal(); |
262 | | |
263 | | /** |
264 | | This method loops through all the *potential* parents of |
265 | | \c logger using the logger name. |
266 | | For example, for a logger named "w.x.y.z", |
267 | | loop through "w.x.y", "w.x" and "w", but not "w.x.y.z". |
268 | | There 3 possible cases: |
269 | | |
270 | | 1) No entry for the potential parent of "w.x.y.z" exists |
271 | | |
272 | | We create a ProvisionNode for this potential parent and insert |
273 | | "w.x.y.z" in that provision node. |
274 | | |
275 | | 2) There entry is of type Logger for the potential parent. |
276 | | |
277 | | The entry is "w.x.y.z"'s nearest existing parent. We update "w.x.y.z"'s |
278 | | parent field with this entry. We also break from the loop |
279 | | because updating our parent's parent is our parent's |
280 | | responsibility. |
281 | | |
282 | | 3) There entry is of type ProvisionNode for this potential parent. |
283 | | |
284 | | We add "w.x.y.z" to the list of children for this potential parent. |
285 | | */ |
286 | | void updateParents(const LoggerPtr& logger, const LoggerPtr& root); |
287 | | |
288 | | /** |
289 | | We update the links for all the children that placed themselves |
290 | | in the provision node 'pn'. The \c logger argument is a |
291 | | newly created Logger, a potential parent of all the |
292 | | children in 'pn' |
293 | | |
294 | | We loop on all the children 'c' in 'pn': |
295 | | |
296 | | If the child 'c' has been already linked to a child of |
297 | | 'cat' then there is no need to update 'c'. |
298 | | |
299 | | Otherwise, we set the \c logger parent to 'c's parent and set |
300 | | 'c's parent field to \c logger. |
301 | | */ |
302 | | void updateChildren(ProvisionNode& pn, const LoggerPtr& logger); |
303 | | |
304 | | Hierarchy(const Hierarchy&); |
305 | | Hierarchy& operator=(const Hierarchy&); |
306 | | |
307 | | }; |
308 | | |
309 | | } //namespace log4cxx |
310 | | |
311 | | #endif //_LOG4CXX_HIERARCHY_H |