28 #include <ndn-cxx/util/logging.hpp>
30 #include <boost/algorithm/string/case_conv.hpp>
31 #include <boost/range/adaptor/map.hpp>
33 #ifdef HAVE_CUSTOM_LOGGER
34 #error "This file should not be compiled when custom logger is used"
46 return globalLoggerFactory;
49 LoggerFactory::LoggerFactory()
62 ndn::util::Logging::setDestination(std::clog);
65 LoggerFactory::~LoggerFactory()
67 ndn::util::Logging::flush();
77 LoggerFactory::parseLevel(
const std::string& level)
79 std::string upperLevel = boost::to_upper_copy(level);
85 LevelMap::const_iterator levelIt = m_levelNames.find(upperLevel);
86 if (levelIt != m_levelNames.end()) {
87 return levelIt->second;
90 uint32_t levelNo = boost::lexical_cast<uint32_t>(level);
92 if ((boost::lexical_cast<uint32_t>(
LOG_NONE) <= levelNo &&
93 levelNo <= boost::lexical_cast<uint32_t>(
LOG_TRACE)) ||
95 return static_cast<LogLevel>(levelNo);
98 catch (
const boost::bad_lexical_cast& error) {
101 BOOST_THROW_EXCEPTION(LoggerFactory::Error(
"Unsupported logging level \"" + level +
"\""));
105 LoggerFactory::extractLevel(
const ConfigSection& item,
const std::string& key)
107 std::string levelString;
109 levelString = item.get_value<std::string>();
111 catch (
const boost::property_tree::ptree_error& error) {
114 if (levelString.empty()) {
115 BOOST_THROW_EXCEPTION(LoggerFactory::Error(
"No logging level found for option \"" + key +
"\""));
118 return parseLevel(levelString);
124 const std::string& filename)
141 ConfigSection::const_assoc_iterator item = section.find(
"default_level");
142 if (item != section.not_found()) {
143 LogLevel level = extractLevel(item->second,
"default_level");
144 setDefaultLevel(level);
151 for (
const auto& i : section) {
152 LogLevel level = extractLevel(i.second, i.first);
154 if (i.first ==
"default_level") {
158 std::unique_lock<std::mutex> lock(m_loggersGuard);
160 if (loggerIt == m_loggers.end()) {
162 NFD_LOG_DEBUG(
"Failed to configure logging level for module \"" <<
163 i.first <<
"\" (module not found)");
165 else if (!isDryRun) {
166 loggerIt->second.setLogLevel(level);
168 NFD_LOG_DEBUG(
"Changing level for module " << i.first <<
" to " << level);
175 LoggerFactory::setDefaultLevel(
LogLevel level)
178 std::lock_guard<std::mutex> lock(m_loggersGuard);
180 m_defaultLevel = level;
181 for (
auto&& logger : m_loggers) {
183 logger.second.setLogLevel(m_defaultLevel);
188 LoggerFactory::flushBackend()
190 ndn::util::Logging::flush();
200 LoggerFactory::createLogger(
const std::string& moduleName)
205 std::lock_guard<std::mutex> lock(m_loggersGuard);
207 std::pair<LoggerMap::iterator, bool> loggerIt =
208 m_loggers.insert(NameAndLogger(moduleName,
Logger(moduleName, m_defaultLevel)));
210 return loggerIt.first->second;
213 std::list<std::string>
216 std::lock_guard<std::mutex> lock(m_loggersGuard);
218 std::list<std::string> modules;
219 for (
const auto& loggerName : m_loggers | boost::adaptors::map_keys) {
220 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