28 #include <ndn-cxx/util/logging.hpp> 30 #include <boost/algorithm/string/case_conv.hpp> 31 #include <boost/range/adaptor/map.hpp> 35 #ifdef HAVE_CUSTOM_LOGGER 36 #error "This file should not be compiled when custom logger is used" 48 return globalLoggerFactory;
51 LoggerFactory::LoggerFactory()
64 ndn::util::Logging::setDestination(std::clog);
67 LoggerFactory::~LoggerFactory()
69 ndn::util::Logging::flush();
79 LoggerFactory::parseLevel(
const std::string& level)
81 std::string upperLevel = boost::to_upper_copy(level);
87 LevelMap::const_iterator levelIt = m_levelNames.find(upperLevel);
88 if (levelIt != m_levelNames.end()) {
89 return levelIt->second;
92 uint32_t levelNo = boost::lexical_cast<uint32_t>(level);
94 if ((boost::lexical_cast<uint32_t>(
LOG_NONE) <= levelNo &&
95 levelNo <= boost::lexical_cast<uint32_t>(
LOG_TRACE)) ||
97 return static_cast<LogLevel>(levelNo);
100 catch (
const boost::bad_lexical_cast& error) {
107 LoggerFactory::extractLevel(
const ConfigSection& item,
const std::string& key)
109 std::string levelString;
111 levelString = item.get_value<std::string>();
113 catch (
const boost::property_tree::ptree_error& error) {
116 if (levelString.empty()) {
117 BOOST_THROW_EXCEPTION(
LoggerFactory::Error(
"No logging level found for option \"" + key +
"\""));
120 return parseLevel(levelString);
126 const std::string& filename)
143 ConfigSection::const_assoc_iterator item = section.find(
"default_level");
144 if (item != section.not_found()) {
145 LogLevel level = extractLevel(item->second,
"default_level");
146 setDefaultLevel(level);
153 for (
const auto& i : section) {
154 LogLevel level = extractLevel(i.second, i.first);
156 if (i.first ==
"default_level") {
160 std::unique_lock<std::mutex> lock(m_loggersGuard);
162 if (loggerIt == m_loggers.end()) {
164 NFD_LOG_DEBUG(
"Failed to configure logging level for module \"" <<
165 i.first <<
"\" (module not found)");
167 else if (!isDryRun) {
168 loggerIt->second.setLogLevel(level);
170 NFD_LOG_DEBUG(
"Changing level for module " << i.first <<
" to " << level);
177 LoggerFactory::setDefaultLevel(
LogLevel level)
180 std::lock_guard<std::mutex> lock(m_loggersGuard);
182 m_defaultLevel = level;
183 for (
auto&& logger : m_loggers) {
185 logger.second.setLogLevel(m_defaultLevel);
190 LoggerFactory::flushBackend()
192 ndn::util::Logging::flush();
202 LoggerFactory::createLogger(
const std::string& moduleName)
207 std::lock_guard<std::mutex> lock(m_loggersGuard);
209 std::pair<LoggerMap::iterator, bool> loggerIt =
210 m_loggers.insert(NameAndLogger(moduleName,
Logger(moduleName, m_defaultLevel)));
212 return loggerIt.first->second;
215 std::list<std::string>
218 std::lock_guard<std::mutex> lock(m_loggersGuard);
220 std::list<std::string> modules;
221 for (
const auto& loggerName : m_loggers | boost::adaptors::map_keys) {
222 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
a config file section
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