27 #include <ndn-cxx/util/logging.hpp>
28 #include <boost/range/adaptor/map.hpp>
30 #ifdef HAVE_CUSTOM_LOGGER
31 #error "This file should not be compiled when custom logger is used"
43 return globalLoggerFactory;
46 LoggerFactory::LoggerFactory()
59 ndn::util::Logging::setDestination(std::clog);
62 LoggerFactory::~LoggerFactory()
64 ndn::util::Logging::flush();
74 LoggerFactory::parseLevel(
const std::string& level)
76 std::string upperLevel = level;
77 boost::to_upper(upperLevel);
83 LevelMap::const_iterator levelIt = m_levelNames.find(upperLevel);
84 if (levelIt != m_levelNames.end()) {
85 return levelIt->second;
88 uint32_t levelNo = boost::lexical_cast<uint32_t>(level);
90 if ((boost::lexical_cast<uint32_t>(
LOG_NONE) <= levelNo &&
91 levelNo <= boost::lexical_cast<uint32_t>(
LOG_TRACE)) ||
93 return static_cast<LogLevel>(levelNo);
96 catch (
const boost::bad_lexical_cast& error) {
99 BOOST_THROW_EXCEPTION(LoggerFactory::Error(
"Unsupported logging level \"" + level +
"\""));
103 LoggerFactory::extractLevel(
const ConfigSection& item,
const std::string& key)
105 std::string levelString;
107 levelString = item.get_value<std::string>();
109 catch (
const boost::property_tree::ptree_error& error) {
112 if (levelString.empty()) {
113 BOOST_THROW_EXCEPTION(LoggerFactory::Error(
"No logging level found for option \"" + key +
"\""));
116 return parseLevel(levelString);
122 const std::string& filename)
139 ConfigSection::const_assoc_iterator item = section.find(
"default_level");
140 if (item != section.not_found()) {
141 LogLevel level = extractLevel(item->second,
"default_level");
142 setDefaultLevel(level);
149 for (
const auto& i : section) {
150 LogLevel level = extractLevel(i.second, i.first);
152 if (i.first ==
"default_level") {
156 std::unique_lock<std::mutex> lock(m_loggersGuard);
158 if (loggerIt == m_loggers.end()) {
160 NFD_LOG_DEBUG(
"Failed to configure logging level for module \"" <<
161 i.first <<
"\" (module not found)");
163 else if (!isDryRun) {
164 loggerIt->second.setLogLevel(level);
166 NFD_LOG_DEBUG(
"Changing level for module " << i.first <<
" to " << level);
173 LoggerFactory::setDefaultLevel(
LogLevel level)
176 std::lock_guard<std::mutex> lock(m_loggersGuard);
178 m_defaultLevel = level;
179 for (
auto&& logger : m_loggers) {
181 logger.second.setLogLevel(m_defaultLevel);
186 LoggerFactory::flushBackend()
188 ndn::util::Logging::flush();
198 LoggerFactory::createLogger(
const std::string& moduleName)
203 std::lock_guard<std::mutex> lock(m_loggersGuard);
205 std::pair<LoggerMap::iterator, bool> loggerIt =
206 m_loggers.insert(NameAndLogger(moduleName,
Logger(moduleName, m_defaultLevel)));
208 return loggerIt.first->second;
211 std::list<std::string>
214 std::lock_guard<std::mutex> lock(m_loggersGuard);
216 std::list<std::string> modules;
217 for (
const auto& loggerName : m_loggers | boost::adaptors::map_keys) {
218 modules.push_back(loggerName);
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
#define NFD_LOG_DEBUG(expression)
configuration file parsing utility
void setConfigFile(ConfigFile &config)
void onConfig(const ConfigSection §ion, bool isDryRun, const std::string &filename)
static LoggerFactory & getInstance()
Table::const_iterator iterator
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
boost::property_tree::ptree ConfigSection
static Logger & create(const std::string &moduleName)
#define NFD_LOG_INIT(name)
LogLevel
indicates a log level
provides logging for a module
std::list< std::string > getModules() const