hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Logger.cpp
Go to the documentation of this file.
1 #include "Logger.hpp"
2 
3 namespace HCE {
4  extern Poco::AutoPtr<Poco::Util::AbstractConfiguration> _config;
5 }
6 
8 
10 
11  HCE::Logger::initializeDefaultLogger();
12  HCE::Logger::checkDefaultLogger();
14 
15  Poco::AutoPtr<Poco::FormattingChannel> pFCFile;
16  Poco::Util::AbstractConfiguration::Keys channels;
17 
19  HCE::_config->keys(CHANNELS,channels);
20 
21  for (std::vector<std::string>::const_iterator key = channels.begin(); key != channels.end(); ++key) {
22  //if key exist use PatternFormatter.key, else use default format.
23  std::string patternForChannel = std::string(PATTERN_FORMATTER) + "." + *key;
24  if (HCE::_config->has(patternForChannel)) {
25  pFCFile = new Poco::FormattingChannel(Poco::AutoPtr<Poco::PatternFormatter>(new Poco::PatternFormatter(HCE::_config->getString(patternForChannel))));
26  }
27  else {
28  pFCFile = new Poco::FormattingChannel(Poco::AutoPtr<Poco::Formatter>(new Poco::PatternFormatter(DEFAULT_LOG_PATTERN_FORMATTER)));
29  }
30 
32  Poco::AutoPtr<Poco::FileChannel> fileChannel(new Poco::FileChannel(HCE::_config->getString(std::string(CHANNELS) + "."+*key)));
33 
34  std::string archive = HCE::_config->getString("LOG.archive",DEFAULT_ARCHIVE);
35  std::string purgeAge = HCE::_config->getString("LOG.purgeAge",DEFAULT_PURGE_AGE);
36  std::string rotation = HCE::_config->getString("LOG.rotation",DEFAULT_ROTATION);
37 
38  fileChannel->setProperty("archive",archive);
39  fileChannel->setProperty("purgeAge",purgeAge);
40  fileChannel->setProperty("rotation",rotation);
41 
42  pFCFile->setChannel(fileChannel);
43 
45  Poco::Logger* logger = Poco::Logger::has(*key);
46  if (logger != NULL) {
47  logger->setChannel(pFCFile);
48  logger->setLevel(HCE::_config->getString(LOG_LEVEL,DEFAULT_LOG_LEVEL));
49  }
50  else {
51  Poco::Logger& newLogger = Poco::Logger::create(*key,pFCFile);
52  newLogger.setLevel(HCE::_config->getString(LOG_LEVEL,DEFAULT_LOG_LEVEL));
53  }
54  }
55 }
56 
57 void HCE::Logger::initializeDefaultLogger() {
58  Poco::AutoPtr<Poco::FormattingChannel> defaultpFCFile =
59  new Poco::FormattingChannel(Poco::AutoPtr<Poco::PatternFormatter>(new Poco::PatternFormatter(DEFAULT_LOG_PATTERN_FORMATTER)));
60  Poco::AutoPtr<Poco::FileChannel> fileChannel(new Poco::FileChannel(getDefaultChannel()));
61 
65 
66  std::string archive = HCE::_config->getString("LOG.archive",DEFAULT_ARCHIVE);
67  std::string purgeAge = HCE::_config->getString("LOG.purgeAge",DEFAULT_PURGE_AGE);
68  std::string rotation = HCE::_config->getString("LOG.rotation",DEFAULT_ROTATION);
69 
70  fileChannel->setProperty("archive",archive);
71  fileChannel->setProperty("purgeAge",purgeAge);
72  fileChannel->setProperty("rotation",rotation);
73  defaultpFCFile->setChannel(fileChannel);
74 
75  //default log level - information
76  Poco::Logger* logger = NULL;
77  logger = Poco::Logger::has(DEFAULT_LOG_NAME);
78  if (logger == NULL) {
79  Poco::Logger& defaultLogger = Poco::Logger::create(DEFAULT_LOG_NAME,defaultpFCFile);
80  defaultLogger.setLevel(HCE::_config->getString(LOG_LEVEL,DEFAULT_LOG_LEVEL));
81  }
82  else {
83  //default logger already exist
84  }
85 }
86 
87 void HCE::Logger::checkDefaultLogger() {
88  //finding logger
89  Poco::Logger* checkLogger = Poco::Logger::has(DEFAULT_LOG_NAME);
90  //<! if default logger does not exist application can not works
91  poco_assert (checkLogger != NULL);
92 }
93 
94 std::string HCE::Logger::getDefaultChannel () {
95  poco_check_ptr(HCE::_config);
96  return HCE::_config->getString(BASE_NAME) + std::string(DEFAULT_LOG_SUFFIX);
97 }
98 
99 //<! channel - logger name
100 //<! msg - log message
101 void HCE::Logger::writeLog (const Poco::Message& msg,const std::string& channel) {
102  //<! if channel exist , write msg to channel
103  Poco::Logger* logger = NULL;
104  logger = Poco::Logger::has(channel);
105  if (logger != NULL) {
106  logger->log(msg);
107  }
108  else {
109  if (HCE::Logger::alwaysUseDefaultLog == true) {
110  logger = Poco::Logger::has(DEFAULT_LOG_NAME);
111  if (logger != NULL) {
112  logger->log(msg);
113  }
114  else {
115  HCE::Logger::writeUnregisterLog (msg,channel);
116  }
117  }
118  else {
119  HCE::Logger::writeUnregisterLog(msg,channel);
120  }
121  }
122 }
123 
124 void HCE::Logger::writeUnregisterLog(const Poco::Message& msg,const std::string& channel) {
125  std::cout << HCE::Logger::createMsgIfThatChannelNotExist(channel) << std::endl;
126  std::cout << MESSAGE_WRITTEN_INTO_UNREGISTER_LOG << " : " << msg.getText() << std::endl;
127 }
128 
129 void HCE::Logger::setLevelByChannelName(const std::string& channelName, const int& level) {
130  Poco::Logger::setLevel(channelName,level);
131 }
132 
133 void HCE::Logger::setLevelByChannelName (const std::string& channelName,const std::string& level) {
134  //this method throw InvalidArgumentException if level not valid.
135  int levelIntegerValue = Poco::Logger::parseLevel(level);
136  HCE::Logger::setLevelByChannelName(channelName,levelIntegerValue);
137 }
138 
139 void HCE::Logger::setLevelForAllChannel(const int& level) {
140  Poco::Logger::setLevel(Poco::Logger::ROOT,level);
141 }
142 
143 void HCE::Logger::setLevelForAllChannel(const std::string& level) {
144  //this method throw InvalidArgumentException if level not valid.
145  int levelIntegerValue = Poco::Logger::parseLevel(level);
146  HCE::Logger::setLevelForAllChannel(levelIntegerValue);
147 }
148 
150  Poco::Logger::setLevel(Poco::Logger::ROOT,LOGGER_OFF_LEVEL);
151 }
152 
153 void HCE::Logger::on(const std::string& level) {
155 }
156 
157 void HCE::Logger::on(const int& level) {
159 }
160 
161 std::string HCE::Logger::createMsgIfThatChannelNotExist (const std::string& channelName) {
162  std::stringstream ss;
163  ss << "Channel "; ss << channelName; ss << " not exists";
164  return ss.str();
165 }