Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 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 NDN_UTIL_LOGGING_HPP |
| 23 | #define NDN_UTIL_LOGGING_HPP |
| 24 | |
| 25 | #include "../common.hpp" |
| 26 | |
Alexander Afanasyev | ed1e99d | 2016-11-05 09:59:35 -0600 | [diff] [blame] | 27 | #ifdef HAVE_NDN_CXX_CUSTOM_LOGGER |
| 28 | #include "ndn-cxx-custom-logging.hpp" |
| 29 | #else |
| 30 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 31 | #include <boost/log/sinks.hpp> |
| 32 | #include <mutex> |
| 33 | #include <unordered_map> |
| 34 | |
| 35 | namespace ndn { |
| 36 | namespace util { |
| 37 | |
| 38 | enum class LogLevel; |
| 39 | class Logger; |
| 40 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 41 | /** \brief Controls the logging facility. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 42 | * |
| 43 | * \note Public static methods are thread safe. |
| 44 | * Non-public methods are not guaranteed to be thread safe. |
| 45 | */ |
| 46 | class Logging : noncopyable |
| 47 | { |
| 48 | public: |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 49 | /** \brief Get list of all registered logger names. |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 50 | */ |
| 51 | static std::set<std::string> |
| 52 | getLoggerNames(); |
| 53 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 54 | /** \brief Set severity level. |
| 55 | * \param prefix logger prefix; this can be a specific logger name, a general prefix like |
| 56 | * `"ndn.a.*"` to apply a setting for all modules that contain that prefix, |
| 57 | * or `"*"` for all modules |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 58 | * \param level minimum severity level |
| 59 | * |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 60 | * Log messages are output only if their severity is greater than the current minimum severity |
| 61 | * level. The initial severity level is \c LogLevel::NONE, which enables FATAL messages only. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 62 | */ |
| 63 | static void |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 64 | setLevel(const std::string& prefix, LogLevel level); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 65 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 66 | /** \brief Set severity levels with a config string. |
| 67 | * \param config colon-separated `key=value` pairs |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 68 | * \throw std::invalid_argument config string is malformed |
| 69 | * |
| 70 | * \code |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 71 | * Logging::setLevel("*=INFO:Face=DEBUG:NfdController=WARN"); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 72 | * \endcode |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 73 | * is equivalent to: |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 74 | * \code |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 75 | * Logging::setLevel("*", LogLevel::INFO); |
| 76 | * Logging::setLevel("Face", LogLevel::DEBUG); |
| 77 | * Logging::setLevel("NfdController", LogLevel::WARN); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 78 | * \endcode |
| 79 | */ |
| 80 | static void |
| 81 | setLevel(const std::string& config); |
| 82 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 83 | /** \brief Set log destination. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 84 | * \param os a stream for log output |
| 85 | * |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 86 | * The initial destination is `std::clog`. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 87 | */ |
| 88 | static void |
| 89 | setDestination(shared_ptr<std::ostream> os); |
| 90 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 91 | /** \brief Set log destination. |
| 92 | * \param os a stream for log output; caller must ensure it remains valid |
| 93 | * until setDestination() is invoked again or program exits |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 94 | * |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 95 | * This is equivalent to `setDestination(shared_ptr<std::ostream>(&os, nullDeleter))`. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 96 | */ |
| 97 | static void |
| 98 | setDestination(std::ostream& os); |
| 99 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 100 | /** \brief Flush log backend. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 101 | * |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 102 | * This ensures all log messages are written to the destination stream. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 103 | */ |
| 104 | static void |
| 105 | flush(); |
| 106 | |
| 107 | private: |
| 108 | Logging(); |
| 109 | |
| 110 | void |
| 111 | addLoggerImpl(Logger& logger); |
| 112 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 113 | void |
| 114 | registerLoggerNameImpl(std::string name); |
| 115 | |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 116 | std::set<std::string> |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 117 | getLoggerNamesImpl() const; |
| 118 | |
| 119 | /** |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 120 | * \brief Finds the appropriate LogLevel for a logger. |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 121 | * \param moduleName name of logger |
| 122 | * |
| 123 | * This searches m_enabledLevel map to determine which LogLevel is appropriate for |
| 124 | * the incoming logger. It looks for the most specific prefix and broadens its |
| 125 | * prefix scope if a setting is not found. For example, when an incoming logger |
| 126 | * name is "ndn.a.b", it will search for "ndn.a.b" first. If this prefix is not |
| 127 | * contained in m_enabledLevel, it will search for "ndn.a.*", then "ndn.*", and |
| 128 | * finally "*". It defaults to INITIAL_DEFAULT_LEVEL if a matching prefix is not |
| 129 | * found. |
| 130 | */ |
| 131 | LogLevel |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 132 | findLevel(std::string moduleName) const; |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 133 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 134 | void |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 135 | setLevelImpl(const std::string& prefix, LogLevel level); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 136 | |
| 137 | void |
| 138 | setLevelImpl(const std::string& config); |
| 139 | |
| 140 | void |
| 141 | setDestinationImpl(shared_ptr<std::ostream> os); |
| 142 | |
| 143 | void |
| 144 | flushImpl(); |
| 145 | |
| 146 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 147 | static Logging& |
| 148 | get(); |
| 149 | |
| 150 | #ifdef NDN_CXX_HAVE_TESTS |
| 151 | bool |
| 152 | removeLogger(Logger& logger); |
| 153 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 154 | void |
| 155 | resetLevels(); |
| 156 | |
| 157 | shared_ptr<std::ostream> |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 158 | getDestination() const; |
| 159 | |
| 160 | void |
| 161 | setLevelImpl(const std::unordered_map<std::string, LogLevel>& prefixRules); |
| 162 | |
| 163 | const std::unordered_map<std::string, LogLevel>& |
| 164 | getLevels() const; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 165 | #endif // NDN_CXX_HAVE_TESTS |
| 166 | |
| 167 | private: |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 168 | friend Logger; |
| 169 | |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 170 | mutable std::mutex m_mutex; |
| 171 | std::unordered_map<std::string, LogLevel> m_enabledLevel; ///< module prefix => minimum level |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 172 | std::unordered_multimap<std::string, Logger*> m_loggers; ///< module name => logger instance |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 173 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 174 | using Sink = boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 175 | boost::shared_ptr<Sink> m_sink; |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 176 | shared_ptr<std::ostream> m_destination; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 179 | inline std::set<std::string> |
| 180 | Logging::getLoggerNames() |
| 181 | { |
| 182 | return get().getLoggerNamesImpl(); |
| 183 | } |
| 184 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 185 | inline void |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 186 | Logging::setLevel(const std::string& prefix, LogLevel level) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 187 | { |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 188 | get().setLevelImpl(prefix, level); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | inline void |
| 192 | Logging::setLevel(const std::string& config) |
| 193 | { |
| 194 | get().setLevelImpl(config); |
| 195 | } |
| 196 | |
| 197 | inline void |
| 198 | Logging::setDestination(shared_ptr<std::ostream> os) |
| 199 | { |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 200 | get().setDestinationImpl(std::move(os)); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | inline void |
| 204 | Logging::flush() |
| 205 | { |
| 206 | get().flushImpl(); |
| 207 | } |
| 208 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 209 | } // namespace util |
| 210 | } // namespace ndn |
| 211 | |
Alexander Afanasyev | ed1e99d | 2016-11-05 09:59:35 -0600 | [diff] [blame] | 212 | #endif // HAVE_NDN_CXX_CUSTOM_LOGGER |
| 213 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 214 | #endif // NDN_UTIL_LOGGING_HPP |