Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 24 | |
| 25 | #include "logger-factory.hpp" |
| 26 | |
| 27 | namespace nfd { |
| 28 | |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame^] | 29 | NFD_LOG_INIT("LoggerFactory"); |
| 30 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 31 | LoggerFactory& |
| 32 | LoggerFactory::getInstance() |
| 33 | { |
| 34 | static LoggerFactory globalLoggerFactory; |
| 35 | |
| 36 | return globalLoggerFactory; |
| 37 | } |
| 38 | |
| 39 | LoggerFactory::LoggerFactory() |
| 40 | : m_defaultLevel(LOG_INFO) |
| 41 | { |
| 42 | m_levelNames["NONE"] = LOG_NONE; |
| 43 | m_levelNames["ERROR"] = LOG_ERROR; |
| 44 | m_levelNames["WARN"] = LOG_WARN; |
| 45 | m_levelNames["INFO"] = LOG_INFO; |
| 46 | m_levelNames["DEBUG"] = LOG_DEBUG; |
| 47 | m_levelNames["TRACE"] = LOG_TRACE; |
| 48 | m_levelNames["ALL"] = LOG_ALL; |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | LoggerFactory::setConfigFile(ConfigFile& config) |
| 53 | { |
| 54 | config.addSectionHandler("log", bind(&LoggerFactory::onConfig, this, _1, _2, _3)); |
| 55 | } |
| 56 | |
| 57 | LogLevel |
| 58 | LoggerFactory::parseLevel(const std::string& level) |
| 59 | { |
| 60 | std::string upperLevel = level; |
| 61 | boost::to_upper(upperLevel); |
| 62 | |
| 63 | // std::cerr << "parsing level: " << upperLevel << std::endl;; |
| 64 | // std::cerr << "# levels: " << m_levelNames.size() << std::endl; |
| 65 | // std::cerr << m_levelNames.begin()->first << std::endl; |
| 66 | |
| 67 | LevelMap::const_iterator levelIt = m_levelNames.find(upperLevel); |
| 68 | if (levelIt != m_levelNames.end()) |
| 69 | { |
| 70 | return levelIt->second; |
| 71 | } |
| 72 | try |
| 73 | { |
| 74 | uint32_t levelNo = boost::lexical_cast<uint32_t>(level); |
| 75 | |
| 76 | if ((LOG_NONE <= levelNo && levelNo <= LOG_TRACE) || |
| 77 | levelNo == LOG_ALL) |
| 78 | { |
| 79 | return static_cast<LogLevel>(levelNo); |
| 80 | } |
| 81 | } |
| 82 | catch (const boost::bad_lexical_cast& error) |
| 83 | { |
| 84 | } |
| 85 | throw LoggerFactory::Error("Unsupported logging level \"" + |
| 86 | level + "\""); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void |
| 91 | LoggerFactory::onConfig(const ConfigSection& section, |
| 92 | bool isDryRun, |
| 93 | const std::string& filename) |
| 94 | { |
| 95 | // log |
| 96 | // { |
| 97 | // ; default_level specifies the logging level for modules |
| 98 | // ; that are not explicitly named. All debugging levels |
| 99 | // ; listed above the selected value are enabled. |
| 100 | // |
| 101 | // default_level INFO |
| 102 | // |
| 103 | // ; You may also override the default for specific modules: |
| 104 | // |
| 105 | // FibManager DEBUG |
| 106 | // Forwarder WARN |
| 107 | // } |
| 108 | |
| 109 | // std::cerr << "loading logging configuration" << std::endl; |
| 110 | for (ConfigSection::const_iterator item = section.begin(); |
| 111 | item != section.end(); |
| 112 | ++item) |
| 113 | { |
| 114 | std::string levelString; |
| 115 | try |
| 116 | { |
| 117 | levelString = item->second.get_value<std::string>(); |
| 118 | } |
| 119 | catch (const boost::property_tree::ptree_error& error) |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | if (levelString.empty()) |
| 124 | { |
| 125 | throw LoggerFactory::Error("No logging level found for option \"" + item->first + "\""); |
| 126 | } |
| 127 | |
| 128 | LogLevel level = parseLevel(levelString); |
| 129 | |
| 130 | if (item->first == "default_level") |
| 131 | { |
| 132 | if (!isDryRun) |
| 133 | { |
| 134 | setDefaultLevel(level); |
| 135 | } |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | LoggerMap::iterator loggerIt = m_loggers.find(item->first); |
| 140 | if (loggerIt == m_loggers.end()) |
| 141 | { |
Steve DiBenedetto | 6ad48ca | 2014-04-22 09:39:31 -0600 | [diff] [blame^] | 142 | NFD_LOG_DEBUG("Failed to configure logging level for module \"" << |
| 143 | item->first << "\" (module not found)"); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if (!isDryRun) |
| 147 | { |
| 148 | // std::cerr << "changing level for module " << item->first << " to " << level << std::endl; |
| 149 | loggerIt->second.setLogLevel(level); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | LoggerFactory::setDefaultLevel(LogLevel level) |
| 157 | { |
| 158 | // std::cerr << "changing to default_level " << level << std::endl; |
| 159 | |
| 160 | m_defaultLevel = level; |
| 161 | for (LoggerMap::iterator i = m_loggers.begin(); i != m_loggers.end(); ++i) |
| 162 | { |
| 163 | // std::cerr << "changing " << i->first << " to default " << m_defaultLevel << std::endl; |
| 164 | i->second.setLogLevel(m_defaultLevel); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | Logger& |
| 169 | LoggerFactory::create(const std::string& moduleName) |
| 170 | { |
| 171 | return LoggerFactory::getInstance().createLogger(moduleName); |
| 172 | } |
| 173 | |
| 174 | Logger& |
| 175 | LoggerFactory::createLogger(const std::string& moduleName) |
| 176 | { |
| 177 | // std::cerr << "creating logger for " << moduleName |
| 178 | // << " with level " << m_defaultLevel << std::endl; |
| 179 | |
| 180 | std::pair<LoggerMap::iterator, bool> loggerIt = |
| 181 | m_loggers.insert(NameAndLogger(moduleName, Logger(moduleName, m_defaultLevel))); |
| 182 | |
| 183 | return loggerIt.first->second; |
| 184 | } |
| 185 | |
| 186 | std::list<std::string> |
| 187 | LoggerFactory::getModules() const |
| 188 | { |
| 189 | std::list<std::string> modules; |
| 190 | for (LoggerMap::const_iterator i = m_loggers.begin(); i != m_loggers.end(); ++i) |
| 191 | { |
| 192 | modules.push_back(i->first); |
| 193 | } |
| 194 | |
| 195 | return modules; |
| 196 | } |
| 197 | |
| 198 | } // namespace nfd |