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 | /* |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 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 | |
Junxiao Shi | d1fc9a7 | 2018-12-12 16:35:34 +0000 | [diff] [blame] | 25 | #include "ndn-cxx/detail/common.hpp" |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 26 | |
Alexander Afanasyev | ed1e99d | 2016-11-05 09:59:35 -0600 | [diff] [blame] | 27 | #ifdef HAVE_NDN_CXX_CUSTOM_LOGGER |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 28 | #include "ndn-cxx/util/custom-logging.hpp" |
Alexander Afanasyev | ed1e99d | 2016-11-05 09:59:35 -0600 | [diff] [blame] | 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 | |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 83 | /** \brief Set or replace log destination. |
| 84 | * \param destination log backend, e.g., returned by `makeDefaultStreamDestination` |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 85 | * |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 86 | * The initial destination is `std::clog`. |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 87 | * |
| 88 | * Note that if \p destination is nullptr, the destination will be removed and the |
| 89 | * application is expected to add its own. If the application does not set a custom |
| 90 | * destination (using this function or directly using Boost.Log routines), the default |
| 91 | * Boost.Log destination will be used. Refer to Boost.Log documentation and source code |
| 92 | * for details. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 93 | */ |
| 94 | static void |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 95 | setDestination(boost::shared_ptr<boost::log::sinks::sink> destination); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 96 | |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 97 | /** \brief Helper method to set stream log destination. |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 98 | * \param os a stream for log output; caller must ensure it remains valid |
| 99 | * until setDestination() is invoked again or program exits |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 100 | *` |
| 101 | * This is equivalent to `setDestination(makeDefaultStreamDestination(shared_ptr<std::ostream>(&os, nullDeleter)))`. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 102 | * |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 103 | */ |
| 104 | static void |
| 105 | setDestination(std::ostream& os); |
| 106 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 107 | /** \brief Flush log backend. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 108 | * |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 109 | * This ensures all log messages are written to the destination stream. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 110 | */ |
| 111 | static void |
| 112 | flush(); |
| 113 | |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 114 | /** \brief Create stream log destination using default formatting |
| 115 | */ |
| 116 | static boost::shared_ptr<boost::log::sinks::sink> |
| 117 | makeDefaultStreamDestination(shared_ptr<std::ostream> os); |
| 118 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 119 | private: |
| 120 | Logging(); |
| 121 | |
| 122 | void |
| 123 | addLoggerImpl(Logger& logger); |
| 124 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 125 | void |
| 126 | registerLoggerNameImpl(std::string name); |
| 127 | |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 128 | std::set<std::string> |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 129 | getLoggerNamesImpl() const; |
| 130 | |
| 131 | /** |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 132 | * \brief Finds the appropriate LogLevel for a logger. |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 133 | * \param moduleName name of logger |
| 134 | * |
| 135 | * This searches m_enabledLevel map to determine which LogLevel is appropriate for |
| 136 | * the incoming logger. It looks for the most specific prefix and broadens its |
| 137 | * prefix scope if a setting is not found. For example, when an incoming logger |
| 138 | * name is "ndn.a.b", it will search for "ndn.a.b" first. If this prefix is not |
| 139 | * contained in m_enabledLevel, it will search for "ndn.a.*", then "ndn.*", and |
| 140 | * finally "*". It defaults to INITIAL_DEFAULT_LEVEL if a matching prefix is not |
| 141 | * found. |
| 142 | */ |
| 143 | LogLevel |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 144 | findLevel(std::string moduleName) const; |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 145 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 146 | void |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 147 | setLevelImpl(const std::string& prefix, LogLevel level); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 148 | |
| 149 | void |
| 150 | setLevelImpl(const std::string& config); |
| 151 | |
| 152 | void |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 153 | setDestinationImpl(boost::shared_ptr<boost::log::sinks::sink> sink); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 154 | |
| 155 | void |
| 156 | flushImpl(); |
| 157 | |
| 158 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 159 | static Logging& |
| 160 | get(); |
| 161 | |
| 162 | #ifdef NDN_CXX_HAVE_TESTS |
| 163 | bool |
| 164 | removeLogger(Logger& logger); |
| 165 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 166 | void |
| 167 | resetLevels(); |
| 168 | |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 169 | boost::shared_ptr<boost::log::sinks::sink> |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 170 | getDestination() const; |
| 171 | |
| 172 | void |
| 173 | setLevelImpl(const std::unordered_map<std::string, LogLevel>& prefixRules); |
| 174 | |
| 175 | const std::unordered_map<std::string, LogLevel>& |
| 176 | getLevels() const; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 177 | #endif // NDN_CXX_HAVE_TESTS |
| 178 | |
| 179 | private: |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 180 | friend Logger; |
| 181 | |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 182 | mutable std::mutex m_mutex; |
| 183 | std::unordered_map<std::string, LogLevel> m_enabledLevel; ///< module prefix => minimum level |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 184 | std::unordered_multimap<std::string, Logger*> m_loggers; ///< module name => logger instance |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 185 | |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 186 | boost::shared_ptr<boost::log::sinks::sink> m_destination; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 189 | inline std::set<std::string> |
| 190 | Logging::getLoggerNames() |
| 191 | { |
| 192 | return get().getLoggerNamesImpl(); |
| 193 | } |
| 194 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 195 | inline void |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 196 | Logging::setLevel(const std::string& prefix, LogLevel level) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 197 | { |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 198 | get().setLevelImpl(prefix, level); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | inline void |
| 202 | Logging::setLevel(const std::string& config) |
| 203 | { |
| 204 | get().setLevelImpl(config); |
| 205 | } |
| 206 | |
| 207 | inline void |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 208 | Logging::setDestination(boost::shared_ptr<boost::log::sinks::sink> destination) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 209 | { |
Alexander Afanasyev | 7b2f58c | 2019-07-13 11:50:19 -0400 | [diff] [blame] | 210 | get().setDestinationImpl(std::move(destination)); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | inline void |
| 214 | Logging::flush() |
| 215 | { |
| 216 | get().flushImpl(); |
| 217 | } |
| 218 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 219 | } // namespace util |
| 220 | } // namespace ndn |
| 221 | |
Alexander Afanasyev | ed1e99d | 2016-11-05 09:59:35 -0600 | [diff] [blame] | 222 | #endif // HAVE_NDN_CXX_CUSTOM_LOGGER |
| 223 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 224 | #endif // NDN_UTIL_LOGGING_HPP |