1 #include <Poco/Util/IniFileConfiguration.h>
2 #include <Poco/Util/AbstractConfiguration.h>
3 #include <Poco/AutoPtr.h>
4 #include <Poco/Logger.h>
5 #include <Poco/PatternFormatter.h>
6 #include <Poco/FormattingChannel.h>
7 #include <Poco/FileChannel.h>
8 #include <Poco/ConsoleChannel.h>
9 #include <Poco/Format.h>
10 #include <Poco/Path.h>
20 const std::string LoggerConfigLoader::sectionPatternFormatter =
"PatternFormatter";
21 const std::string LoggerConfigLoader::sectionFileChannel =
"FileChannel";
22 const std::string LoggerConfigLoader::sectionLogger =
"Logger";
23 const std::string LoggerConfigLoader::delimiter =
"-";
29 if (!configFileName.empty())
36 if (!configFileName.empty())
46 Poco::AutoPtr<Poco::PatternFormatter> pPatternFormatter(
new Poco::PatternFormatter(
"%Y-%m-%d %H:%M:%S.%i %N[%P]:%s:%q:%t"));
47 if (pPatternFormatter)
49 pPatternFormatter->setProperty(Poco::PatternFormatter::PROP_TIMES,
"local");
50 Poco::AutoPtr<Poco::FormattingChannel> pChannel =
new Poco::FormattingChannel(pPatternFormatter);
53 Poco::AutoPtr<Poco::ConsoleChannel> pCons(
new Poco::ConsoleChannel());
54 pChannel->setChannel(pCons);
56 Poco::Logger::root().setChannel(pChannel);
57 Poco::Logger::root().setLevel(
"information");
58 Poco::Logger::root().information(
"Initialization console logger");
69 if (configFileName.empty())
73 Poco::AutoPtr<Poco::Util::IniFileConfiguration> pConf(
new Poco::Util::IniFileConfiguration(configFileName));
76 Poco::Util::IniFileConfiguration::Keys loggerNames;
82 for (
size_t i=0;i<
loggers.size();++i)
89 catch(Poco::SyntaxException& e)
93 catch(Poco::Exception& e)
102 Poco::Configurable& configurable)
throw (Poco::Exception)
104 Poco::Util::IniFileConfiguration::Keys keys;
107 for (
size_t i=0;i<keys.size();++i)
110 if (iniFileConfiguration.has(parameterName))
112 std::string
parameterValue = iniFileConfiguration.getString(parameterName);
115 parameterValue = Poco::format(parameterValue,
nodeName);
116 parameterValue = Poco::Path(parameterValue).absolute().toString();
118 configurable.setProperty(keys[i], parameterValue);
125 Poco::AutoPtr<Poco::PatternFormatter> pPatternFormatter(
new Poco::PatternFormatter());
126 if (pPatternFormatter)
128 loadSection(iniFileConfiguration, getLoggerSectionName(LoggerConfigLoader::sectionPatternFormatter, loggerName), *pPatternFormatter);
130 Poco::AutoPtr<Poco::FormattingChannel> pChannel =
new Poco::FormattingChannel(pPatternFormatter);
133 Poco::AutoPtr<Poco::FileChannel> pFileChannel =
new Poco::FileChannel();
134 pChannel->setChannel(pFileChannel);
136 loadSection(iniFileConfiguration, getLoggerSectionName(LoggerConfigLoader::sectionFileChannel, loggerName), *pFileChannel);
138 Poco::Logger& logger = Poco::Logger::get(loggerName);
139 logger.setChannel(pChannel);
142 loadSection(iniFileConfiguration, getLoggerSectionName(LoggerConfigLoader::sectionLogger, loggerName), logger);
149 std::string loggerSectionName(sectionName);
151 if (!loggerName.empty())
152 loggerSectionName.append(LoggerConfigLoader::delimiter).append(loggerName);
154 return loggerSectionName;
160 Poco::Util::IniFileConfiguration::Keys keys;
161 iniFileConfiguration.keys(keys);
162 for (
size_t i=0;i<keys.size();++i)
164 size_t pos = keys[i].find(LoggerConfigLoader::sectionLogger);
165 if (pos!=std::string::npos)
168 sectionName.erase(pos, LoggerConfigLoader::sectionLogger.length());
169 pos = sectionName.find(LoggerConfigLoader::delimiter);
170 if (pos!=std::string::npos)
171 sectionName.erase(pos, LoggerConfigLoader::delimiter.length());
173 loggerNames.push_back(sectionName);
179 const Poco::Util::IniFileConfiguration::Keys& loggerNames)
throw (Poco::Exception)
181 for (
const std::string logger : loggers)
184 for (
const std::string loggerName : loggerNames)
186 if (logger == loggerName)
193 throw Poco::Exception(
"Not found mandatory logger name: '"+logger+
"'");
196 Poco::Util::IniFileConfiguration::Keys sections;
197 iniFileConfiguration.keys(sections);
199 for (
const std::string loggerName : loggerNames)
208 const std::string&
sectionName)
throw (Poco::Exception)
211 for (
const std::string section : sections)
220 throw Poco::Exception(
"Not found mandatory section name: '"+
sectionName+
"'");
225 return static_cast<unsigned int>(Poco::Logger::root().getLevel());