Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 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 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 25 | |
| 26 | #include "logger-factory.hpp" |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 27 | |
Alexander Afanasyev | 6459e64 | 2016-09-12 04:07:33 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/util/logging.hpp> |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 29 | |
| 30 | #include <boost/algorithm/string/case_conv.hpp> |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 31 | #include <boost/range/adaptor/map.hpp> |
| 32 | |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 33 | #ifdef HAVE_CUSTOM_LOGGER |
| 34 | #error "This file should not be compiled when custom logger is used" |
| 35 | #endif |
| 36 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 37 | namespace nfd { |
| 38 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 39 | NFD_LOG_INIT("LoggerFactory"); |
| 40 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 41 | LoggerFactory& |
| 42 | LoggerFactory::getInstance() |
| 43 | { |
| 44 | static LoggerFactory globalLoggerFactory; |
| 45 | |
| 46 | return globalLoggerFactory; |
| 47 | } |
| 48 | |
| 49 | LoggerFactory::LoggerFactory() |
| 50 | : m_defaultLevel(LOG_INFO) |
| 51 | { |
| 52 | m_levelNames["NONE"] = LOG_NONE; |
| 53 | m_levelNames["ERROR"] = LOG_ERROR; |
| 54 | m_levelNames["WARN"] = LOG_WARN; |
| 55 | m_levelNames["INFO"] = LOG_INFO; |
| 56 | m_levelNames["DEBUG"] = LOG_DEBUG; |
| 57 | m_levelNames["TRACE"] = LOG_TRACE; |
| 58 | m_levelNames["ALL"] = LOG_ALL; |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | 6459e64 | 2016-09-12 04:07:33 +0000 | [diff] [blame] | 60 | // Let ndn-cxx logging facility initialize Boost.Log backend, |
| 61 | // so that only one sink is attached to Boost.Log core. |
| 62 | ndn::util::Logging::setDestination(std::clog); |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | LoggerFactory::~LoggerFactory() |
| 66 | { |
Alexander Afanasyev | 6459e64 | 2016-09-12 04:07:33 +0000 | [diff] [blame] | 67 | ndn::util::Logging::flush(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
| 71 | LoggerFactory::setConfigFile(ConfigFile& config) |
| 72 | { |
| 73 | config.addSectionHandler("log", bind(&LoggerFactory::onConfig, this, _1, _2, _3)); |
| 74 | } |
| 75 | |
| 76 | LogLevel |
| 77 | LoggerFactory::parseLevel(const std::string& level) |
| 78 | { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 79 | std::string upperLevel = boost::to_upper_copy(level); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 80 | |
| 81 | // std::cerr << "parsing level: " << upperLevel << std::endl;; |
| 82 | // std::cerr << "# levels: " << m_levelNames.size() << std::endl; |
| 83 | // std::cerr << m_levelNames.begin()->first << std::endl; |
| 84 | |
| 85 | LevelMap::const_iterator levelIt = m_levelNames.find(upperLevel); |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 86 | if (levelIt != m_levelNames.end()) { |
| 87 | return levelIt->second; |
| 88 | } |
| 89 | try { |
| 90 | uint32_t levelNo = boost::lexical_cast<uint32_t>(level); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 91 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 92 | if ((boost::lexical_cast<uint32_t>(LOG_NONE) <= levelNo && |
| 93 | levelNo <= boost::lexical_cast<uint32_t>(LOG_TRACE)) || |
| 94 | levelNo == LOG_ALL) { |
| 95 | return static_cast<LogLevel>(levelNo); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 96 | } |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 97 | } |
| 98 | catch (const boost::bad_lexical_cast& error) { |
| 99 | } |
| 100 | |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 101 | BOOST_THROW_EXCEPTION(LoggerFactory::Error("Unsupported logging level \"" + level + "\"")); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 102 | } |
| 103 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 104 | LogLevel |
| 105 | LoggerFactory::extractLevel(const ConfigSection& item, const std::string& key) |
| 106 | { |
| 107 | std::string levelString; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 108 | try { |
| 109 | levelString = item.get_value<std::string>(); |
| 110 | } |
| 111 | catch (const boost::property_tree::ptree_error& error) { |
| 112 | } |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 113 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 114 | if (levelString.empty()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 115 | BOOST_THROW_EXCEPTION(LoggerFactory::Error("No logging level found for option \"" + key + "\"")); |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 116 | } |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 117 | |
| 118 | return parseLevel(levelString); |
| 119 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 120 | |
| 121 | void |
| 122 | LoggerFactory::onConfig(const ConfigSection& section, |
| 123 | bool isDryRun, |
| 124 | const std::string& filename) |
| 125 | { |
Alexander Afanasyev | 6459e64 | 2016-09-12 04:07:33 +0000 | [diff] [blame] | 126 | // log |
| 127 | // { |
| 128 | // ; default_level specifies the logging level for modules |
| 129 | // ; that are not explicitly named. All debugging levels |
| 130 | // ; listed above the selected value are enabled. |
| 131 | // |
| 132 | // default_level INFO |
| 133 | // |
| 134 | // ; You may also override the default for specific modules: |
| 135 | // |
| 136 | // FibManager DEBUG |
| 137 | // Forwarder WARN |
| 138 | // } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 139 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 140 | if (!isDryRun) { |
| 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); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 145 | } |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 146 | else { |
| 147 | setDefaultLevel(LOG_INFO); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 148 | } |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | for (const auto& i : section) { |
| 152 | LogLevel level = extractLevel(i.second, i.first); |
| 153 | |
| 154 | if (i.first == "default_level") { |
| 155 | // do nothing |
| 156 | } |
| 157 | else { |
| 158 | std::unique_lock<std::mutex> lock(m_loggersGuard); |
| 159 | LoggerMap::iterator loggerIt = m_loggers.find(i.first); |
| 160 | if (loggerIt == m_loggers.end()) { |
| 161 | lock.unlock(); |
| 162 | NFD_LOG_DEBUG("Failed to configure logging level for module \"" << |
| 163 | i.first << "\" (module not found)"); |
| 164 | } |
| 165 | else if (!isDryRun) { |
| 166 | loggerIt->second.setLogLevel(level); |
| 167 | lock.unlock(); |
| 168 | NFD_LOG_DEBUG("Changing level for module " << i.first << " to " << level); |
| 169 | } |
| 170 | } |
| 171 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void |
| 175 | LoggerFactory::setDefaultLevel(LogLevel level) |
| 176 | { |
| 177 | // std::cerr << "changing to default_level " << level << std::endl; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 178 | std::lock_guard<std::mutex> lock(m_loggersGuard); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 179 | |
| 180 | m_defaultLevel = level; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 181 | for (auto&& logger : m_loggers) { |
| 182 | // std::cerr << "changing " << i->first << " to default " << m_defaultLevel << std::endl; |
| 183 | logger.second.setLogLevel(m_defaultLevel); |
| 184 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 185 | } |
| 186 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 187 | void |
| 188 | LoggerFactory::flushBackend() |
| 189 | { |
Alexander Afanasyev | 6459e64 | 2016-09-12 04:07:33 +0000 | [diff] [blame] | 190 | ndn::util::Logging::flush(); |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 191 | } |
| 192 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 193 | Logger& |
| 194 | LoggerFactory::create(const std::string& moduleName) |
| 195 | { |
| 196 | return LoggerFactory::getInstance().createLogger(moduleName); |
| 197 | } |
| 198 | |
| 199 | Logger& |
| 200 | LoggerFactory::createLogger(const std::string& moduleName) |
| 201 | { |
| 202 | // std::cerr << "creating logger for " << moduleName |
| 203 | // << " with level " << m_defaultLevel << std::endl; |
| 204 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 205 | std::lock_guard<std::mutex> lock(m_loggersGuard); |
| 206 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 207 | std::pair<LoggerMap::iterator, bool> loggerIt = |
| 208 | m_loggers.insert(NameAndLogger(moduleName, Logger(moduleName, m_defaultLevel))); |
| 209 | |
| 210 | return loggerIt.first->second; |
| 211 | } |
| 212 | |
| 213 | std::list<std::string> |
| 214 | LoggerFactory::getModules() const |
| 215 | { |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 216 | std::lock_guard<std::mutex> lock(m_loggersGuard); |
| 217 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 218 | std::list<std::string> modules; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 219 | for (const auto& loggerName : m_loggers | boost::adaptors::map_keys) { |
| 220 | modules.push_back(loggerName); |
| 221 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 222 | |
| 223 | return modules; |
| 224 | } |
| 225 | |
| 226 | } // namespace nfd |