Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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" |
| 27 | |
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; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | LoggerFactory::setConfigFile(ConfigFile& config) |
| 60 | { |
| 61 | config.addSectionHandler("log", bind(&LoggerFactory::onConfig, this, _1, _2, _3)); |
| 62 | } |
| 63 | |
| 64 | LogLevel |
| 65 | LoggerFactory::parseLevel(const std::string& level) |
| 66 | { |
| 67 | std::string upperLevel = level; |
| 68 | boost::to_upper(upperLevel); |
| 69 | |
| 70 | // std::cerr << "parsing level: " << upperLevel << std::endl;; |
| 71 | // std::cerr << "# levels: " << m_levelNames.size() << std::endl; |
| 72 | // std::cerr << m_levelNames.begin()->first << std::endl; |
| 73 | |
| 74 | LevelMap::const_iterator levelIt = m_levelNames.find(upperLevel); |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 75 | if (levelIt != m_levelNames.end()) { |
| 76 | return levelIt->second; |
| 77 | } |
| 78 | try { |
| 79 | uint32_t levelNo = boost::lexical_cast<uint32_t>(level); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 80 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 81 | if ((boost::lexical_cast<uint32_t>(LOG_NONE) <= levelNo && |
| 82 | levelNo <= boost::lexical_cast<uint32_t>(LOG_TRACE)) || |
| 83 | levelNo == LOG_ALL) { |
| 84 | return static_cast<LogLevel>(levelNo); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 85 | } |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 86 | } |
| 87 | catch (const boost::bad_lexical_cast& error) { |
| 88 | } |
| 89 | |
| 90 | throw LoggerFactory::Error("Unsupported logging level \"" + level + "\""); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 91 | } |
| 92 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 93 | LogLevel |
| 94 | LoggerFactory::extractLevel(const ConfigSection& item, const std::string& key) |
| 95 | { |
| 96 | std::string levelString; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 97 | try { |
| 98 | levelString = item.get_value<std::string>(); |
| 99 | } |
| 100 | catch (const boost::property_tree::ptree_error& error) { |
| 101 | } |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 102 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 103 | if (levelString.empty()) { |
| 104 | throw LoggerFactory::Error("No logging level found for option \"" + key + "\""); |
| 105 | } |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 106 | |
| 107 | return parseLevel(levelString); |
| 108 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 109 | |
| 110 | void |
| 111 | LoggerFactory::onConfig(const ConfigSection& section, |
| 112 | bool isDryRun, |
| 113 | const std::string& filename) |
| 114 | { |
| 115 | // log |
| 116 | // { |
| 117 | // ; default_level specifies the logging level for modules |
| 118 | // ; that are not explicitly named. All debugging levels |
| 119 | // ; listed above the selected value are enabled. |
| 120 | // |
| 121 | // default_level INFO |
| 122 | // |
| 123 | // ; You may also override the default for specific modules: |
| 124 | // |
| 125 | // FibManager DEBUG |
| 126 | // Forwarder WARN |
| 127 | // } |
| 128 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 129 | if (!isDryRun) { |
| 130 | ConfigSection::const_assoc_iterator item = section.find("default_level"); |
| 131 | if (item != section.not_found()) { |
| 132 | LogLevel level = extractLevel(item->second, "default_level"); |
| 133 | setDefaultLevel(level); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 134 | } |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 135 | else { |
| 136 | setDefaultLevel(LOG_INFO); |
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 | } |
| 139 | |
| 140 | for (const auto& i : section) { |
| 141 | LogLevel level = extractLevel(i.second, i.first); |
| 142 | |
| 143 | if (i.first == "default_level") { |
| 144 | // do nothing |
| 145 | } |
| 146 | else { |
| 147 | std::unique_lock<std::mutex> lock(m_loggersGuard); |
| 148 | LoggerMap::iterator loggerIt = m_loggers.find(i.first); |
| 149 | if (loggerIt == m_loggers.end()) { |
| 150 | lock.unlock(); |
| 151 | NFD_LOG_DEBUG("Failed to configure logging level for module \"" << |
| 152 | i.first << "\" (module not found)"); |
| 153 | } |
| 154 | else if (!isDryRun) { |
| 155 | loggerIt->second.setLogLevel(level); |
| 156 | lock.unlock(); |
| 157 | NFD_LOG_DEBUG("Changing level for module " << i.first << " to " << level); |
| 158 | } |
| 159 | } |
| 160 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
| 164 | LoggerFactory::setDefaultLevel(LogLevel level) |
| 165 | { |
| 166 | // std::cerr << "changing to default_level " << level << std::endl; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 167 | std::lock_guard<std::mutex> lock(m_loggersGuard); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 168 | |
| 169 | m_defaultLevel = level; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 170 | for (auto&& logger : m_loggers) { |
| 171 | // std::cerr << "changing " << i->first << " to default " << m_defaultLevel << std::endl; |
| 172 | logger.second.setLogLevel(m_defaultLevel); |
| 173 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | Logger& |
| 177 | LoggerFactory::create(const std::string& moduleName) |
| 178 | { |
| 179 | return LoggerFactory::getInstance().createLogger(moduleName); |
| 180 | } |
| 181 | |
| 182 | Logger& |
| 183 | LoggerFactory::createLogger(const std::string& moduleName) |
| 184 | { |
| 185 | // std::cerr << "creating logger for " << moduleName |
| 186 | // << " with level " << m_defaultLevel << std::endl; |
| 187 | |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 188 | std::lock_guard<std::mutex> lock(m_loggersGuard); |
| 189 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 190 | std::pair<LoggerMap::iterator, bool> loggerIt = |
| 191 | m_loggers.insert(NameAndLogger(moduleName, Logger(moduleName, m_defaultLevel))); |
| 192 | |
| 193 | return loggerIt.first->second; |
| 194 | } |
| 195 | |
| 196 | std::list<std::string> |
| 197 | LoggerFactory::getModules() const |
| 198 | { |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 199 | std::lock_guard<std::mutex> lock(m_loggersGuard); |
| 200 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 201 | std::list<std::string> modules; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame^] | 202 | for (const auto& loggerName : m_loggers | boost::adaptors::map_keys) { |
| 203 | modules.push_back(loggerName); |
| 204 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 205 | |
| 206 | return modules; |
| 207 | } |
| 208 | |
| 209 | } // namespace nfd |