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 | #ifndef NFD_ANDROID_NDN_CXX_ANDROID_NDN_CXX_CUSTOM_LOGGING_HPP |
| 23 | #define NFD_ANDROID_NDN_CXX_ANDROID_NDN_CXX_CUSTOM_LOGGING_HPP |
| 24 | |
| 25 | #include "common.hpp" |
| 26 | |
| 27 | #include <mutex> |
| 28 | #include <unordered_map> |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace util { |
| 32 | |
| 33 | enum class LogLevel; |
| 34 | class Logger; |
| 35 | |
| 36 | /** \brief controls the logging facility |
| 37 | * |
| 38 | * \note Public static methods are thread safe. |
| 39 | * Non-public methods are not guaranteed to be thread safe. |
| 40 | */ |
| 41 | class Logging : noncopyable |
| 42 | { |
| 43 | public: |
| 44 | /** \brief register a new logger |
| 45 | * \note App should declare a new logger with \p NDN_LOG_INIT macro. |
| 46 | */ |
| 47 | static void |
| 48 | addLogger(Logger& logger); |
| 49 | |
| 50 | /** \brief set severity level |
| 51 | * \param moduleName logger name, or "*" for default level |
| 52 | * \param level minimum severity level |
| 53 | * |
| 54 | * Log messages are output only if its severity is greater than the set minimum severity level. |
| 55 | * Initial default severity level is \p LogLevel::NONE which enables FATAL only. |
| 56 | * |
| 57 | * Changing the default level overwrites individual settings. |
| 58 | */ |
| 59 | static void |
| 60 | setLevel(const std::string& moduleName, LogLevel level); |
| 61 | |
| 62 | /** \brief set severity levels with a config string |
| 63 | * \param config colon-separate key=value pairs |
| 64 | * \throw std::invalid_argument config string is malformed |
| 65 | * |
| 66 | * \code |
| 67 | * Logging::setSeverityLevels("*=INFO:Face=DEBUG:NfdController=WARN"); |
| 68 | * \endcode |
| 69 | * is equivalent to |
| 70 | * \code |
| 71 | * Logging::setSeverityLevel("*", LogLevel::INFO); |
| 72 | * Logging::setSeverityLevel("Face", LogLevel::DEBUG); |
| 73 | * Logging::setSeverityLevel("NfdController", LogLevel::WARN); |
| 74 | * \endcode |
| 75 | */ |
| 76 | static void |
| 77 | setLevel(const std::string& config); |
| 78 | |
| 79 | private: |
| 80 | Logging(); |
| 81 | |
| 82 | void |
| 83 | addLoggerImpl(Logger& logger); |
| 84 | |
| 85 | void |
| 86 | setLevelImpl(const std::string& moduleName, LogLevel level); |
| 87 | |
| 88 | void |
| 89 | setDefaultLevel(LogLevel level); |
| 90 | |
| 91 | void |
| 92 | setLevelImpl(const std::string& config); |
| 93 | |
| 94 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 95 | static Logging& |
| 96 | get(); |
| 97 | |
| 98 | private: |
| 99 | std::mutex m_mutex; |
| 100 | std::unordered_map<std::string, LogLevel> m_enabledLevel; ///< moduleName => minimum level |
| 101 | std::unordered_multimap<std::string, Logger*> m_loggers; ///< moduleName => logger |
| 102 | }; |
| 103 | |
| 104 | inline void |
| 105 | Logging::addLogger(Logger& logger) |
| 106 | { |
| 107 | get().addLoggerImpl(logger); |
| 108 | } |
| 109 | |
| 110 | inline void |
| 111 | Logging::setLevel(const std::string& moduleName, LogLevel level) |
| 112 | { |
| 113 | get().setLevelImpl(moduleName, level); |
| 114 | } |
| 115 | |
| 116 | inline void |
| 117 | Logging::setLevel(const std::string& config) |
| 118 | { |
| 119 | get().setLevelImpl(config); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | } // namespace util |
| 124 | } // namespace ndn |
| 125 | |
| 126 | #endif // NFD_ANDROID_NDN_CXX_ANDROID_NDN_CXX_CUSTOM_LOGGING_HPP |