RSC  0.7.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 "Logger.h"
30 #include "LoggerFactory.h"
31 
32 using namespace std;
33 using namespace rsc::config;
34 
35 namespace rsc {
36 namespace logging {
37 
38 OptionBasedConfigurator::OptionBasedConfigurator(
39  const vector<string>& rootOption) :
40  rootOption(rootOption) {
41 }
42 
44 }
45 
47  vector<string> root;
48  root.push_back("rsc");
49  root.push_back("logging");
50  return root;
51 }
52 
54  return rootOption;
55 }
56 
58  const vector<string>& key) const {
59 
60  if (key.size() < rootOption.size()) {
61  return false;
62  }
63 
64  for (size_t i = 0; i < rootOption.size(); ++i) {
65  if (rootOption[i] != key[i]) {
66  return false;
67  }
68  }
69 
70  return true;
71 
72 }
73 
75  const vector<string>& key) const {
76 
77  string name;
78  // the last fragment must be the setting, do not include in the name
79  for (vector<string>::const_iterator keyIt = key.begin() + rootOption.size();
80  keyIt != key.end() - 1; ++keyIt) {
81 
82  name += "." + *keyIt;
83 
84  }
85 
86  if (name.empty()) {
87  return name;
88  } else {
89  return name.substr(1, name.size() - 1);
90  }
91 
92 }
93 
94 void OptionBasedConfigurator::handleOption(const vector<string>& key,
95  const string& value) {
96 
97  if (!keyStartWithRoot(key)) {
98  return;
99  }
100 
101  // something can only be a valid key for the configuration if there is a
102  // chance that a special configuration key is at the end. Hence, sufficient
103  // length is required
104  if (key.size() <= rootOption.size()) {
105  return;
106  }
107 
109 
110  string setting = key.back();
111  if (setting == "LEVEL") {
112 
113  if (value == "ALL") {
114  logger->setLevel(Logger::LEVEL_ALL);
115  } else if (value == "TRACE") {
116  logger->setLevel(Logger::LEVEL_TRACE);
117  } else if (value == "DEBUG") {
118  logger->setLevel(Logger::LEVEL_DEBUG);
119  } else if (value == "INFO") {
120  logger->setLevel(Logger::LEVEL_INFO);
121  } else if (value == "WARN") {
122  logger->setLevel(Logger::LEVEL_WARN);
123  } else if (value == "ERROR") {
124  logger->setLevel(Logger::LEVEL_ERROR);
125  } else if (value == "FATAL") {
126  logger->setLevel(Logger::LEVEL_FATAL);
127  } else if (value == "OFF") {
128  logger->setLevel(Logger::LEVEL_OFF);
129  }
130 
131  } else if (setting == "SYSTEM" && logger->getName() == "") {
132  LoggerFactory::getInstance().reselectLoggingSystem(value);
133  }
134 
135 }
136 
137 }
138 }
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.
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