blob: 61ca866647a166d239817be3aa3351dc48688394 [file] [log] [blame]
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology
9 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060024
25#include "logger-factory.hpp"
26
27namespace nfd {
28
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -060029NFD_LOG_INIT("LoggerFactory");
30
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060031LoggerFactory&
32LoggerFactory::getInstance()
33{
34 static LoggerFactory globalLoggerFactory;
35
36 return globalLoggerFactory;
37}
38
39LoggerFactory::LoggerFactory()
40 : m_defaultLevel(LOG_INFO)
41{
42 m_levelNames["NONE"] = LOG_NONE;
43 m_levelNames["ERROR"] = LOG_ERROR;
44 m_levelNames["WARN"] = LOG_WARN;
45 m_levelNames["INFO"] = LOG_INFO;
46 m_levelNames["DEBUG"] = LOG_DEBUG;
47 m_levelNames["TRACE"] = LOG_TRACE;
48 m_levelNames["ALL"] = LOG_ALL;
49}
50
51void
52LoggerFactory::setConfigFile(ConfigFile& config)
53{
54 config.addSectionHandler("log", bind(&LoggerFactory::onConfig, this, _1, _2, _3));
55}
56
57LogLevel
58LoggerFactory::parseLevel(const std::string& level)
59{
60 std::string upperLevel = level;
61 boost::to_upper(upperLevel);
62
63 // std::cerr << "parsing level: " << upperLevel << std::endl;;
64 // std::cerr << "# levels: " << m_levelNames.size() << std::endl;
65 // std::cerr << m_levelNames.begin()->first << std::endl;
66
67 LevelMap::const_iterator levelIt = m_levelNames.find(upperLevel);
68 if (levelIt != m_levelNames.end())
69 {
70 return levelIt->second;
71 }
72 try
73 {
74 uint32_t levelNo = boost::lexical_cast<uint32_t>(level);
75
Alexander Afanasyevf4e89b42014-05-31 15:54:18 +030076 if ((boost::lexical_cast<uint32_t>(LOG_NONE) <= levelNo &&
77 levelNo <= boost::lexical_cast<uint32_t>(LOG_TRACE)) ||
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060078 levelNo == LOG_ALL)
79 {
80 return static_cast<LogLevel>(levelNo);
81 }
82 }
83 catch (const boost::bad_lexical_cast& error)
84 {
85 }
86 throw LoggerFactory::Error("Unsupported logging level \"" +
87 level + "\"");
88}
89
90
91void
92LoggerFactory::onConfig(const ConfigSection& section,
93 bool isDryRun,
94 const std::string& filename)
95{
96// log
97// {
98// ; default_level specifies the logging level for modules
99// ; that are not explicitly named. All debugging levels
100// ; listed above the selected value are enabled.
101//
102// default_level INFO
103//
104// ; You may also override the default for specific modules:
105//
106// FibManager DEBUG
107// Forwarder WARN
108// }
109
110 // std::cerr << "loading logging configuration" << std::endl;
111 for (ConfigSection::const_iterator item = section.begin();
112 item != section.end();
113 ++item)
114 {
115 std::string levelString;
116 try
117 {
118 levelString = item->second.get_value<std::string>();
119 }
120 catch (const boost::property_tree::ptree_error& error)
121 {
122 }
123
124 if (levelString.empty())
125 {
126 throw LoggerFactory::Error("No logging level found for option \"" + item->first + "\"");
127 }
128
129 LogLevel level = parseLevel(levelString);
130
131 if (item->first == "default_level")
132 {
133 if (!isDryRun)
134 {
135 setDefaultLevel(level);
136 }
137 }
138 else
139 {
140 LoggerMap::iterator loggerIt = m_loggers.find(item->first);
141 if (loggerIt == m_loggers.end())
142 {
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600143 NFD_LOG_DEBUG("Failed to configure logging level for module \"" <<
144 item->first << "\" (module not found)");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600145 }
Steve DiBenedetto2e948b12014-05-03 21:02:47 -0600146 else if (!isDryRun)
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600147 {
148 // std::cerr << "changing level for module " << item->first << " to " << level << std::endl;
149 loggerIt->second.setLogLevel(level);
150 }
151 }
152 }
153}
154
155void
156LoggerFactory::setDefaultLevel(LogLevel level)
157{
158 // std::cerr << "changing to default_level " << level << std::endl;
159
160 m_defaultLevel = level;
161 for (LoggerMap::iterator i = m_loggers.begin(); i != m_loggers.end(); ++i)
162 {
163 // std::cerr << "changing " << i->first << " to default " << m_defaultLevel << std::endl;
164 i->second.setLogLevel(m_defaultLevel);
165 }
166}
167
168Logger&
169LoggerFactory::create(const std::string& moduleName)
170{
171 return LoggerFactory::getInstance().createLogger(moduleName);
172}
173
174Logger&
175LoggerFactory::createLogger(const std::string& moduleName)
176{
177 // std::cerr << "creating logger for " << moduleName
178 // << " with level " << m_defaultLevel << std::endl;
179
180 std::pair<LoggerMap::iterator, bool> loggerIt =
181 m_loggers.insert(NameAndLogger(moduleName, Logger(moduleName, m_defaultLevel)));
182
183 return loggerIt.first->second;
184}
185
186std::list<std::string>
187LoggerFactory::getModules() const
188{
189 std::list<std::string> modules;
190 for (LoggerMap::const_iterator i = m_loggers.begin(); i != m_loggers.end(); ++i)
191 {
192 modules.push_back(i->first);
193 }
194
195 return modules;
196}
197
198} // namespace nfd