/src/logging-log4cxx/src/main/cpp/domconfigurator.cpp
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 | | #include <log4cxx/private/string_c11.h> |
18 | | #include <log4cxx/logstring.h> |
19 | | #include <log4cxx/xml/domconfigurator.h> |
20 | | #include <log4cxx/appender.h> |
21 | | #include <log4cxx/layout.h> |
22 | | #include <log4cxx/logger.h> |
23 | | #include <log4cxx/logmanager.h> |
24 | | #include <log4cxx/level.h> |
25 | | #include <log4cxx/spi/filter.h> |
26 | | #include <log4cxx/helpers/loglog.h> |
27 | | #include <log4cxx/helpers/stringhelper.h> |
28 | | #include <log4cxx/helpers/loader.h> |
29 | | #include <log4cxx/helpers/optionconverter.h> |
30 | | #include <log4cxx/config/propertysetter.h> |
31 | | #include <log4cxx/spi/errorhandler.h> |
32 | | #include <log4cxx/spi/loggerfactory.h> |
33 | | #if LOG4CXX_ABI_VERSION <= 15 |
34 | | #include <log4cxx/defaultloggerfactory.h> |
35 | | #else |
36 | | #include <log4cxx/spi/loggerfactory.h> |
37 | | #endif |
38 | | #include <log4cxx/helpers/filewatchdog.h> |
39 | | #include <log4cxx/spi/loggerrepository.h> |
40 | | #include <log4cxx/spi/loggingevent.h> |
41 | | #include <log4cxx/helpers/pool.h> |
42 | | #include <sstream> |
43 | | #include <log4cxx/helpers/transcoder.h> |
44 | | #include <log4cxx/rolling/rollingfileappender.h> |
45 | | #include <log4cxx/rolling/filterbasedtriggeringpolicy.h> |
46 | | #include <apr_xml.h> |
47 | | #include <log4cxx/helpers/bytebuffer.h> |
48 | | #include <log4cxx/helpers/charsetdecoder.h> |
49 | | #include <log4cxx/net/smtpappender.h> |
50 | | #include <log4cxx/helpers/messagebuffer.h> |
51 | | #include <log4cxx/helpers/threadutility.h> |
52 | | |
53 | | #define LOG4CXX 1 |
54 | | #include <log4cxx/helpers/aprinitializer.h> |
55 | | |
56 | | using namespace LOG4CXX_NS; |
57 | | using namespace LOG4CXX_NS::xml; |
58 | | using namespace LOG4CXX_NS::helpers; |
59 | | using namespace LOG4CXX_NS::spi; |
60 | | using namespace LOG4CXX_NS::config; |
61 | | using namespace LOG4CXX_NS::rolling; |
62 | | |
63 | 0 | #define MAX_ATTRIBUTE_NAME_LEN 2000 |
64 | | |
65 | | struct DOMConfigurator::DOMConfiguratorPrivate |
66 | | { |
67 | | helpers::Properties props; |
68 | | spi::LoggerRepositoryPtr repository; |
69 | | spi::LoggerFactoryPtr loggerFactory; |
70 | | }; |
71 | | |
72 | | namespace LOG4CXX_NS |
73 | | { |
74 | | namespace xml |
75 | | { |
76 | | class XMLWatchdog : public FileWatchdog |
77 | | { |
78 | | public: |
79 | 0 | XMLWatchdog(const File& filename) : FileWatchdog(filename) |
80 | 0 | { |
81 | 0 | } |
82 | | |
83 | | /** |
84 | | Call DOMConfigurator#doConfigure with the |
85 | | <code>filename</code> to reconfigure log4cxx. |
86 | | */ |
87 | | void doOnChange() |
88 | 0 | { |
89 | 0 | DOMConfigurator().doConfigure(file(), |
90 | 0 | LogManager::getLoggerRepository()); |
91 | 0 | } |
92 | | }; |
93 | | } |
94 | | } |
95 | | XMLWatchdog* DOMConfigurator::xdog = NULL; |
96 | | |
97 | | |
98 | | IMPLEMENT_LOG4CXX_OBJECT(DOMConfigurator) |
99 | | |
100 | 0 | #define CONFIGURATION_TAG "log4j:configuration" |
101 | 0 | #define OLD_CONFIGURATION_TAG "configuration" |
102 | 0 | #define APPENDER_TAG "appender" |
103 | 0 | #define APPENDER_REF_TAG "appender-ref" |
104 | 0 | #define PARAM_TAG "param" |
105 | 0 | #define LAYOUT_TAG "layout" |
106 | 0 | #define ROLLING_POLICY_TAG "rollingPolicy" |
107 | 0 | #define TRIGGERING_POLICY_TAG "triggeringPolicy" |
108 | 0 | #define CATEGORY "category" |
109 | 0 | #define LOGGER "logger" |
110 | 0 | #define LOGGER_REF "logger-ref" |
111 | 0 | #define CATEGORY_FACTORY_TAG "categoryFactory" |
112 | 0 | #define NAME_ATTR "name" |
113 | 0 | #define CLASS_ATTR "class" |
114 | 0 | #define VALUE_ATTR "value" |
115 | 0 | #define ROOT_TAG "root" |
116 | 0 | #define ROOT_REF "root-ref" |
117 | 0 | #define LEVEL_TAG "level" |
118 | 0 | #define PRIORITY_TAG "priority" |
119 | 0 | #define FILTER_TAG "filter" |
120 | 0 | #define ERROR_HANDLER_TAG "errorHandler" |
121 | 0 | #define REF_ATTR "ref" |
122 | 0 | #define ADDITIVITY_ATTR "additivity" |
123 | 0 | #define THRESHOLD_ATTR "threshold" |
124 | | #define STRINGSTREAM_ATTR "stringstream" |
125 | 0 | #define CONFIG_DEBUG_ATTR "configDebug" |
126 | 0 | #define INTERNAL_DEBUG_ATTR "debug" |
127 | 0 | #define THREAD_CONFIG_ATTR "threadConfiguration" |
128 | | |
129 | | DOMConfigurator::DOMConfigurator() |
130 | 0 | : m_priv(std::make_unique<DOMConfiguratorPrivate>()) |
131 | 0 | { |
132 | 0 | } Unexecuted instantiation: log4cxx::xml::DOMConfigurator::DOMConfigurator() Unexecuted instantiation: log4cxx::xml::DOMConfigurator::DOMConfigurator() |
133 | | |
134 | 0 | DOMConfigurator::~DOMConfigurator() {} |
135 | | |
136 | | /** |
137 | | Used internally to parse appenders by IDREF name. |
138 | | */ |
139 | | AppenderPtr DOMConfigurator::findAppenderByName(LOG4CXX_NS::helpers::Pool& p, |
140 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
141 | | apr_xml_elem* element, |
142 | | apr_xml_doc* doc, |
143 | | const LogString& appenderName, |
144 | | AppenderMap& appenders) |
145 | 0 | { |
146 | 0 | AppenderPtr appender; |
147 | 0 | std::string tagName(element->name); |
148 | |
|
149 | 0 | if (tagName == APPENDER_TAG) |
150 | 0 | { |
151 | 0 | if (appenderName == getAttribute(utf8Decoder, element, NAME_ATTR)) |
152 | 0 | { |
153 | 0 | appender = parseAppender(p, utf8Decoder, element, doc, appenders); |
154 | 0 | } |
155 | 0 | } |
156 | |
|
157 | 0 | if (element->first_child && !appender) |
158 | 0 | { |
159 | 0 | appender = findAppenderByName(p, utf8Decoder, element->first_child, doc, appenderName, appenders); |
160 | 0 | } |
161 | |
|
162 | 0 | if (element->next && !appender) |
163 | 0 | { |
164 | 0 | appender = findAppenderByName(p, utf8Decoder, element->next, doc, appenderName, appenders); |
165 | 0 | } |
166 | |
|
167 | 0 | return appender; |
168 | 0 | } |
169 | | |
170 | | /** |
171 | | Used internally to parse appenders by IDREF element. |
172 | | */ |
173 | | AppenderPtr DOMConfigurator::findAppenderByReference( |
174 | | LOG4CXX_NS::helpers::Pool& p, |
175 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
176 | | apr_xml_elem* appenderRef, |
177 | | apr_xml_doc* doc, |
178 | | AppenderMap& appenders) |
179 | 0 | { |
180 | 0 | LogString appenderName(subst(getAttribute(utf8Decoder, appenderRef, REF_ATTR))); |
181 | 0 | AppenderMap::const_iterator match = appenders.find(appenderName); |
182 | 0 | AppenderPtr appender; |
183 | |
|
184 | 0 | if (match != appenders.end()) |
185 | 0 | { |
186 | 0 | appender = match->second; |
187 | 0 | } |
188 | 0 | else if (doc) |
189 | 0 | { |
190 | 0 | appender = findAppenderByName(p, utf8Decoder, doc->root, doc, appenderName, appenders); |
191 | |
|
192 | 0 | if (appender) |
193 | 0 | { |
194 | 0 | appenders.insert(AppenderMap::value_type(appenderName, appender)); |
195 | 0 | } |
196 | 0 | } |
197 | |
|
198 | 0 | if (!appender) |
199 | 0 | { |
200 | 0 | LogLog::error(LOG4CXX_STR("No appender named [") + |
201 | 0 | appenderName + LOG4CXX_STR("] could be found.")); |
202 | 0 | } |
203 | |
|
204 | 0 | return appender; |
205 | 0 | } |
206 | | |
207 | | /** |
208 | | Used internally to parse an appender element. |
209 | | */ |
210 | | AppenderPtr DOMConfigurator::parseAppender(Pool& p, |
211 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
212 | | apr_xml_elem* appenderElement, |
213 | | apr_xml_doc* doc, |
214 | | AppenderMap& appenders) |
215 | 0 | { |
216 | |
|
217 | 0 | LogString className(subst(getAttribute(utf8Decoder, appenderElement, CLASS_ATTR))); |
218 | 0 | if (LogLog::isDebugEnabled()) |
219 | 0 | { |
220 | 0 | LogLog::debug(LOG4CXX_STR("Class name: [") + className + LOG4CXX_STR("]")); |
221 | 0 | } |
222 | |
|
223 | 0 | try |
224 | 0 | { |
225 | 0 | ObjectPtr instance = ObjectPtr(Loader::loadClass(className).newInstance()); |
226 | 0 | AppenderPtr appender = LOG4CXX_NS::cast<Appender>(instance); |
227 | 0 | PropertySetter propSetter(appender); |
228 | |
|
229 | 0 | appender->setName(subst(getAttribute(utf8Decoder, appenderElement, NAME_ATTR))); |
230 | |
|
231 | 0 | for (apr_xml_elem* currentElement = appenderElement->first_child; |
232 | 0 | currentElement; |
233 | 0 | currentElement = currentElement->next) |
234 | 0 | { |
235 | |
|
236 | 0 | std::string tagName(currentElement->name); |
237 | | |
238 | | // Parse appender parameters |
239 | 0 | if (tagName == PARAM_TAG) |
240 | 0 | { |
241 | 0 | setParameter(p, utf8Decoder, currentElement, propSetter); |
242 | 0 | } |
243 | | // Set appender layout |
244 | 0 | else if (tagName == LAYOUT_TAG) |
245 | 0 | { |
246 | 0 | appender->setLayout(parseLayout(p, utf8Decoder, currentElement)); |
247 | 0 | } |
248 | | // Add filters |
249 | 0 | else if (tagName == FILTER_TAG) |
250 | 0 | { |
251 | 0 | std::vector<LOG4CXX_NS::spi::FilterPtr> filters; |
252 | 0 | parseFilters(p, utf8Decoder, currentElement, filters); |
253 | |
|
254 | 0 | for (auto& item : filters) |
255 | 0 | { |
256 | 0 | appender->addFilter(item); |
257 | 0 | } |
258 | 0 | } |
259 | 0 | else if (tagName == ERROR_HANDLER_TAG) |
260 | 0 | { |
261 | 0 | parseErrorHandler(p, utf8Decoder, currentElement, appender, doc, appenders); |
262 | 0 | } |
263 | 0 | else if (tagName == ROLLING_POLICY_TAG) |
264 | 0 | { |
265 | 0 | RollingPolicyPtr rollPolicy(parseRollingPolicy(p, utf8Decoder, currentElement)); |
266 | 0 | RollingFileAppenderPtr rfa = LOG4CXX_NS::cast<RollingFileAppender>(appender); |
267 | |
|
268 | 0 | if (rfa != NULL) |
269 | 0 | { |
270 | 0 | rfa->setRollingPolicy(rollPolicy); |
271 | 0 | } |
272 | 0 | } |
273 | 0 | else if (tagName == TRIGGERING_POLICY_TAG) |
274 | 0 | { |
275 | 0 | ObjectPtr policy(parseTriggeringPolicy(p, utf8Decoder, currentElement)); |
276 | 0 | RollingFileAppenderPtr rfa = LOG4CXX_NS::cast<RollingFileAppender>(appender); |
277 | 0 | TriggeringPolicyPtr policyPtr = LOG4CXX_NS::cast<TriggeringPolicy>(policy); |
278 | |
|
279 | 0 | if (rfa != NULL) |
280 | 0 | { |
281 | 0 | rfa->setTriggeringPolicy(policyPtr); |
282 | 0 | } |
283 | 0 | else |
284 | 0 | { |
285 | 0 | auto smtpa = LOG4CXX_NS::cast<LOG4CXX_NS::net::SMTPAppender>(appender); |
286 | |
|
287 | 0 | if (smtpa != NULL) |
288 | 0 | { |
289 | 0 | auto evaluator = LOG4CXX_NS::cast<TriggeringEventEvaluator>(policy); |
290 | 0 | smtpa->setEvaluator(evaluator); |
291 | 0 | } |
292 | 0 | } |
293 | 0 | } |
294 | 0 | else if (tagName == APPENDER_REF_TAG) |
295 | 0 | { |
296 | 0 | LogString refName = subst(getAttribute(utf8Decoder, currentElement, REF_ATTR)); |
297 | |
|
298 | 0 | if (appender->instanceof(AppenderAttachable::getStaticClass())) |
299 | 0 | { |
300 | 0 | AppenderAttachablePtr aa = LOG4CXX_NS::cast<AppenderAttachable>(appender); |
301 | 0 | if (LogLog::isDebugEnabled()) |
302 | 0 | { |
303 | 0 | LogLog::debug(LOG4CXX_STR("Attaching appender named [") + |
304 | 0 | refName + LOG4CXX_STR("] to appender named [") + |
305 | 0 | appender->getName() + LOG4CXX_STR("].")); |
306 | 0 | } |
307 | 0 | aa->addAppender(findAppenderByReference(p, utf8Decoder, currentElement, doc, appenders)); |
308 | 0 | } |
309 | 0 | else |
310 | 0 | { |
311 | 0 | LogLog::error(LOG4CXX_STR("Requesting attachment of appender named [") + |
312 | 0 | refName + LOG4CXX_STR("] to appender named [") + appender->getName() + |
313 | 0 | LOG4CXX_STR("] which does not implement AppenderAttachable.")); |
314 | 0 | } |
315 | 0 | } |
316 | 0 | } |
317 | |
|
318 | 0 | propSetter.activate(p); |
319 | 0 | return appender; |
320 | 0 | } |
321 | | /* Yes, it's ugly. But all of these exceptions point to the same |
322 | | problem: we can't create an Appender */ |
323 | 0 | catch (Exception& oops) |
324 | 0 | { |
325 | 0 | LogLog::error(LOG4CXX_STR("Could not create an Appender. Reported error follows."), |
326 | 0 | oops); |
327 | 0 | return 0; |
328 | 0 | } |
329 | 0 | } |
330 | | |
331 | | /** |
332 | | Used internally to parse an {@link ErrorHandler} element. |
333 | | */ |
334 | | void DOMConfigurator::parseErrorHandler(Pool& p, |
335 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
336 | | apr_xml_elem* element, |
337 | | AppenderPtr& appender, |
338 | | apr_xml_doc* doc, |
339 | | AppenderMap& appenders) |
340 | 0 | { |
341 | |
|
342 | 0 | ErrorHandlerPtr eh; |
343 | 0 | std::shared_ptr<Object> obj = OptionConverter::instantiateByClassName( |
344 | 0 | subst(getAttribute(utf8Decoder, element, CLASS_ATTR)), |
345 | 0 | ErrorHandler::getStaticClass(), |
346 | 0 | 0); |
347 | 0 | eh = LOG4CXX_NS::cast<ErrorHandler>(obj); |
348 | |
|
349 | 0 | if (eh != 0) |
350 | 0 | { |
351 | 0 | eh->setAppender(appender); |
352 | |
|
353 | 0 | PropertySetter propSetter(eh); |
354 | |
|
355 | 0 | for (apr_xml_elem* currentElement = element->first_child; |
356 | 0 | currentElement; |
357 | 0 | currentElement = currentElement->next) |
358 | 0 | { |
359 | 0 | std::string tagName(currentElement->name); |
360 | |
|
361 | 0 | if (tagName == PARAM_TAG) |
362 | 0 | { |
363 | 0 | setParameter(p, utf8Decoder, currentElement, propSetter); |
364 | 0 | } |
365 | 0 | else if (tagName == APPENDER_REF_TAG) |
366 | 0 | { |
367 | 0 | eh->setBackupAppender(findAppenderByReference(p, utf8Decoder, currentElement, doc, appenders)); |
368 | 0 | } |
369 | 0 | else if (tagName == LOGGER_REF) |
370 | 0 | { |
371 | 0 | LogString loggerName(getAttribute(utf8Decoder, currentElement, REF_ATTR)); |
372 | 0 | LoggerPtr logger = m_priv->repository->getLogger(loggerName, m_priv->loggerFactory); |
373 | 0 | eh->setLogger(logger); |
374 | 0 | } |
375 | 0 | else if (tagName == ROOT_REF) |
376 | 0 | { |
377 | 0 | LoggerPtr root = m_priv->repository->getRootLogger(); |
378 | 0 | eh->setLogger(root); |
379 | 0 | } |
380 | 0 | } |
381 | |
|
382 | 0 | propSetter.activate(p); |
383 | 0 | std::shared_ptr<AppenderSkeleton> appSkeleton = LOG4CXX_NS::cast<AppenderSkeleton>(appender); |
384 | |
|
385 | 0 | if (appSkeleton != 0) |
386 | 0 | { |
387 | 0 | appSkeleton->setErrorHandler(eh); |
388 | 0 | } |
389 | 0 | } |
390 | 0 | } |
391 | | |
392 | | /** |
393 | | Used internally to parse a filter element. |
394 | | */ |
395 | | void DOMConfigurator::parseFilters(Pool& p, |
396 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
397 | | apr_xml_elem* element, |
398 | | std::vector<LOG4CXX_NS::spi::FilterPtr>& filters) |
399 | 0 | { |
400 | 0 | LogString clazz = subst(getAttribute(utf8Decoder, element, CLASS_ATTR)); |
401 | 0 | FilterPtr filter; |
402 | 0 | std::shared_ptr<Object> obj = OptionConverter::instantiateByClassName(clazz, |
403 | 0 | Filter::getStaticClass(), 0); |
404 | 0 | filter = LOG4CXX_NS::cast<Filter>(obj); |
405 | |
|
406 | 0 | if (filter != 0) |
407 | 0 | { |
408 | 0 | PropertySetter propSetter(filter); |
409 | |
|
410 | 0 | for (apr_xml_elem* currentElement = element->first_child; |
411 | 0 | currentElement; |
412 | 0 | currentElement = currentElement->next) |
413 | 0 | { |
414 | 0 | std::string tagName(currentElement->name); |
415 | |
|
416 | 0 | if (tagName == PARAM_TAG) |
417 | 0 | { |
418 | 0 | setParameter(p, utf8Decoder, currentElement, propSetter); |
419 | 0 | } |
420 | 0 | } |
421 | |
|
422 | 0 | propSetter.activate(p); |
423 | 0 | filters.push_back(filter); |
424 | 0 | } |
425 | 0 | } |
426 | | |
427 | | /** |
428 | | Used internally to parse an category or logger element. |
429 | | */ |
430 | | void DOMConfigurator::parseLogger( |
431 | | LOG4CXX_NS::helpers::Pool& p, |
432 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
433 | | apr_xml_elem* loggerElement, |
434 | | apr_xml_doc* doc, |
435 | | AppenderMap& appenders) |
436 | 0 | { |
437 | | // Create a new Logger object from the <category> element. |
438 | 0 | LogString loggerName = subst(getAttribute(utf8Decoder, loggerElement, NAME_ATTR)); |
439 | |
|
440 | 0 | if (LogLog::isDebugEnabled()) |
441 | 0 | { |
442 | 0 | LogLog::debug(LOG4CXX_STR("Retreiving an instance of ") + loggerName); |
443 | 0 | } |
444 | 0 | LoggerPtr logger = m_priv->repository->getLogger(loggerName, m_priv->loggerFactory); |
445 | | |
446 | | // Setting up a logger needs to be an atomic operation, in order |
447 | | // to protect potential log operations while logger |
448 | | // configuration is in progress. |
449 | 0 | bool additivity = OptionConverter::toBoolean( |
450 | 0 | subst(getAttribute(utf8Decoder, loggerElement, ADDITIVITY_ATTR)), |
451 | 0 | true); |
452 | |
|
453 | 0 | if (LogLog::isDebugEnabled()) |
454 | 0 | { |
455 | 0 | LogLog::debug(LOG4CXX_STR("Setting [") + logger->getName() + LOG4CXX_STR("] additivity to [") + |
456 | 0 | (additivity ? LogString(LOG4CXX_STR("true")) : LogString(LOG4CXX_STR("false"))) + LOG4CXX_STR("].")); |
457 | 0 | } |
458 | 0 | logger->setAdditivity(additivity); |
459 | 0 | parseChildrenOfLoggerElement(p, utf8Decoder, loggerElement, logger, false, doc, appenders); |
460 | 0 | } |
461 | | |
462 | | /** |
463 | | Used internally to parse the logger factory element. |
464 | | */ |
465 | | void DOMConfigurator::parseLoggerFactory( |
466 | | LOG4CXX_NS::helpers::Pool& p, |
467 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
468 | | apr_xml_elem* factoryElement) |
469 | 0 | { |
470 | 0 | LogString className(subst(getAttribute(utf8Decoder, factoryElement, CLASS_ATTR))); |
471 | |
|
472 | 0 | if (className.empty()) |
473 | 0 | { |
474 | 0 | LogLog::error(LOG4CXX_STR("Logger Factory tag class attribute not found.")); |
475 | 0 | } |
476 | 0 | else |
477 | 0 | { |
478 | 0 | if (LogLog::isDebugEnabled()) |
479 | 0 | { |
480 | 0 | LogLog::debug(LOG4CXX_STR("Desired logger factory: [") + className + LOG4CXX_STR("]")); |
481 | 0 | } |
482 | 0 | std::shared_ptr<Object> obj = OptionConverter::instantiateByClassName( |
483 | 0 | className, |
484 | 0 | LoggerFactory::getStaticClass(), |
485 | 0 | 0); |
486 | 0 | m_priv->loggerFactory = LOG4CXX_NS::cast<LoggerFactory>(obj); |
487 | 0 | PropertySetter propSetter(m_priv->loggerFactory); |
488 | |
|
489 | 0 | for (apr_xml_elem* currentElement = factoryElement->first_child; |
490 | 0 | currentElement; |
491 | 0 | currentElement = currentElement->next) |
492 | 0 | { |
493 | 0 | std::string tagName(currentElement->name); |
494 | |
|
495 | 0 | if (tagName == PARAM_TAG) |
496 | 0 | { |
497 | 0 | setParameter(p, utf8Decoder, currentElement, propSetter); |
498 | 0 | } |
499 | 0 | } |
500 | 0 | } |
501 | 0 | } |
502 | | |
503 | | /** |
504 | | Used internally to parse the root logger element. |
505 | | */ |
506 | | void DOMConfigurator::parseRoot( |
507 | | LOG4CXX_NS::helpers::Pool& p, |
508 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
509 | | apr_xml_elem* rootElement, |
510 | | apr_xml_doc* doc, |
511 | | AppenderMap& appenders) |
512 | 0 | { |
513 | 0 | LoggerPtr root = m_priv->repository->getRootLogger(); |
514 | 0 | parseChildrenOfLoggerElement(p, utf8Decoder, rootElement, root, true, doc, appenders); |
515 | 0 | } |
516 | | |
517 | | /** |
518 | | Used internally to parse the children of a logger element. |
519 | | */ |
520 | | void DOMConfigurator::parseChildrenOfLoggerElement( |
521 | | LOG4CXX_NS::helpers::Pool& p, |
522 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
523 | | apr_xml_elem* loggerElement, LoggerPtr logger, bool isRoot, |
524 | | apr_xml_doc* doc, |
525 | | AppenderMap& appenders) |
526 | 0 | { |
527 | 0 | PropertySetter propSetter(logger); |
528 | 0 | std::vector<AppenderPtr> newappenders; |
529 | | |
530 | | // Remove all existing appenders from logger. They will be |
531 | | // reconstructed if need be. |
532 | 0 | logger->removeAllAppenders(); |
533 | |
|
534 | 0 | for (apr_xml_elem* currentElement = loggerElement->first_child; |
535 | 0 | currentElement; |
536 | 0 | currentElement = currentElement->next) |
537 | 0 | { |
538 | 0 | std::string tagName(currentElement->name); |
539 | |
|
540 | 0 | if (tagName == APPENDER_REF_TAG) |
541 | 0 | { |
542 | 0 | AppenderPtr appender = findAppenderByReference(p, utf8Decoder, currentElement, doc, appenders); |
543 | 0 | LogString refName = subst(getAttribute(utf8Decoder, currentElement, REF_ATTR)); |
544 | |
|
545 | 0 | if (!LogLog::isDebugEnabled()) |
546 | 0 | ; |
547 | 0 | else if (appender != 0) |
548 | 0 | { |
549 | 0 | LogLog::debug(LOG4CXX_STR("Adding appender named [") + refName + |
550 | 0 | LOG4CXX_STR("] to logger [") + logger->getName() + LOG4CXX_STR("].")); |
551 | 0 | } |
552 | 0 | else |
553 | 0 | { |
554 | 0 | LogLog::debug(LOG4CXX_STR("Appender named [") + refName + |
555 | 0 | LOG4CXX_STR("] not found.")); |
556 | 0 | } |
557 | |
|
558 | 0 | logger->addAppender(appender); |
559 | |
|
560 | 0 | } |
561 | 0 | else if (tagName == LEVEL_TAG) |
562 | 0 | { |
563 | 0 | parseLevel(p, utf8Decoder, currentElement, logger, isRoot); |
564 | 0 | } |
565 | 0 | else if (tagName == PRIORITY_TAG) |
566 | 0 | { |
567 | 0 | parseLevel(p, utf8Decoder, currentElement, logger, isRoot); |
568 | 0 | } |
569 | 0 | else if (tagName == PARAM_TAG) |
570 | 0 | { |
571 | 0 | setParameter(p, utf8Decoder, currentElement, propSetter); |
572 | 0 | } |
573 | 0 | } |
574 | |
|
575 | 0 | propSetter.activate(p); |
576 | 0 | } |
577 | | |
578 | | /** |
579 | | Used internally to parse a layout element. |
580 | | */ |
581 | | LayoutPtr DOMConfigurator::parseLayout ( |
582 | | LOG4CXX_NS::helpers::Pool& p, |
583 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
584 | | apr_xml_elem* layout_element) |
585 | 0 | { |
586 | 0 | LogString className(subst(getAttribute(utf8Decoder, layout_element, CLASS_ATTR))); |
587 | 0 | if (LogLog::isDebugEnabled()) |
588 | 0 | { |
589 | 0 | LogLog::debug(LOG4CXX_STR("Parsing layout of class: \"") + className + LOG4CXX_STR("\"")); |
590 | 0 | } |
591 | |
|
592 | 0 | try |
593 | 0 | { |
594 | 0 | ObjectPtr instance = ObjectPtr(Loader::loadClass(className).newInstance()); |
595 | 0 | LayoutPtr layout = LOG4CXX_NS::cast<Layout>(instance); |
596 | 0 | PropertySetter propSetter(layout); |
597 | |
|
598 | 0 | for (apr_xml_elem* currentElement = layout_element->first_child; |
599 | 0 | currentElement; |
600 | 0 | currentElement = currentElement->next) |
601 | 0 | { |
602 | 0 | std::string tagName(currentElement->name); |
603 | |
|
604 | 0 | if (tagName == PARAM_TAG) |
605 | 0 | { |
606 | 0 | setParameter(p, utf8Decoder, currentElement, propSetter); |
607 | 0 | } |
608 | 0 | } |
609 | |
|
610 | 0 | propSetter.activate(p); |
611 | 0 | return layout; |
612 | 0 | } |
613 | 0 | catch (Exception& oops) |
614 | 0 | { |
615 | 0 | LogLog::error(LOG4CXX_STR("Could not create the Layout. Reported error follows."), |
616 | 0 | oops); |
617 | 0 | return 0; |
618 | 0 | } |
619 | 0 | } |
620 | | |
621 | | /** |
622 | | Used internally to parse a triggering policy |
623 | | */ |
624 | | ObjectPtr DOMConfigurator::parseTriggeringPolicy ( |
625 | | LOG4CXX_NS::helpers::Pool& p, |
626 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
627 | | apr_xml_elem* layout_element) |
628 | 0 | { |
629 | 0 | LogString className = subst(getAttribute(utf8Decoder, layout_element, CLASS_ATTR)); |
630 | 0 | if (LogLog::isDebugEnabled()) |
631 | 0 | { |
632 | 0 | LogLog::debug(LOG4CXX_STR("Parsing triggering policy of class: \"") + className + LOG4CXX_STR("\"")); |
633 | 0 | } |
634 | |
|
635 | 0 | try |
636 | 0 | { |
637 | 0 | ObjectPtr instance = ObjectPtr(Loader::loadClass(className).newInstance()); |
638 | 0 | PropertySetter propSetter(instance); |
639 | |
|
640 | 0 | for (apr_xml_elem* currentElement = layout_element->first_child; |
641 | 0 | currentElement; |
642 | 0 | currentElement = currentElement->next) |
643 | 0 | { |
644 | 0 | std::string tagName(currentElement->name); |
645 | |
|
646 | 0 | if (tagName == PARAM_TAG) |
647 | 0 | { |
648 | 0 | setParameter(p, utf8Decoder, currentElement, propSetter); |
649 | 0 | } |
650 | 0 | else if (tagName == FILTER_TAG) |
651 | 0 | { |
652 | 0 | std::vector<LOG4CXX_NS::spi::FilterPtr> filters; |
653 | 0 | parseFilters(p, utf8Decoder, currentElement, filters); |
654 | 0 | FilterBasedTriggeringPolicyPtr fbtp = LOG4CXX_NS::cast<FilterBasedTriggeringPolicy>(instance); |
655 | |
|
656 | 0 | if (fbtp != NULL) |
657 | 0 | { |
658 | 0 | for (auto& item : filters) |
659 | 0 | { |
660 | 0 | fbtp->addFilter(item); |
661 | 0 | } |
662 | 0 | } |
663 | 0 | } |
664 | 0 | } |
665 | |
|
666 | 0 | propSetter.activate(p); |
667 | 0 | return instance; |
668 | 0 | } |
669 | 0 | catch (Exception& oops) |
670 | 0 | { |
671 | 0 | LogLog::error(LOG4CXX_STR("Could not create the TriggeringPolicy. Reported error follows."), |
672 | 0 | oops); |
673 | 0 | return 0; |
674 | 0 | } |
675 | 0 | } |
676 | | |
677 | | /** |
678 | | Used internally to parse a triggering policy |
679 | | */ |
680 | | RollingPolicyPtr DOMConfigurator::parseRollingPolicy ( |
681 | | LOG4CXX_NS::helpers::Pool& p, |
682 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
683 | | apr_xml_elem* layout_element) |
684 | 0 | { |
685 | 0 | LogString className = subst(getAttribute(utf8Decoder, layout_element, CLASS_ATTR)); |
686 | 0 | if (LogLog::isDebugEnabled()) |
687 | 0 | { |
688 | 0 | LogLog::debug(LOG4CXX_STR("Parsing rolling policy of class: \"") + className + LOG4CXX_STR("\"")); |
689 | 0 | } |
690 | |
|
691 | 0 | try |
692 | 0 | { |
693 | 0 | ObjectPtr instance = ObjectPtr(Loader::loadClass(className).newInstance()); |
694 | 0 | RollingPolicyPtr layout = LOG4CXX_NS::cast<RollingPolicy>(instance); |
695 | 0 | PropertySetter propSetter(layout); |
696 | |
|
697 | 0 | for (apr_xml_elem* currentElement = layout_element->first_child; |
698 | 0 | currentElement; |
699 | 0 | currentElement = currentElement->next) |
700 | 0 | { |
701 | 0 | std::string tagName(currentElement->name); |
702 | |
|
703 | 0 | if (tagName == PARAM_TAG) |
704 | 0 | { |
705 | 0 | setParameter(p, utf8Decoder, currentElement, propSetter); |
706 | 0 | } |
707 | 0 | } |
708 | |
|
709 | 0 | propSetter.activate(p); |
710 | 0 | return layout; |
711 | 0 | } |
712 | 0 | catch (Exception& oops) |
713 | 0 | { |
714 | 0 | LogLog::error(LOG4CXX_STR("Could not create the RollingPolicy. Reported error follows."), |
715 | 0 | oops); |
716 | 0 | return 0; |
717 | 0 | } |
718 | 0 | } |
719 | | |
720 | | |
721 | | |
722 | | /** |
723 | | Used internally to parse a level element. |
724 | | */ |
725 | | void DOMConfigurator::parseLevel( |
726 | | LOG4CXX_NS::helpers::Pool& p, |
727 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
728 | | apr_xml_elem* element, LoggerPtr logger, bool isRoot) |
729 | 0 | { |
730 | 0 | LogString loggerName = logger->getName(); |
731 | |
|
732 | 0 | if (isRoot) |
733 | 0 | { |
734 | 0 | loggerName = LOG4CXX_STR("root"); |
735 | 0 | } |
736 | |
|
737 | 0 | LogString levelStr(subst(getAttribute(utf8Decoder, element, VALUE_ATTR))); |
738 | 0 | if (LogLog::isDebugEnabled()) |
739 | 0 | { |
740 | 0 | LogLog::debug(LOG4CXX_STR("Level value for ") + loggerName + LOG4CXX_STR(" is [") + levelStr + LOG4CXX_STR("].")); |
741 | 0 | } |
742 | |
|
743 | 0 | if (StringHelper::equalsIgnoreCase(levelStr, LOG4CXX_STR("INHERITED"), LOG4CXX_STR("inherited")) |
744 | 0 | || StringHelper::equalsIgnoreCase(levelStr, LOG4CXX_STR("NULL"), LOG4CXX_STR("null"))) |
745 | 0 | { |
746 | 0 | if (isRoot) |
747 | 0 | { |
748 | 0 | LogLog::error(LOG4CXX_STR("Root level cannot be inherited. Ignoring directive.")); |
749 | 0 | } |
750 | 0 | else |
751 | 0 | { |
752 | 0 | logger->setLevel(0); |
753 | 0 | } |
754 | 0 | } |
755 | 0 | else |
756 | 0 | { |
757 | 0 | LogString className(subst(getAttribute(utf8Decoder, element, CLASS_ATTR))); |
758 | |
|
759 | 0 | if (className.empty()) |
760 | 0 | { |
761 | 0 | logger->setLevel(OptionConverter::toLevel(levelStr, Level::getDebug())); |
762 | 0 | } |
763 | 0 | else |
764 | 0 | { |
765 | 0 | if (LogLog::isDebugEnabled()) |
766 | 0 | { |
767 | 0 | LogLog::debug(LOG4CXX_STR("Desired Level sub-class: [") + className + LOG4CXX_STR("]")); |
768 | 0 | } |
769 | |
|
770 | 0 | try |
771 | 0 | { |
772 | 0 | Level::LevelClass& levelClass = |
773 | 0 | (Level::LevelClass&)Loader::loadClass(className); |
774 | 0 | LevelPtr level = levelClass.toLevel(levelStr); |
775 | 0 | logger->setLevel(level); |
776 | 0 | } |
777 | 0 | catch (Exception& oops) |
778 | 0 | { |
779 | 0 | LogLog::error( |
780 | 0 | LOG4CXX_STR("Could not create level [") + levelStr + |
781 | 0 | LOG4CXX_STR("]. Reported error follows."), |
782 | 0 | oops); |
783 | |
|
784 | 0 | return; |
785 | 0 | } |
786 | 0 | catch (...) |
787 | 0 | { |
788 | 0 | LogLog::error( |
789 | 0 | LOG4CXX_STR("Could not create level [") + levelStr); |
790 | |
|
791 | 0 | return; |
792 | 0 | } |
793 | 0 | } |
794 | 0 | } |
795 | | |
796 | 0 | if (LogLog::isDebugEnabled()) |
797 | 0 | { |
798 | 0 | LogLog::debug(loggerName + LOG4CXX_STR(" level set to ") + |
799 | 0 | logger->getEffectiveLevel()->toString()); |
800 | 0 | } |
801 | 0 | } |
802 | | |
803 | | void DOMConfigurator::setParameter(LOG4CXX_NS::helpers::Pool& p, |
804 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
805 | | apr_xml_elem* elem, |
806 | | PropertySetter& propSetter) |
807 | 0 | { |
808 | 0 | LogString name(subst(getAttribute(utf8Decoder, elem, NAME_ATTR))); |
809 | 0 | LogString value(subst(getAttribute(utf8Decoder, elem, VALUE_ATTR))); |
810 | 0 | value = subst(value); |
811 | 0 | propSetter.setProperty(name, value, p); |
812 | 0 | } |
813 | | |
814 | | spi::ConfigurationStatus DOMConfigurator::doConfigure |
815 | | ( const File& filename |
816 | | #if LOG4CXX_ABI_VERSION <= 15 |
817 | | , spi::LoggerRepositoryPtr repository |
818 | | #else |
819 | | , const spi::LoggerRepositoryPtr& repository |
820 | | #endif |
821 | | ) |
822 | 0 | { |
823 | 0 | m_priv->repository = repository ? repository : LogManager::getLoggerRepository(); |
824 | 0 | m_priv->repository->setConfigured(true); |
825 | 0 | if (LogLog::isDebugEnabled()) |
826 | 0 | { |
827 | 0 | LogString msg(LOG4CXX_STR("DOMConfigurator configuring file ")); |
828 | 0 | msg.append(filename.getPath()); |
829 | 0 | msg.append(LOG4CXX_STR("...")); |
830 | 0 | LogLog::debug(msg); |
831 | 0 | } |
832 | |
|
833 | 0 | #if LOG4CXX_ABI_VERSION <= 15 |
834 | 0 | m_priv->loggerFactory = std::make_shared<DefaultLoggerFactory>(); |
835 | | #else |
836 | | m_priv->loggerFactory = std::make_shared<LoggerFactory>(); |
837 | | #endif |
838 | |
|
839 | 0 | Pool p; |
840 | 0 | apr_file_t* fd; |
841 | |
|
842 | 0 | log4cxx_status_t rv = filename.open(&fd, APR_READ, APR_OS_DEFAULT, p); |
843 | |
|
844 | 0 | if (rv != APR_SUCCESS) |
845 | 0 | { |
846 | | // There is not technically an exception thrown here, but this behavior matches |
847 | | // what the PropertyConfigurator does |
848 | 0 | IOException io(rv); |
849 | 0 | LogString msg2(LOG4CXX_STR("Could not read configuration file [")); |
850 | 0 | msg2.append(filename.getPath()); |
851 | 0 | msg2.append(LOG4CXX_STR("]. ")); |
852 | 0 | LOG4CXX_DECODE_CHAR(msg, io.what()); |
853 | 0 | msg2.append(msg); |
854 | 0 | LogLog::error(msg2); |
855 | 0 | return spi::ConfigurationStatus::NotConfigured; |
856 | 0 | } |
857 | 0 | else |
858 | 0 | { |
859 | 0 | apr_xml_parser* parser = NULL; |
860 | 0 | apr_xml_doc* doc = NULL; |
861 | |
|
862 | 0 | if (LogLog::isDebugEnabled()) |
863 | 0 | { |
864 | 0 | LogString debugMsg = LOG4CXX_STR("Loading configuration file [") |
865 | 0 | + filename.getPath() + LOG4CXX_STR("]."); |
866 | 0 | LogLog::debug(debugMsg); |
867 | 0 | } |
868 | |
|
869 | 0 | rv = apr_xml_parse_file(p.getAPRPool(), &parser, &doc, fd, 2000); |
870 | |
|
871 | 0 | if (rv != APR_SUCCESS) |
872 | 0 | { |
873 | 0 | char errbuf[2000]; |
874 | 0 | char errbufXML[2000]; |
875 | 0 | LogString msg2(LOG4CXX_STR("Error parsing file [")); |
876 | 0 | msg2.append(filename.getPath()); |
877 | 0 | msg2.append(LOG4CXX_STR("], ")); |
878 | 0 | apr_strerror(rv, errbuf, sizeof(errbuf)); |
879 | 0 | LOG4CXX_DECODE_CHAR(lerrbuf, std::string(errbuf)); |
880 | 0 | msg2.append(lerrbuf); |
881 | |
|
882 | 0 | if (parser) |
883 | 0 | { |
884 | 0 | apr_xml_parser_geterror(parser, errbufXML, sizeof(errbufXML)); |
885 | 0 | LOG4CXX_DECODE_CHAR(lerrbufXML, std::string(errbufXML)); |
886 | 0 | msg2.append(lerrbufXML); |
887 | 0 | } |
888 | |
|
889 | 0 | LogLog::error(msg2); |
890 | 0 | return spi::ConfigurationStatus::NotConfigured; |
891 | 0 | } |
892 | 0 | else |
893 | 0 | { |
894 | 0 | AppenderMap appenders; |
895 | 0 | CharsetDecoderPtr utf8Decoder(CharsetDecoder::getUTF8Decoder()); |
896 | 0 | parse(p, utf8Decoder, doc->root, doc, appenders); |
897 | 0 | } |
898 | 0 | } |
899 | | |
900 | 0 | return spi::ConfigurationStatus::Configured; |
901 | 0 | } |
902 | | |
903 | | // Read configuration options from \c filename. |
904 | | spi::ConfigurationStatus DOMConfigurator::configure(const File& filename) |
905 | 0 | { |
906 | 0 | return DOMConfigurator().doConfigure(filename, LogManager::getLoggerRepository()); |
907 | 0 | } |
908 | | |
909 | | #if LOG4CXX_ABI_VERSION <= 15 |
910 | | spi::ConfigurationStatus DOMConfigurator::configure(const std::string& filename) |
911 | 0 | { |
912 | 0 | File file(filename); |
913 | 0 | return DOMConfigurator().doConfigure(file, LogManager::getLoggerRepository()); |
914 | 0 | } |
915 | | |
916 | | #if LOG4CXX_WCHAR_T_API |
917 | | spi::ConfigurationStatus DOMConfigurator::configure(const std::wstring& filename) |
918 | 0 | { |
919 | 0 | File file(filename); |
920 | 0 | return DOMConfigurator().doConfigure(file, LogManager::getLoggerRepository()); |
921 | 0 | } |
922 | | #endif |
923 | | |
924 | | #if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR |
925 | | spi::ConfigurationStatus DOMConfigurator::configure(const std::basic_string<UniChar>& filename) |
926 | | { |
927 | | File file(filename); |
928 | | return DOMConfigurator().doConfigure(file, LogManager::getLoggerRepository()); |
929 | | } |
930 | | #endif |
931 | | |
932 | | #if LOG4CXX_CFSTRING_API |
933 | | spi::ConfigurationStatus DOMConfigurator::configure(const CFStringRef& filename) |
934 | | { |
935 | | File file(filename); |
936 | | return DOMConfigurator().doConfigure(file, LogManager::getLoggerRepository()); |
937 | | } |
938 | | #endif |
939 | | |
940 | | |
941 | | spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const std::string& filename) |
942 | 0 | { |
943 | 0 | return configureAndWatch(filename, FileWatchdog::DEFAULT_DELAY); |
944 | 0 | } |
945 | | |
946 | | #if LOG4CXX_WCHAR_T_API |
947 | | spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const std::wstring& filename) |
948 | 0 | { |
949 | 0 | return configureAndWatch(filename, FileWatchdog::DEFAULT_DELAY); |
950 | 0 | } |
951 | | #endif |
952 | | |
953 | | #if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR |
954 | | spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const std::basic_string<UniChar>& filename) |
955 | | { |
956 | | return configureAndWatch(filename, FileWatchdog::DEFAULT_DELAY); |
957 | | } |
958 | | #endif |
959 | | |
960 | | #if LOG4CXX_CFSTRING_API |
961 | | spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const CFStringRef& filename) |
962 | | { |
963 | | return configureAndWatch(filename, FileWatchdog::DEFAULT_DELAY); |
964 | | } |
965 | | #endif |
966 | | |
967 | | spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const std::string& filename, long delay) |
968 | 0 | { |
969 | 0 | return configureAndWatch(File(filename), delay); |
970 | 0 | } |
971 | | #endif // LOG4CXX_ABI_VERSION <= 15 |
972 | | |
973 | | spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const File& file, long delay) |
974 | 0 | { |
975 | 0 | if ( xdog ) |
976 | 0 | { |
977 | 0 | APRInitializer::unregisterCleanup(xdog); |
978 | 0 | delete xdog; |
979 | 0 | } |
980 | |
|
981 | 0 | spi::ConfigurationStatus status = DOMConfigurator().doConfigure(file, LogManager::getLoggerRepository()); |
982 | |
|
983 | 0 | xdog = new XMLWatchdog(file); |
984 | 0 | APRInitializer::registerCleanup(xdog); |
985 | 0 | xdog->setDelay(0 < delay ? delay : FileWatchdog::DEFAULT_DELAY); |
986 | 0 | xdog->start(); |
987 | |
|
988 | 0 | return status; |
989 | 0 | } |
990 | | |
991 | | #if LOG4CXX_ABI_VERSION <= 15 |
992 | | #if LOG4CXX_WCHAR_T_API |
993 | | spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const std::wstring& filename, long delay) |
994 | 0 | { |
995 | 0 | File file(filename); |
996 | 0 | if ( xdog ) |
997 | 0 | { |
998 | 0 | APRInitializer::unregisterCleanup(xdog); |
999 | 0 | delete xdog; |
1000 | 0 | } |
1001 | |
|
1002 | 0 | spi::ConfigurationStatus status = DOMConfigurator().doConfigure(file, LogManager::getLoggerRepository()); |
1003 | |
|
1004 | 0 | xdog = new XMLWatchdog(file); |
1005 | 0 | APRInitializer::registerCleanup(xdog); |
1006 | 0 | xdog->setDelay(delay); |
1007 | 0 | xdog->start(); |
1008 | |
|
1009 | 0 | return status; |
1010 | 0 | } |
1011 | | #endif |
1012 | | |
1013 | | #if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR |
1014 | | spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const std::basic_string<UniChar>& filename, long delay) |
1015 | | { |
1016 | | File file(filename); |
1017 | | if ( xdog ) |
1018 | | { |
1019 | | APRInitializer::unregisterCleanup(xdog); |
1020 | | delete xdog; |
1021 | | } |
1022 | | |
1023 | | spi::ConfigurationStatus status = DOMConfigurator().doConfigure(file, LogManager::getLoggerRepository()); |
1024 | | |
1025 | | xdog = new XMLWatchdog(file); |
1026 | | APRInitializer::registerCleanup(xdog); |
1027 | | xdog->setDelay(delay); |
1028 | | xdog->start(); |
1029 | | |
1030 | | return status; |
1031 | | } |
1032 | | #endif |
1033 | | |
1034 | | #if LOG4CXX_CFSTRING_API |
1035 | | spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const CFStringRef& filename, long delay) |
1036 | | { |
1037 | | File file(filename); |
1038 | | if ( xdog ) |
1039 | | { |
1040 | | APRInitializer::unregisterCleanup(xdog); |
1041 | | delete xdog; |
1042 | | } |
1043 | | |
1044 | | spi::ConfigurationStatus status = DOMConfigurator().doConfigure(file, LogManager::getLoggerRepository()); |
1045 | | |
1046 | | xdog = new XMLWatchdog(file); |
1047 | | APRInitializer::registerCleanup(xdog); |
1048 | | xdog->setDelay(delay); |
1049 | | xdog->start(); |
1050 | | |
1051 | | return status; |
1052 | | } |
1053 | | #endif |
1054 | | #endif // LOG4CXX_ABI_VERSION <= 15 |
1055 | | |
1056 | | void DOMConfigurator::parse( |
1057 | | Pool& p, |
1058 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
1059 | | apr_xml_elem* element, |
1060 | | apr_xml_doc* doc, |
1061 | | AppenderMap& appenders) |
1062 | 0 | { |
1063 | 0 | std::string rootElementName(element->name); |
1064 | |
|
1065 | 0 | if (rootElementName != CONFIGURATION_TAG) |
1066 | 0 | { |
1067 | 0 | if (rootElementName == OLD_CONFIGURATION_TAG) |
1068 | 0 | { |
1069 | | //LogLog::warn(LOG4CXX_STR("The <")+String(OLD_CONFIGURATION_TAG)+ |
1070 | | // LOG4CXX_STR("> element has been deprecated.")); |
1071 | | //LogLog::warn(LOG4CXX_STR("Use the <")+String(CONFIGURATION_TAG)+ |
1072 | | // LOG4CXX_STR("> element instead.")); |
1073 | 0 | } |
1074 | 0 | else |
1075 | 0 | { |
1076 | 0 | LogLog::error(LOG4CXX_STR("DOM element is - not a <configuration> element.")); |
1077 | 0 | return; |
1078 | 0 | } |
1079 | 0 | } |
1080 | | |
1081 | 0 | LogString debugAttrib = subst(getAttribute(utf8Decoder, element, INTERNAL_DEBUG_ATTR)); |
1082 | |
|
1083 | 0 | static const WideLife<LogString> NULL_STRING(LOG4CXX_STR("NULL")); |
1084 | 0 | if (LogLog::isDebugEnabled()) |
1085 | 0 | { |
1086 | 0 | LogLog::debug(LOG4CXX_STR("debug attribute= \"") + debugAttrib + LOG4CXX_STR("\".")); |
1087 | 0 | } |
1088 | | |
1089 | | // if the log4j.dtd is not specified in the XML file, then the |
1090 | | // "debug" attribute is returned as the empty string. |
1091 | 0 | if (!debugAttrib.empty() && debugAttrib != NULL_STRING.value()) |
1092 | 0 | { |
1093 | 0 | LogLog::setInternalDebugging(OptionConverter::toBoolean(debugAttrib, true)); |
1094 | 0 | } |
1095 | 0 | else if (LogLog::isDebugEnabled()) |
1096 | 0 | { |
1097 | 0 | LogLog::debug(LOG4CXX_STR("Ignoring internalDebug attribute.")); |
1098 | 0 | } |
1099 | | |
1100 | |
|
1101 | 0 | LogString confDebug = subst(getAttribute(utf8Decoder, element, CONFIG_DEBUG_ATTR)); |
1102 | |
|
1103 | 0 | if (!confDebug.empty() && confDebug != NULL_STRING.value()) |
1104 | 0 | { |
1105 | 0 | LogLog::warn(LOG4CXX_STR("The \"configDebug\" attribute is deprecated.")); |
1106 | 0 | LogLog::warn(LOG4CXX_STR("Use the \"internalDebug\" attribute instead.")); |
1107 | 0 | LogLog::setInternalDebugging(OptionConverter::toBoolean(confDebug, true)); |
1108 | 0 | } |
1109 | |
|
1110 | 0 | LogString thresholdStr = subst(getAttribute(utf8Decoder, element, THRESHOLD_ATTR)); |
1111 | 0 | if (LogLog::isDebugEnabled()) |
1112 | 0 | { |
1113 | 0 | LogLog::debug(LOG4CXX_STR("Threshold =\"") + thresholdStr + LOG4CXX_STR("\".")); |
1114 | 0 | } |
1115 | |
|
1116 | 0 | if (!thresholdStr.empty() && thresholdStr != NULL_STRING.value()) |
1117 | 0 | { |
1118 | 0 | m_priv->repository->setThreshold(thresholdStr); |
1119 | 0 | } |
1120 | |
|
1121 | 0 | LogString threadSignalValue = subst(getAttribute(utf8Decoder, element, THREAD_CONFIG_ATTR)); |
1122 | |
|
1123 | 0 | if ( !threadSignalValue.empty() && threadSignalValue != NULL_STRING.value() ) |
1124 | 0 | { |
1125 | 0 | if ( threadSignalValue == LOG4CXX_STR("NoConfiguration") ) |
1126 | 0 | { |
1127 | 0 | helpers::ThreadUtility::configure( ThreadConfigurationType::NoConfiguration ); |
1128 | 0 | } |
1129 | 0 | else if ( threadSignalValue == LOG4CXX_STR("BlockSignalsOnly") ) |
1130 | 0 | { |
1131 | 0 | helpers::ThreadUtility::configure( ThreadConfigurationType::BlockSignalsOnly ); |
1132 | 0 | } |
1133 | 0 | else if ( threadSignalValue == LOG4CXX_STR("NameThreadOnly") ) |
1134 | 0 | { |
1135 | 0 | helpers::ThreadUtility::configure( ThreadConfigurationType::NameThreadOnly ); |
1136 | 0 | } |
1137 | 0 | else if ( threadSignalValue == LOG4CXX_STR("BlockSignalsAndNameThread") ) |
1138 | 0 | { |
1139 | 0 | helpers::ThreadUtility::configure( ThreadConfigurationType::BlockSignalsAndNameThread ); |
1140 | 0 | } |
1141 | 0 | } |
1142 | |
|
1143 | 0 | apr_xml_elem* currentElement; |
1144 | |
|
1145 | 0 | for (currentElement = element->first_child; |
1146 | 0 | currentElement; |
1147 | 0 | currentElement = currentElement->next) |
1148 | 0 | { |
1149 | 0 | std::string tagName(currentElement->name); |
1150 | |
|
1151 | 0 | if (tagName == CATEGORY_FACTORY_TAG) |
1152 | 0 | { |
1153 | 0 | parseLoggerFactory(p, utf8Decoder, currentElement); |
1154 | 0 | } |
1155 | 0 | } |
1156 | |
|
1157 | 0 | for (currentElement = element->first_child; |
1158 | 0 | currentElement; |
1159 | 0 | currentElement = currentElement->next) |
1160 | 0 | { |
1161 | 0 | std::string tagName(currentElement->name); |
1162 | |
|
1163 | 0 | if (tagName == CATEGORY || tagName == LOGGER) |
1164 | 0 | { |
1165 | 0 | parseLogger(p, utf8Decoder, currentElement, doc, appenders); |
1166 | 0 | } |
1167 | 0 | else if (tagName == ROOT_TAG) |
1168 | 0 | { |
1169 | 0 | parseRoot(p, utf8Decoder, currentElement, doc, appenders); |
1170 | 0 | } |
1171 | 0 | } |
1172 | 0 | } |
1173 | | |
1174 | | LogString DOMConfigurator::subst(const LogString& value) |
1175 | 0 | { |
1176 | 0 | try |
1177 | 0 | { |
1178 | 0 | return OptionConverter::substVars(value, m_priv->props); |
1179 | 0 | } |
1180 | 0 | catch (IllegalArgumentException& e) |
1181 | 0 | { |
1182 | 0 | LogLog::warn(LOG4CXX_STR("Could not perform variable substitution."), e); |
1183 | 0 | return value; |
1184 | 0 | } |
1185 | 0 | } |
1186 | | |
1187 | | |
1188 | | LogString DOMConfigurator::getAttribute( |
1189 | | LOG4CXX_NS::helpers::CharsetDecoderPtr& utf8Decoder, |
1190 | | apr_xml_elem* element, |
1191 | | const std::string& attrName) |
1192 | 0 | { |
1193 | 0 | LogString attrValue; |
1194 | |
|
1195 | 0 | for (apr_xml_attr* attr = element->attr; |
1196 | 0 | attr; |
1197 | 0 | attr = attr->next) |
1198 | 0 | { |
1199 | 0 | if (attrName == attr->name) |
1200 | 0 | { |
1201 | 0 | ByteBuffer buf((char*) attr->value, strnlen_s(attr->value, MAX_ATTRIBUTE_NAME_LEN)); |
1202 | 0 | utf8Decoder->decode(buf, attrValue); |
1203 | 0 | } |
1204 | 0 | } |
1205 | |
|
1206 | 0 | return attrValue; |
1207 | 0 | } |