Alexander Afanasyev | a8d404b | 2016-11-05 10:07:08 -0600 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "ndn-cxx-custom-logging.hpp" |
| 23 | #include "ndn-cxx-custom-logger.hpp" |
| 24 | |
| 25 | #include <cstdlib> |
| 26 | #include <sstream> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace util { |
| 30 | |
| 31 | static const LogLevel INITIAL_DEFAULT_LEVEL = LogLevel::NONE; |
| 32 | |
| 33 | Logging& |
| 34 | Logging::get() |
| 35 | { |
| 36 | // Initialization of block-scope variables with static storage duration is thread-safe. |
| 37 | // See ISO C++ standard [stmt.dcl]/4 |
| 38 | static Logging instance; |
| 39 | return instance; |
| 40 | } |
| 41 | |
| 42 | Logging::Logging() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | Logging::addLoggerImpl(Logger& logger) |
| 48 | { |
| 49 | std::lock_guard<std::mutex> lock(m_mutex); |
| 50 | |
| 51 | const std::string& moduleName = logger.getModuleName(); |
| 52 | m_loggers.insert({moduleName, &logger}); |
| 53 | |
| 54 | auto levelIt = m_enabledLevel.find(moduleName); |
| 55 | if (levelIt == m_enabledLevel.end()) { |
| 56 | levelIt = m_enabledLevel.find("*"); |
| 57 | } |
| 58 | LogLevel level = levelIt == m_enabledLevel.end() ? INITIAL_DEFAULT_LEVEL : levelIt->second; |
| 59 | logger.setLevel(level); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | Logging::setLevelImpl(const std::string& moduleName, LogLevel level) |
| 64 | { |
| 65 | std::lock_guard<std::mutex> lock(m_mutex); |
| 66 | |
| 67 | if (moduleName == "*") { |
| 68 | this->setDefaultLevel(level); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | m_enabledLevel[moduleName] = level; |
| 73 | auto range = m_loggers.equal_range(moduleName); |
| 74 | for (auto i = range.first; i != range.second; ++i) { |
| 75 | i->second->setLevel(level); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | Logging::setDefaultLevel(LogLevel level) |
| 81 | { |
| 82 | m_enabledLevel.clear(); |
| 83 | m_enabledLevel["*"] = level; |
| 84 | |
| 85 | for (auto i = m_loggers.begin(); i != m_loggers.end(); ++i) { |
| 86 | i->second->setLevel(level); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | Logging::setLevelImpl(const std::string& config) |
| 92 | { |
| 93 | std::stringstream ss(config); |
| 94 | std::string configModule; |
| 95 | while (std::getline(ss, configModule, ':')) { |
| 96 | size_t ind = configModule.find('='); |
| 97 | if (ind == std::string::npos) { |
| 98 | BOOST_THROW_EXCEPTION(std::invalid_argument("malformed logging config: '=' is missing")); |
| 99 | } |
| 100 | |
| 101 | std::string moduleName = configModule.substr(0, ind); |
| 102 | LogLevel level = parseLogLevel(configModule.substr(ind+1)); |
| 103 | |
| 104 | this->setLevelImpl(moduleName, level); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | } // namespace util |
| 109 | } // namespace ndn |