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" |
Alexander Afanasyev | 6459e64 | 2016-09-12 04:07:33 +0000 | [diff] [blame^] | 27 | #include <ndn-cxx/util/logging.hpp> |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 28 | #include <boost/range/adaptor/map.hpp> |
| 29 | |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 30 | #ifdef HAVE_CUSTOM_LOGGER |
| 31 | #error "This file should not be compiled when custom logger is used" |
| 32 | #endif |
| 33 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 34 | namespace nfd { |
| 35 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame] | 36 | NFD_LOG_INIT("LoggerFactory"); |
| 37 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 38 | LoggerFactory& |
| 39 | LoggerFactory::getInstance() |
| 40 | { |
| 41 | static LoggerFactory globalLoggerFactory; |
| 42 | |
| 43 | return globalLoggerFactory; |
| 44 | } |
| 45 | |
| 46 | LoggerFactory::LoggerFactory() |
| 47 | : m_defaultLevel(LOG_INFO) |
| 48 | { |
| 49 | m_levelNames["NONE"] = LOG_NONE; |
| 50 | m_levelNames["ERROR"] = LOG_ERROR; |
| 51 | m_levelNames["WARN"] = LOG_WARN; |
| 52 | m_levelNames["INFO"] = LOG_INFO; |
| 53 | m_levelNames["DEBUG"] = LOG_DEBUG; |
| 54 | m_levelNames["TRACE"] = LOG_TRACE; |
| 55 | m_levelNames["ALL"] = LOG_ALL; |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | 6459e64 | 2016-09-12 04:07:33 +0000 | [diff] [blame^] | 57 | // Let ndn-cxx logging facility initialize Boost.Log backend, |
| 58 | // so that only one sink is attached to Boost.Log core. |
| 59 | ndn::util::Logging::setDestination(std::clog); |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | LoggerFactory::~LoggerFactory() |
| 63 | { |
Alexander Afanasyev | 6459e64 | 2016-09-12 04:07:33 +0000 | [diff] [blame^] | 64 | ndn::util::Logging::flush(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
| 68 | LoggerFactory::setConfigFile(ConfigFile& config) |
| 69 | { |
| 70 | config.addSectionHandler("log", bind(&LoggerFactory::onConfig, this, _1, _2, _3)); |
| 71 | } |
| 72 | |
| 73 | LogLevel |
| 74 | LoggerFactory::parseLevel(const std::string& level) |
| 75 | { |
| 76 | std::string upperLevel = level; |
| 77 | boost::to_upper(upperLevel); |
| 78 | |
| 79 | // std::cerr << "parsing level: " << upperLevel << std::endl;; |
| 80 | // std::cerr << "# levels: " << m_levelNames.size() << std::endl; |
| 81 | // std::cerr << m_levelNames.begin()->first << std::endl; |
| 82 | |
| 83 | LevelMap::const_iterator levelIt = m_levelNames.find(upperLevel); |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 84 | if (levelIt != m_levelNames.end()) { |
| 85 | return levelIt->second; |
| 86 | } |
| 87 | try { |
| 88 | uint32_t levelNo = boost::lexical_cast<uint32_t>(level); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 89 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 90 | if ((boost::lexical_cast<uint32_t>(LOG_NONE) <= levelNo && |
| 91 | levelNo <= boost::lexical_cast<uint32_t>(LOG_TRACE)) || |
| 92 | levelNo == LOG_ALL) { |
| 93 | return static_cast<LogLevel>(levelNo); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 94 | } |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 95 | } |
| 96 | catch (const boost::bad_lexical_cast& error) { |
| 97 | } |
| 98 | |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 99 | BOOST_THROW_EXCEPTION(LoggerFactory::Error("Unsupported logging level \"" + level + "\"")); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 100 | } |
| 101 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 102 | LogLevel |
| 103 | LoggerFactory::extractLevel(const ConfigSection& item, const std::string& key) |
| 104 | { |
| 105 | std::string levelString; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 106 | try { |
| 107 | levelString = item.get_value<std::string>(); |
| 108 | } |
| 109 | catch (const boost::property_tree::ptree_error& error) { |
| 110 | } |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 111 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 112 | if (levelString.empty()) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 113 | BOOST_THROW_EXCEPTION(LoggerFactory::Error("No logging level found for option \"" + key + "\"")); |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 114 | } |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 115 | |
| 116 | return parseLevel(levelString); |
| 117 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 118 | |
| 119 | void |
| 120 | LoggerFactory::onConfig(const ConfigSection& section, |
| 121 | bool isDryRun, |
| 122 | const std::string& filename) |
| 123 | { |
Alexander Afanasyev | 6459e64 | 2016-09-12 04:07:33 +0000 | [diff] [blame^] | 124 | // log |
| 125 | // { |
| 126 | // ; default_level specifies the logging level for modules |
| 127 | // ; that are not explicitly named. All debugging levels |
| 128 | // ; listed above the selected value are enabled. |
| 129 | // |
| 130 | // default_level INFO |
| 131 | // |
| 132 | // ; You may also override the default for specific modules: |
| 133 | // |
| 134 | // FibManager DEBUG |
| 135 | // Forwarder WARN |
| 136 | // } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 137 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 138 | if (!isDryRun) { |
| 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); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 143 | } |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 144 | else { |
| 145 | setDefaultLevel(LOG_INFO); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 146 | } |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | for (const auto& i : section) { |
| 150 | LogLevel level = extractLevel(i.second, i.first); |
| 151 | |
| 152 | if (i.first == "default_level") { |
| 153 | // do nothing |
| 154 | } |
| 155 | else { |
| 156 | std::unique_lock<std::mutex> lock(m_loggersGuard); |
| 157 | LoggerMap::iterator loggerIt = m_loggers.find(i.first); |
| 158 | if (loggerIt == m_loggers.end()) { |
| 159 | lock.unlock(); |
| 160 | NFD_LOG_DEBUG("Failed to configure logging level for module \"" << |
| 161 | i.first << "\" (module not found)"); |
| 162 | } |
| 163 | else if (!isDryRun) { |
| 164 | loggerIt->second.setLogLevel(level); |
| 165 | lock.unlock(); |
| 166 | NFD_LOG_DEBUG("Changing level for module " << i.first << " to " << level); |
| 167 | } |
| 168 | } |
| 169 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
| 173 | LoggerFactory::setDefaultLevel(LogLevel level) |
| 174 | { |
| 175 | // std::cerr << "changing to default_level " << level << std::endl; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 176 | std::lock_guard<std::mutex> lock(m_loggersGuard); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 177 | |
| 178 | m_defaultLevel = level; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 179 | for (auto&& logger : m_loggers) { |
| 180 | // std::cerr << "changing " << i->first << " to default " << m_defaultLevel << std::endl; |
| 181 | logger.second.setLogLevel(m_defaultLevel); |
| 182 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 183 | } |
| 184 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 185 | void |
| 186 | LoggerFactory::flushBackend() |
| 187 | { |
Alexander Afanasyev | 6459e64 | 2016-09-12 04:07:33 +0000 | [diff] [blame^] | 188 | ndn::util::Logging::flush(); |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 189 | } |
| 190 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 191 | Logger& |
| 192 | LoggerFactory::create(const std::string& moduleName) |
| 193 | { |
| 194 | return LoggerFactory::getInstance().createLogger(moduleName); |
| 195 | } |
| 196 | |
| 197 | Logger& |
| 198 | LoggerFactory::createLogger(const std::string& moduleName) |
| 199 | { |
| 200 | // std::cerr << "creating logger for " << moduleName |
| 201 | // << " with level " << m_defaultLevel << std::endl; |
| 202 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 203 | std::lock_guard<std::mutex> lock(m_loggersGuard); |
| 204 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 205 | std::pair<LoggerMap::iterator, bool> loggerIt = |
| 206 | m_loggers.insert(NameAndLogger(moduleName, Logger(moduleName, m_defaultLevel))); |
| 207 | |
| 208 | return loggerIt.first->second; |
| 209 | } |
| 210 | |
| 211 | std::list<std::string> |
| 212 | LoggerFactory::getModules() const |
| 213 | { |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 214 | std::lock_guard<std::mutex> lock(m_loggersGuard); |
| 215 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 216 | std::list<std::string> modules; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 217 | for (const auto& loggerName : m_loggers | boost::adaptors::map_keys) { |
| 218 | modules.push_back(loggerName); |
| 219 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 220 | |
| 221 | return modules; |
| 222 | } |
| 223 | |
| 224 | } // namespace nfd |