blob: 6805af9db6538ba708a10622c9f48d3afd52b8cd [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
76 if ((LOG_NONE <= levelNo && levelNo <= LOG_TRACE) ||
77 levelNo == LOG_ALL)
78 {
79 return static_cast<LogLevel>(levelNo);
80 }
81 }
82 catch (const boost::bad_lexical_cast& error)
83 {
84 }
85 throw LoggerFactory::Error("Unsupported logging level \"" +
86 level + "\"");
87}
88
89
90void
91LoggerFactory::onConfig(const ConfigSection& section,
92 bool isDryRun,
93 const std::string& filename)
94{
95// log
96// {
97// ; default_level specifies the logging level for modules
98// ; that are not explicitly named. All debugging levels
99// ; listed above the selected value are enabled.
100//
101// default_level INFO
102//
103// ; You may also override the default for specific modules:
104//
105// FibManager DEBUG
106// Forwarder WARN
107// }
108
109 // std::cerr << "loading logging configuration" << std::endl;
110 for (ConfigSection::const_iterator item = section.begin();
111 item != section.end();
112 ++item)
113 {
114 std::string levelString;
115 try
116 {
117 levelString = item->second.get_value<std::string>();
118 }
119 catch (const boost::property_tree::ptree_error& error)
120 {
121 }
122
123 if (levelString.empty())
124 {
125 throw LoggerFactory::Error("No logging level found for option \"" + item->first + "\"");
126 }
127
128 LogLevel level = parseLevel(levelString);
129
130 if (item->first == "default_level")
131 {
132 if (!isDryRun)
133 {
134 setDefaultLevel(level);
135 }
136 }
137 else
138 {
139 LoggerMap::iterator loggerIt = m_loggers.find(item->first);
140 if (loggerIt == m_loggers.end())
141 {
Steve DiBenedetto6ad48ca2014-04-22 09:39:31 -0600142 NFD_LOG_DEBUG("Failed to configure logging level for module \"" <<
143 item->first << "\" (module not found)");
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600144 }
Steve DiBenedetto2e948b12014-05-03 21:02:47 -0600145 else if (!isDryRun)
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600146 {
147 // std::cerr << "changing level for module " << item->first << " to " << level << std::endl;
148 loggerIt->second.setLogLevel(level);
149 }
150 }
151 }
152}
153
154void
155LoggerFactory::setDefaultLevel(LogLevel level)
156{
157 // std::cerr << "changing to default_level " << level << std::endl;
158
159 m_defaultLevel = level;
160 for (LoggerMap::iterator i = m_loggers.begin(); i != m_loggers.end(); ++i)
161 {
162 // std::cerr << "changing " << i->first << " to default " << m_defaultLevel << std::endl;
163 i->second.setLogLevel(m_defaultLevel);
164 }
165}
166
167Logger&
168LoggerFactory::create(const std::string& moduleName)
169{
170 return LoggerFactory::getInstance().createLogger(moduleName);
171}
172
173Logger&
174LoggerFactory::createLogger(const std::string& moduleName)
175{
176 // std::cerr << "creating logger for " << moduleName
177 // << " with level " << m_defaultLevel << std::endl;
178
179 std::pair<LoggerMap::iterator, bool> loggerIt =
180 m_loggers.insert(NameAndLogger(moduleName, Logger(moduleName, m_defaultLevel)));
181
182 return loggerIt.first->second;
183}
184
185std::list<std::string>
186LoggerFactory::getModules() const
187{
188 std::list<std::string> modules;
189 for (LoggerMap::const_iterator i = m_loggers.begin(); i != m_loggers.end(); ++i)
190 {
191 modules.push_back(i->first);
192 }
193
194 return modules;
195}
196
197} // namespace nfd