RSC  0.12.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OptionBasedConfigurator.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSC project.
4  *
5  * Copyright (C) 2012 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
28 
29 #include <boost/algorithm/string.hpp>
30 
31 #include "Logger.h"
32 #include "LoggerFactory.h"
33 #include "../runtime/ContainerIO.h"
34 
35 using namespace std;
36 using namespace rsc::config;
37 
38 namespace rsc {
39 namespace logging {
40 
41 OptionBasedConfigurator::OptionBasedConfigurator(
42  const vector<string>& rootOption) :
43  rootOption(normalizeKey(rootOption)) {
44 }
45 
47 }
48 
50  vector<string> root;
51  root.push_back("rsc");
52  root.push_back("logging");
53  return root;
54 }
55 
57  return rootOption;
58 }
59 
61  const vector<string>& key) const {
62 
63  if (key.size() < rootOption.size()) {
64  return false;
65  }
66 
67  for (size_t i = 0; i < rootOption.size(); ++i) {
68  string keyPart = key[i];
69  boost::algorithm::to_lower(keyPart);
70  if (rootOption[i] != keyPart) {
71  return false;
72  }
73  }
74 
75  return true;
76 
77 }
78 
80  const vector<string>& key) const {
81 
82  vector<string> normalizedKey = normalizeKey(key);
83 
84  string name;
85  // the last fragment must be the setting, do not include in the name
86  for (vector<string>::const_iterator keyIt = key.begin() + rootOption.size();
87  keyIt != key.end() - 1; ++keyIt) {
88 
89  name += "." + *keyIt;
90 
91  }
92 
93  if (name.empty()) {
94  return name;
95  } else {
96  return name.substr(1, name.size() - 1);
97  }
98 
99 }
100 
101 void OptionBasedConfigurator::handleOption(const vector<string>& key,
102  const string& value) {
103 
104  vector<string> normalizedKey = normalizeKey(key);
105 
106  if (!keyStartWithRoot(normalizedKey)) {
107  return;
108  }
109 
110  // something can only be a valid key for the configuration if there is a
111  // chance that a special configuration key is at the end. Hence, sufficient
112  // length is required
113  if (normalizedKey.size() <= rootOption.size()) {
114  return;
115  }
116 
117  LoggerPtr logger = Logger::getLogger(loggerNameFromKey(normalizedKey));
118 
119  string setting = normalizedKey.back();
120  if (setting == "level") {
121 
122  if (value == "ALL") {
123  logger->setLevel(Logger::LEVEL_ALL);
124  } else if (value == "TRACE") {
125  logger->setLevel(Logger::LEVEL_TRACE);
126  } else if (value == "DEBUG") {
127  logger->setLevel(Logger::LEVEL_DEBUG);
128  } else if (value == "INFO") {
129  logger->setLevel(Logger::LEVEL_INFO);
130  } else if (value == "WARN") {
131  logger->setLevel(Logger::LEVEL_WARN);
132  } else if (value == "ERROR") {
133  logger->setLevel(Logger::LEVEL_ERROR);
134  } else if (value == "FATAL") {
135  logger->setLevel(Logger::LEVEL_FATAL);
136  } else if (value == "OFF") {
137  logger->setLevel(Logger::LEVEL_OFF);
138  }
139 
140  } else if (setting == "system" && logger->getName() == "") {
141  LoggerFactory::getInstance().reselectLoggingSystem(value);
142  }
143 
144 }
145 
147  const vector<string>& key) const {
148 
149  vector<string> normalizedKey;
150  for (vector<string>::const_iterator keyIt = key.begin(); keyIt != key.end(); ++keyIt) {
151  string part = *keyIt;
152  boost::algorithm::to_lower(part);
153  normalizedKey.push_back(part);
154  }
155 
156  return normalizedKey;
157 
158 }
159 
160 }
161 }
virtual void handleOption(const std::vector< std::string > &key, const std::string &value)
This method is called once for each individual option available from a given ConfigSource.
std::vector< std::string > getRootOption() const
Returns the option root used by this configurator.
std::vector< std::string > normalizeKey(const std::vector< std::string > &key) const
static LoggerPtr getLogger(const std::string &name)
Returns a logger for the given name.
Definition: Logger.cpp:39
bool keyStartWithRoot(const std::vector< std::string > &key) const
std::string loggerNameFromKey(const std::vector< std::string > &key) const
static LoggerFactory & getInstance()
Retrieve the singleton instance, creating it if necessary.
static std::vector< std::string > getDefaultRootOption()
Returns the default config entry assumed for the root logger.
boost::shared_ptr< Logger > LoggerPtr
Definition: Logger.h:41