// // LoggingConfigurator.cpp // // $Id: //poco/1.1.0/Util/src/LoggingConfigurator.cpp#2 $ // // Library: Util // Package: Configuration // Module: LoggingConfigurator // // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. // and Contributors. // // Permission is hereby granted, free of charge, to any person or organization // obtaining a copy of the software and accompanying documentation covered by // this license (the "Software") to use, reproduce, display, distribute, // execute, and transmit the Software, and to prepare derivative works of the // Software, and to permit third-parties to whom the Software is furnished to // do so, all subject to the following: // // The copyright notices in the Software and this entire statement, including // the above license grant, this restriction and the following disclaimer, // must be included in all copies of the Software, in whole or in part, and // all derivative works of the Software, unless such copies or derivative // works are solely in the form of machine-executable object code generated by // a source language processor. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // #include "Util/LoggingConfigurator.h" #include "Util/AbstractConfiguration.h" #include "Foundation/AutoPtr.h" #include "Foundation/Channel.h" #include "Foundation/FormattingChannel.h" #include "Foundation/Formatter.h" #include "Foundation/PatternFormatter.h" #include "Foundation/Logger.h" #include "Foundation/LoggingRegistry.h" #include "Foundation/LoggingFactory.h" using Foundation::AutoPtr; using Foundation::Formatter; using Foundation::PatternFormatter; using Foundation::Channel; using Foundation::FormattingChannel; using Foundation::Logger; using Foundation::LoggingRegistry; using Foundation::LoggingFactory; Util_BEGIN LoggingConfigurator::LoggingConfigurator() { } LoggingConfigurator::~LoggingConfigurator() { } void LoggingConfigurator::configure(AbstractConfiguration* pConfig) { poco_check_ptr (pConfig); AutoPtr pFormattersConfig = pConfig->createView("logging.formatters"); configureFormatters(pFormattersConfig); AutoPtr pChannelsConfig = pConfig->createView("logging.channels"); configureChannels(pChannelsConfig); AutoPtr pLoggersConfig = pConfig->createView("logging.loggers"); configureLoggers(pLoggersConfig); } void LoggingConfigurator::configureFormatters(AbstractConfiguration* pConfig) { AbstractConfiguration::Keys formatters; pConfig->keys(formatters); for (AbstractConfiguration::Keys::const_iterator it = formatters.begin(); it != formatters.end(); ++it) { AutoPtr pFormatterConfig = pConfig->createView(*it); AutoPtr pFormatter = createFormatter(pFormatterConfig); LoggingRegistry::defaultRegistry().registerFormatter(*it, pFormatter); } } void LoggingConfigurator::configureChannels(AbstractConfiguration* pConfig) { AbstractConfiguration::Keys channels; pConfig->keys(channels); for (AbstractConfiguration::Keys::const_iterator it = channels.begin(); it != channels.end(); ++it) { AutoPtr pChannelConfig = pConfig->createView(*it); AutoPtr pChannel = createChannel(pChannelConfig); LoggingRegistry::defaultRegistry().registerChannel(*it, pChannel); } } void LoggingConfigurator::configureLoggers(AbstractConfiguration* pConfig) { AbstractConfiguration::Keys loggers; pConfig->keys(loggers); for (AbstractConfiguration::Keys::const_iterator it = loggers.begin(); it != loggers.end(); ++it) { AutoPtr pLoggerConfig = pConfig->createView(*it); configureLogger(pLoggerConfig); } } Formatter* LoggingConfigurator::createFormatter(AbstractConfiguration* pConfig) { AutoPtr pFormatter = LoggingFactory::defaultFactory().createFormatter(pConfig->getString("class")); AbstractConfiguration::Keys props; pConfig->keys(props); for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it) { if (*it != "class") pFormatter->setProperty(*it, pConfig->getString(*it)); } return pFormatter.duplicate(); } Channel* LoggingConfigurator::createChannel(AbstractConfiguration* pConfig) { AutoPtr pChannel = LoggingFactory::defaultFactory().createChannel(pConfig->getString("class")); AutoPtr pWrapper = pChannel; AbstractConfiguration::Keys props; pConfig->keys(props); for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it) { if (*it == "pattern") { AutoPtr pPatternFormatter = new PatternFormatter(pConfig->getString(*it)); pWrapper = new FormattingChannel(pPatternFormatter, pChannel); } else if (*it == "formatter") { AutoPtr pFormattingChannel = new FormattingChannel(0, pChannel); if (pConfig->hasProperty("formatter.class")) { AutoPtr pFormatterConfig = pConfig->createView(*it); AutoPtr pFormatter = createFormatter(pFormatterConfig); pFormattingChannel->setFormatter(pFormatter); } else pFormattingChannel->setProperty(*it, pConfig->getString(*it)); #if defined(__GNUC__) && __GNUC__ < 3 pWrapper = pFormattingChannel.duplicate(); #else pWrapper = pFormattingChannel; #endif } else if (*it != "class") { pChannel->setProperty(*it, pConfig->getString(*it)); } } return pWrapper.duplicate(); } void LoggingConfigurator::configureLogger(AbstractConfiguration* pConfig) { Logger& logger = Logger::get(pConfig->getString("name", "")); AbstractConfiguration::Keys props; pConfig->keys(props); for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it) { if (*it == "channel" && pConfig->hasProperty("channel.class")) { AutoPtr pChannelConfig = pConfig->createView(*it); AutoPtr pChannel = createChannel(pChannelConfig); logger.setChannel(pChannel); } else if (*it != "name") { logger.setProperty(*it, pConfig->getString(*it)); } } } Util_END