RSC  0.17.1
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  string name;
82  // the last fragment must be the setting, do not include in the name
83  for (vector<string>::const_iterator keyIt = key.begin() + rootOption.size();
84  keyIt != key.end() - 1; ++keyIt) {
85  name += "." + *keyIt;
86  }
87 
88  if (name.empty()) {
89  return name;
90  } else {
91  return name.substr(1, name.size() - 1);
92  }
93 
94 }
95 
96 void OptionBasedConfigurator::handleOption(const vector<string>& key,
97  const string& value) {
98 
99  vector<string> normalizedKey = normalizeKey(key);
100 
101  if (!keyStartWithRoot(normalizedKey)) {
102  return;
103  }
104 
105  // something can only be a valid key for the configuration if there is a
106  // chance that a special configuration key is at the end. Hence, sufficient
107  // length is required
108  if (normalizedKey.size() <= rootOption.size()) {
109  return;
110  }
111 
112  LoggerPtr logger = Logger::getLogger(loggerNameFromKey(normalizedKey));
113 
114  string setting = normalizedKey.back();
115  if (setting == "level") {
116 
117  if (value == "ALL") {
118  logger->setLevel(Logger::LEVEL_ALL);
119  } else if (value == "TRACE") {
120  logger->setLevel(Logger::LEVEL_TRACE);
121  } else if (value == "DEBUG") {
122  logger->setLevel(Logger::LEVEL_DEBUG);
123  } else if (value == "INFO") {
124  logger->setLevel(Logger::LEVEL_INFO);
125  } else if (value == "WARN") {
126  logger->setLevel(Logger::LEVEL_WARN);
127  } else if (value == "ERROR") {
128  logger->setLevel(Logger::LEVEL_ERROR);
129  } else if (value == "FATAL") {
130  logger->setLevel(Logger::LEVEL_FATAL);
131  } else if (value == "OFF") {
132  logger->setLevel(Logger::LEVEL_OFF);
133  }
134 
135  } else if (setting == "system" && logger->getName() == "") {
136  LoggerFactory::getInstance().reselectLoggingSystem(value);
137  }
138 
139 }
140 
142  const vector<string>& key) const {
143 
144  vector<string> normalizedKey;
145  for (vector<string>::const_iterator keyIt = key.begin(); keyIt != key.end(); ++keyIt) {
146  string part = *keyIt;
147  boost::algorithm::to_lower(part);
148  normalizedKey.push_back(part);
149  }
150 
151  return normalizedKey;
152 
153 }
154 
155 }
156 }
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.
STL namespace.
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