blob: 3a738ebfe437ce4e9067a368db2d8a9d1db03362 [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
29LoggerFactory&
30LoggerFactory::getInstance()
31{
32 static LoggerFactory globalLoggerFactory;
33
34 return globalLoggerFactory;
35}
36
37LoggerFactory::LoggerFactory()
38 : m_defaultLevel(LOG_INFO)
39{
40 m_levelNames["NONE"] = LOG_NONE;
41 m_levelNames["ERROR"] = LOG_ERROR;
42 m_levelNames["WARN"] = LOG_WARN;
43 m_levelNames["INFO"] = LOG_INFO;
44 m_levelNames["DEBUG"] = LOG_DEBUG;
45 m_levelNames["TRACE"] = LOG_TRACE;
46 m_levelNames["ALL"] = LOG_ALL;
47}
48
49void
50LoggerFactory::setConfigFile(ConfigFile& config)
51{
52 config.addSectionHandler("log", bind(&LoggerFactory::onConfig, this, _1, _2, _3));
53}
54
55LogLevel
56LoggerFactory::parseLevel(const std::string& level)
57{
58 std::string upperLevel = level;
59 boost::to_upper(upperLevel);
60
61 // std::cerr << "parsing level: " << upperLevel << std::endl;;
62 // std::cerr << "# levels: " << m_levelNames.size() << std::endl;
63 // std::cerr << m_levelNames.begin()->first << std::endl;
64
65 LevelMap::const_iterator levelIt = m_levelNames.find(upperLevel);
66 if (levelIt != m_levelNames.end())
67 {
68 return levelIt->second;
69 }
70 try
71 {
72 uint32_t levelNo = boost::lexical_cast<uint32_t>(level);
73
74 if ((LOG_NONE <= levelNo && levelNo <= LOG_TRACE) ||
75 levelNo == LOG_ALL)
76 {
77 return static_cast<LogLevel>(levelNo);
78 }
79 }
80 catch (const boost::bad_lexical_cast& error)
81 {
82 }
83 throw LoggerFactory::Error("Unsupported logging level \"" +
84 level + "\"");
85}
86
87
88void
89LoggerFactory::onConfig(const ConfigSection& section,
90 bool isDryRun,
91 const std::string& filename)
92{
93// log
94// {
95// ; default_level specifies the logging level for modules
96// ; that are not explicitly named. All debugging levels
97// ; listed above the selected value are enabled.
98//
99// default_level INFO
100//
101// ; You may also override the default for specific modules:
102//
103// FibManager DEBUG
104// Forwarder WARN
105// }
106
107 // std::cerr << "loading logging configuration" << std::endl;
108 for (ConfigSection::const_iterator item = section.begin();
109 item != section.end();
110 ++item)
111 {
112 std::string levelString;
113 try
114 {
115 levelString = item->second.get_value<std::string>();
116 }
117 catch (const boost::property_tree::ptree_error& error)
118 {
119 }
120
121 if (levelString.empty())
122 {
123 throw LoggerFactory::Error("No logging level found for option \"" + item->first + "\"");
124 }
125
126 LogLevel level = parseLevel(levelString);
127
128 if (item->first == "default_level")
129 {
130 if (!isDryRun)
131 {
132 setDefaultLevel(level);
133 }
134 }
135 else
136 {
137 LoggerMap::iterator loggerIt = m_loggers.find(item->first);
138 if (loggerIt == m_loggers.end())
139 {
140 throw LoggerFactory::Error("Invalid module name \"" +
141 item->first + "\" in configuration file");
142 }
143
144 if (!isDryRun)
145 {
146 // std::cerr << "changing level for module " << item->first << " to " << level << std::endl;
147 loggerIt->second.setLogLevel(level);
148 }
149 }
150 }
151}
152
153void
154LoggerFactory::setDefaultLevel(LogLevel level)
155{
156 // std::cerr << "changing to default_level " << level << std::endl;
157
158 m_defaultLevel = level;
159 for (LoggerMap::iterator i = m_loggers.begin(); i != m_loggers.end(); ++i)
160 {
161 // std::cerr << "changing " << i->first << " to default " << m_defaultLevel << std::endl;
162 i->second.setLogLevel(m_defaultLevel);
163 }
164}
165
166Logger&
167LoggerFactory::create(const std::string& moduleName)
168{
169 return LoggerFactory::getInstance().createLogger(moduleName);
170}
171
172Logger&
173LoggerFactory::createLogger(const std::string& moduleName)
174{
175 // std::cerr << "creating logger for " << moduleName
176 // << " with level " << m_defaultLevel << std::endl;
177
178 std::pair<LoggerMap::iterator, bool> loggerIt =
179 m_loggers.insert(NameAndLogger(moduleName, Logger(moduleName, m_defaultLevel)));
180
181 return loggerIt.first->second;
182}
183
184std::list<std::string>
185LoggerFactory::getModules() const
186{
187 std::list<std::string> modules;
188 for (LoggerMap::const_iterator i = m_loggers.begin(); i != m_loggers.end(); ++i)
189 {
190 modules.push_back(i->first);
191 }
192
193 return modules;
194}
195
196} // namespace nfd