hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
LoggerConfigLoader.cpp
Go to the documentation of this file.
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>
11 
12 #include "LoggerConfigLoader.hpp"
13 
14 namespace HCE
15 {
16 namespace handlers
17 {
18 //-----------------------------------------------------------------------------
19 // constants using for initialization Poco::Logger
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 = "-";
24 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
26 LoggerConfigLoader::LoggerConfigLoader(const std::string& nodeName_, const Poco::Util::IniFileConfiguration::Keys& loggers_, const std::string& configFileName)
27 : nodeName(nodeName_), errorMsg(""), _isError(false), loggers(loggers_)
28 {
29  if (!configFileName.empty())
30  loadConfig(configFileName);
31 }
32 //-----------------------------------------------------------------------------
33 LoggerConfigLoader::LoggerConfigLoader(const std::string& nodeName_, const std::string& configFileName)
34 : nodeName(nodeName_), errorMsg(""), _isError(false), loggers()
35 {
36  if (!configFileName.empty())
37  loadConfig(configFileName);
38 }
39 //-----------------------------------------------------------------------------
41 {
42 }
43 //-----------------------------------------------------------------------------
45 {
46  Poco::AutoPtr<Poco::PatternFormatter> pPatternFormatter(new Poco::PatternFormatter("%Y-%m-%d %H:%M:%S.%i %N[%P]:%s:%q:%t"));
47  if (pPatternFormatter)
48  {
49  pPatternFormatter->setProperty(Poco::PatternFormatter::PROP_TIMES, "local");
50  Poco::AutoPtr<Poco::FormattingChannel> pChannel = new Poco::FormattingChannel(pPatternFormatter);
51  if (pChannel)
52  {
53  Poco::AutoPtr<Poco::ConsoleChannel> pCons(new Poco::ConsoleChannel());
54  pChannel->setChannel(pCons);
55  pChannel->open();
56  Poco::Logger::root().setChannel(pChannel);
57  Poco::Logger::root().setLevel("information");
58  Poco::Logger::root().information("Initialization console logger");
59  }
60  }
61 }
62 //-----------------------------------------------------------------------------
63 bool LoggerConfigLoader::loadConfig(const std::string& configFileName)
64 {
65  _isError = true;
66  errorMsg.clear();
67  try
68  {
69  if (configFileName.empty())
71  else
72  {
73  Poco::AutoPtr<Poco::Util::IniFileConfiguration> pConf(new Poco::Util::IniFileConfiguration(configFileName));
74 
75  // getting names all loggers
76  Poco::Util::IniFileConfiguration::Keys loggerNames;
77  getLoggers(*pConf, loggerNames);
78 
79  checkExistSection(*pConf, loggerNames);
80 
81  // initialization all found loggers
82  for (size_t i=0;i<loggers.size();++i)
83  {
84  loggerInitialization(*pConf, loggers[i]);
85  }
86  }
87  _isError = false;
88  }
89  catch(Poco::SyntaxException& e)
90  {
91  errorMsg = e.displayText();
92  }
93  catch(Poco::Exception& e)
94  {
95  errorMsg = e.displayText();
96  }
97  return !_isError;
98 }
99 //-----------------------------------------------------------------------------
100 void LoggerConfigLoader::loadSection(Poco::Util::IniFileConfiguration& iniFileConfiguration,
101  const std::string& sectionName,
102  Poco::Configurable& configurable) throw (Poco::Exception)
103 {
104  Poco::Util::IniFileConfiguration::Keys keys;
105  iniFileConfiguration.keys(sectionName, keys);
106 
107  for (size_t i=0;i<keys.size();++i)
108  {
109  std::string parameterName = sectionName+"."+keys[i];
110  if (iniFileConfiguration.has(parameterName))
111  {
112  std::string parameterValue = iniFileConfiguration.getString(parameterName);
113  if (keys[i]=="path")
114  {
115  parameterValue = Poco::format(parameterValue, nodeName);
116  parameterValue = Poco::Path(parameterValue).absolute().toString();
117  }
118  configurable.setProperty(keys[i], parameterValue);
119  }
120  }
121 }
122 //-----------------------------------------------------------------------------
123 void LoggerConfigLoader::loggerInitialization(Poco::Util::IniFileConfiguration& iniFileConfiguration, const std::string& loggerName) throw (Poco::Exception)
124 {
125  Poco::AutoPtr<Poco::PatternFormatter> pPatternFormatter(new Poco::PatternFormatter());
126  if (pPatternFormatter)
127  {
128  loadSection(iniFileConfiguration, getLoggerSectionName(LoggerConfigLoader::sectionPatternFormatter, loggerName), *pPatternFormatter);
129 
130  Poco::AutoPtr<Poco::FormattingChannel> pChannel = new Poco::FormattingChannel(pPatternFormatter);
131  if (pChannel)
132  {
133  Poco::AutoPtr<Poco::FileChannel> pFileChannel = new Poco::FileChannel();
134  pChannel->setChannel(pFileChannel);
135 
136  loadSection(iniFileConfiguration, getLoggerSectionName(LoggerConfigLoader::sectionFileChannel, loggerName), *pFileChannel);
137 
138  Poco::Logger& logger = Poco::Logger::get(loggerName);
139  logger.setChannel(pChannel);
140  pChannel->open();
141 
142  loadSection(iniFileConfiguration, getLoggerSectionName(LoggerConfigLoader::sectionLogger, loggerName), logger);
143  }
144  }
145 }
146 //-----------------------------------------------------------------------------
147 std::string LoggerConfigLoader::getLoggerSectionName(const std::string& sectionName, const std::string& loggerName)
148 {
149  std::string loggerSectionName(sectionName);
150 
151  if (!loggerName.empty())
152  loggerSectionName.append(LoggerConfigLoader::delimiter).append(loggerName);
153 
154  return loggerSectionName;
155 }
156 //-----------------------------------------------------------------------------
157 void LoggerConfigLoader::getLoggers(Poco::Util::IniFileConfiguration& iniFileConfiguration, Poco::Util::IniFileConfiguration::Keys& loggerNames)
158 {
159  loggerNames.clear();
160  Poco::Util::IniFileConfiguration::Keys keys;
161  iniFileConfiguration.keys(keys);
162  for (size_t i=0;i<keys.size();++i)
163  {
164  size_t pos = keys[i].find(LoggerConfigLoader::sectionLogger);
165  if (pos!=std::string::npos)
166  {
167  std::string sectionName = keys[i].substr();
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());
172 
173  loggerNames.push_back(sectionName);
174  }
175  }
176 }
177 //-----------------------------------------------------------------------------
178 void LoggerConfigLoader::checkExistSection(Poco::Util::IniFileConfiguration& iniFileConfiguration,
179  const Poco::Util::IniFileConfiguration::Keys& loggerNames) throw (Poco::Exception)
180 {
181  for (const std::string logger : loggers)
182  {
183  bool found = false;
184  for (const std::string loggerName : loggerNames)
185  {
186  if (logger == loggerName)
187  {
188  found = true;
189  break;
190  }
191  }
192  if (!found)
193  throw Poco::Exception("Not found mandatory logger name: '"+logger+"'");
194  }
195 
196  Poco::Util::IniFileConfiguration::Keys sections;
197  iniFileConfiguration.keys(sections);
198 
199  for (const std::string loggerName : loggerNames)
200  {
201  checkExistSection(sections, LoggerConfigLoader::getLoggerSectionName(sectionPatternFormatter, loggerName));
202  checkExistSection(sections, LoggerConfigLoader::getLoggerSectionName(sectionFileChannel, loggerName));
203  checkExistSection(sections, LoggerConfigLoader::getLoggerSectionName(sectionLogger, loggerName));
204  }
205 }
206 //-----------------------------------------------------------------------------
207 void LoggerConfigLoader::checkExistSection(const Poco::Util::IniFileConfiguration::Keys& sections,
208  const std::string& sectionName) throw (Poco::Exception)
209 {
210  bool found = false;
211  for (const std::string section : sections)
212  {
213  if (section == sectionName)
214  {
215  found = true;
216  break;
217  }
218  }
219  if (!found)
220  throw Poco::Exception("Not found mandatory section name: '"+sectionName+"'");
221 }
222 //-----------------------------------------------------------------------------
224 {
225  return static_cast<unsigned int>(Poco::Logger::root().getLevel());
226 }
227 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
229 } // end namespace handlers
230 } // end namespace HCE