blob: 4a353f19241f6d86173a1a7940a44e933b88106d [file] [log] [blame]
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "logger-factory.hpp"
8
9namespace nfd {
10
11LoggerFactory&
12LoggerFactory::getInstance()
13{
14 static LoggerFactory globalLoggerFactory;
15
16 return globalLoggerFactory;
17}
18
19LoggerFactory::LoggerFactory()
20 : m_defaultLevel(LOG_INFO)
21{
22 m_levelNames["NONE"] = LOG_NONE;
23 m_levelNames["ERROR"] = LOG_ERROR;
24 m_levelNames["WARN"] = LOG_WARN;
25 m_levelNames["INFO"] = LOG_INFO;
26 m_levelNames["DEBUG"] = LOG_DEBUG;
27 m_levelNames["TRACE"] = LOG_TRACE;
28 m_levelNames["ALL"] = LOG_ALL;
29}
30
31void
32LoggerFactory::setConfigFile(ConfigFile& config)
33{
34 config.addSectionHandler("log", bind(&LoggerFactory::onConfig, this, _1, _2, _3));
35}
36
37LogLevel
38LoggerFactory::parseLevel(const std::string& level)
39{
40 std::string upperLevel = level;
41 boost::to_upper(upperLevel);
42
43 // std::cerr << "parsing level: " << upperLevel << std::endl;;
44 // std::cerr << "# levels: " << m_levelNames.size() << std::endl;
45 // std::cerr << m_levelNames.begin()->first << std::endl;
46
47 LevelMap::const_iterator levelIt = m_levelNames.find(upperLevel);
48 if (levelIt != m_levelNames.end())
49 {
50 return levelIt->second;
51 }
52 try
53 {
54 uint32_t levelNo = boost::lexical_cast<uint32_t>(level);
55
56 if ((LOG_NONE <= levelNo && levelNo <= LOG_TRACE) ||
57 levelNo == LOG_ALL)
58 {
59 return static_cast<LogLevel>(levelNo);
60 }
61 }
62 catch (const boost::bad_lexical_cast& error)
63 {
64 }
65 throw LoggerFactory::Error("Unsupported logging level \"" +
66 level + "\"");
67}
68
69
70void
71LoggerFactory::onConfig(const ConfigSection& section,
72 bool isDryRun,
73 const std::string& filename)
74{
75// log
76// {
77// ; default_level specifies the logging level for modules
78// ; that are not explicitly named. All debugging levels
79// ; listed above the selected value are enabled.
80//
81// default_level INFO
82//
83// ; You may also override the default for specific modules:
84//
85// FibManager DEBUG
86// Forwarder WARN
87// }
88
89 // std::cerr << "loading logging configuration" << std::endl;
90 for (ConfigSection::const_iterator item = section.begin();
91 item != section.end();
92 ++item)
93 {
94 std::string levelString;
95 try
96 {
97 levelString = item->second.get_value<std::string>();
98 }
99 catch (const boost::property_tree::ptree_error& error)
100 {
101 }
102
103 if (levelString.empty())
104 {
105 throw LoggerFactory::Error("No logging level found for option \"" + item->first + "\"");
106 }
107
108 LogLevel level = parseLevel(levelString);
109
110 if (item->first == "default_level")
111 {
112 if (!isDryRun)
113 {
114 setDefaultLevel(level);
115 }
116 }
117 else
118 {
119 LoggerMap::iterator loggerIt = m_loggers.find(item->first);
120 if (loggerIt == m_loggers.end())
121 {
122 throw LoggerFactory::Error("Invalid module name \"" +
123 item->first + "\" in configuration file");
124 }
125
126 if (!isDryRun)
127 {
128 // std::cerr << "changing level for module " << item->first << " to " << level << std::endl;
129 loggerIt->second.setLogLevel(level);
130 }
131 }
132 }
133}
134
135void
136LoggerFactory::setDefaultLevel(LogLevel level)
137{
138 // std::cerr << "changing to default_level " << level << std::endl;
139
140 m_defaultLevel = level;
141 for (LoggerMap::iterator i = m_loggers.begin(); i != m_loggers.end(); ++i)
142 {
143 // std::cerr << "changing " << i->first << " to default " << m_defaultLevel << std::endl;
144 i->second.setLogLevel(m_defaultLevel);
145 }
146}
147
148Logger&
149LoggerFactory::create(const std::string& moduleName)
150{
151 return LoggerFactory::getInstance().createLogger(moduleName);
152}
153
154Logger&
155LoggerFactory::createLogger(const std::string& moduleName)
156{
157 // std::cerr << "creating logger for " << moduleName
158 // << " with level " << m_defaultLevel << std::endl;
159
160 std::pair<LoggerMap::iterator, bool> loggerIt =
161 m_loggers.insert(NameAndLogger(moduleName, Logger(moduleName, m_defaultLevel)));
162
163 return loggerIt.first->second;
164}
165
166std::list<std::string>
167LoggerFactory::getModules() const
168{
169 std::list<std::string> modules;
170 for (LoggerMap::const_iterator i = m_loggers.begin(); i != m_loggers.end(); ++i)
171 {
172 modules.push_back(i->first);
173 }
174
175 return modules;
176}
177
178} // namespace nfd