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 | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
| 41 | /** \brief controls the logging facility |
| 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: |
| 49 | /** \brief register a new logger |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 50 | * \note App should declare a new logger with \p NDN_LOG_INIT macro. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 51 | */ |
| 52 | static void |
| 53 | addLogger(Logger& logger); |
| 54 | |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 55 | /** \brief get list of names of all registered loggers |
| 56 | */ |
| 57 | static std::set<std::string> |
| 58 | getLoggerNames(); |
| 59 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 60 | /** \brief set severity level |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 61 | * \param prefix logger prefix; this can be a specific logger name, a general prefix like ndn.a.* |
| 62 | * to apply a setting for all modules that contain this prefix, or "*" for all modules |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 63 | * \param level minimum severity level |
| 64 | * |
| 65 | * Log messages are output only if its severity is greater than the set minimum severity level. |
| 66 | * Initial default severity level is \p LogLevel::NONE which enables FATAL only. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 67 | */ |
| 68 | static void |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 69 | setLevel(const std::string& prefix, LogLevel level); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 70 | |
| 71 | /** \brief set severity levels with a config string |
| 72 | * \param config colon-separate key=value pairs |
| 73 | * \throw std::invalid_argument config string is malformed |
| 74 | * |
| 75 | * \code |
| 76 | * Logging::setSeverityLevels("*=INFO:Face=DEBUG:NfdController=WARN"); |
| 77 | * \endcode |
| 78 | * is equivalent to |
| 79 | * \code |
| 80 | * Logging::setSeverityLevel("*", LogLevel::INFO); |
| 81 | * Logging::setSeverityLevel("Face", LogLevel::DEBUG); |
| 82 | * Logging::setSeverityLevel("NfdController", LogLevel::WARN); |
| 83 | * \endcode |
| 84 | */ |
| 85 | static void |
| 86 | setLevel(const std::string& config); |
| 87 | |
| 88 | /** \brief set log destination |
| 89 | * \param os a stream for log output |
| 90 | * |
| 91 | * Initial destination is \p std::clog . |
| 92 | */ |
| 93 | static void |
| 94 | setDestination(shared_ptr<std::ostream> os); |
| 95 | |
| 96 | /** \brief set log destination |
| 97 | * \param os a stream for log output; caller must ensure this is valid |
| 98 | * until setDestination is invoked again or program exits |
| 99 | * |
| 100 | * This is equivalent to setDestination(shared_ptr<std::ostream>(&os, nullDeleter)) |
| 101 | */ |
| 102 | static void |
| 103 | setDestination(std::ostream& os); |
| 104 | |
| 105 | /** \brief flush log backend |
| 106 | * |
| 107 | * This ensures log messages are written to the destination stream. |
| 108 | */ |
| 109 | static void |
| 110 | flush(); |
| 111 | |
| 112 | private: |
| 113 | Logging(); |
| 114 | |
| 115 | void |
| 116 | addLoggerImpl(Logger& logger); |
| 117 | |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 118 | std::set<std::string> |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 119 | getLoggerNamesImpl() const; |
| 120 | |
| 121 | /** |
| 122 | * \brief finds the appropriate LogLevel for a logger |
| 123 | * \param moduleName name of logger |
| 124 | * |
| 125 | * This searches m_enabledLevel map to determine which LogLevel is appropriate for |
| 126 | * the incoming logger. It looks for the most specific prefix and broadens its |
| 127 | * prefix scope if a setting is not found. For example, when an incoming logger |
| 128 | * name is "ndn.a.b", it will search for "ndn.a.b" first. If this prefix is not |
| 129 | * contained in m_enabledLevel, it will search for "ndn.a.*", then "ndn.*", and |
| 130 | * finally "*". It defaults to INITIAL_DEFAULT_LEVEL if a matching prefix is not |
| 131 | * found. |
| 132 | */ |
| 133 | LogLevel |
| 134 | findLevel(const std::string& moduleName) const; |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 135 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 136 | void |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 137 | setLevelImpl(const std::string& prefix, LogLevel level); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 138 | |
| 139 | void |
| 140 | setLevelImpl(const std::string& config); |
| 141 | |
| 142 | void |
| 143 | setDestinationImpl(shared_ptr<std::ostream> os); |
| 144 | |
| 145 | void |
| 146 | flushImpl(); |
| 147 | |
| 148 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 149 | static Logging& |
| 150 | get(); |
| 151 | |
| 152 | #ifdef NDN_CXX_HAVE_TESTS |
| 153 | bool |
| 154 | removeLogger(Logger& logger); |
| 155 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 156 | void |
| 157 | resetLevels(); |
| 158 | |
| 159 | shared_ptr<std::ostream> |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 160 | getDestination() const; |
| 161 | |
| 162 | void |
| 163 | setLevelImpl(const std::unordered_map<std::string, LogLevel>& prefixRules); |
| 164 | |
| 165 | const std::unordered_map<std::string, LogLevel>& |
| 166 | getLevels() const; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 167 | #endif // NDN_CXX_HAVE_TESTS |
| 168 | |
| 169 | private: |
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 |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 172 | std::unordered_multimap<std::string, Logger*> m_loggers; ///< moduleName => logger |
| 173 | |
| 174 | shared_ptr<std::ostream> m_destination; |
| 175 | typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend> Sink; |
| 176 | boost::shared_ptr<Sink> m_sink; |
| 177 | }; |
| 178 | |
| 179 | inline void |
| 180 | Logging::addLogger(Logger& logger) |
| 181 | { |
| 182 | get().addLoggerImpl(logger); |
| 183 | } |
| 184 | |
Alexander Afanasyev | 354f382 | 2017-03-27 15:26:41 -0500 | [diff] [blame] | 185 | inline std::set<std::string> |
| 186 | Logging::getLoggerNames() |
| 187 | { |
| 188 | return get().getLoggerNamesImpl(); |
| 189 | } |
| 190 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 191 | inline void |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 192 | Logging::setLevel(const std::string& prefix, LogLevel level) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 193 | { |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 194 | get().setLevelImpl(prefix, level); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | inline void |
| 198 | Logging::setLevel(const std::string& config) |
| 199 | { |
| 200 | get().setLevelImpl(config); |
| 201 | } |
| 202 | |
| 203 | inline void |
| 204 | Logging::setDestination(shared_ptr<std::ostream> os) |
| 205 | { |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 206 | get().setDestinationImpl(std::move(os)); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | inline void |
| 210 | Logging::flush() |
| 211 | { |
| 212 | get().flushImpl(); |
| 213 | } |
| 214 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 215 | } // namespace util |
| 216 | } // namespace ndn |
| 217 | |
Alexander Afanasyev | ed1e99d | 2016-11-05 09:59:35 -0600 | [diff] [blame] | 218 | #endif // HAVE_NDN_CXX_CUSTOM_LOGGER |
| 219 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 220 | #endif // NDN_UTIL_LOGGING_HPP |