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 | 0c145ec | 2018-04-10 21:33:57 -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_LOGGER_HPP |
| 23 | #define NDN_UTIL_LOGGER_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-logger.hpp" |
| 29 | #else |
| 30 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 31 | #include <boost/log/common.hpp> |
| 32 | #include <boost/log/sources/logger.hpp> |
| 33 | #include <atomic> |
| 34 | |
| 35 | namespace ndn { |
| 36 | namespace util { |
| 37 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 38 | /** \brief Indicates the severity level of a log message. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 39 | */ |
| 40 | enum class LogLevel { |
| 41 | FATAL = -1, ///< fatal (will be logged unconditionally) |
| 42 | NONE = 0, ///< no messages |
| 43 | ERROR = 1, ///< serious error messages |
| 44 | WARN = 2, ///< warning messages |
| 45 | INFO = 3, ///< informational messages |
| 46 | DEBUG = 4, ///< debug messages |
| 47 | TRACE = 5, ///< trace messages (most verbose) |
| 48 | ALL = 255 ///< all messages |
| 49 | }; |
| 50 | |
Davide Pesavento | d95274b | 2018-04-12 17:42:20 -0400 | [diff] [blame^] | 51 | /** \brief Output LogLevel as a string. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 52 | * \throw std::invalid_argument unknown \p level |
| 53 | */ |
| 54 | std::ostream& |
| 55 | operator<<(std::ostream& os, LogLevel level); |
| 56 | |
Davide Pesavento | d95274b | 2018-04-12 17:42:20 -0400 | [diff] [blame^] | 57 | /** \brief Parse LogLevel from a string. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 58 | * \throw std::invalid_argument unknown level name |
| 59 | */ |
| 60 | LogLevel |
| 61 | parseLogLevel(const std::string& s); |
| 62 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 63 | /** \brief Represents a log module in the logging facility. |
| 64 | * \note New loggers should be defined using #NDN_LOG_INIT or #NDN_LOG_MEMBER_INIT. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 65 | */ |
| 66 | class Logger : public boost::log::sources::logger_mt |
| 67 | { |
| 68 | public: |
| 69 | explicit |
| 70 | Logger(const std::string& name); |
| 71 | |
| 72 | const std::string& |
| 73 | getModuleName() const |
| 74 | { |
| 75 | return m_moduleName; |
| 76 | } |
| 77 | |
| 78 | bool |
| 79 | isLevelEnabled(LogLevel level) const |
| 80 | { |
| 81 | return m_currentLevel.load(std::memory_order_relaxed) >= level; |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | setLevel(LogLevel level) |
| 86 | { |
| 87 | m_currentLevel.store(level, std::memory_order_relaxed); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | const std::string m_moduleName; |
| 92 | std::atomic<LogLevel> m_currentLevel; |
| 93 | }; |
| 94 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 95 | namespace detail { |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 96 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 97 | /** \brief A tag type used to output a timestamp to a stream. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 98 | * \code |
| 99 | * std::clog << LoggerTimestamp(); |
| 100 | * \endcode |
| 101 | */ |
| 102 | struct LoggerTimestamp |
| 103 | { |
| 104 | }; |
| 105 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 106 | /** \brief Write a timestamp to \p os. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 107 | * \note This function is thread-safe. |
| 108 | */ |
| 109 | std::ostream& |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 110 | operator<<(std::ostream& os, LoggerTimestamp); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 111 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 112 | /** \cond */ |
| 113 | template<class T> |
| 114 | struct ExtractArgument; |
| 115 | |
| 116 | template<class T, class U> |
| 117 | struct ExtractArgument<T(U)> |
| 118 | { |
| 119 | using type = U; |
| 120 | }; |
| 121 | |
| 122 | template<class T> |
| 123 | using ArgumentType = typename ExtractArgument<T>::type; |
| 124 | /** \endcond */ |
| 125 | |
| 126 | } // namespace detail |
| 127 | |
| 128 | /** \cond */ |
| 129 | #define NDN_LOG_INIT_FUNCTION_BODY(name) \ |
| 130 | { \ |
| 131 | static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \ |
| 132 | return logger; \ |
| 133 | } |
| 134 | /** \endcond */ |
| 135 | |
| 136 | /** \brief Define a non-member log module. |
| 137 | * |
| 138 | * This macro can be used in global scope to define a log module for an entire translation |
| 139 | * unit, or in namespace scope to define a log module for the enclosing namespace. |
| 140 | * Use #NDN_LOG_MEMBER_INIT to define a log module as a class or struct member. |
| 141 | * |
| 142 | * \warning Do not use this macro in header files unless you know what you're doing, |
| 143 | * as it can easily trigger ODR violations if used incorrectly. |
| 144 | * |
| 145 | * \param name the logger name |
| 146 | * \note The logger name is restricted to alphanumeric characters and a select set of |
| 147 | * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with |
| 148 | * a dot (`.`), or contain multiple consecutive dots. |
| 149 | */ |
| 150 | #define NDN_LOG_INIT(name) \ |
| 151 | namespace { \ |
| 152 | ::ndn::util::Logger& ndn_cxx_getLogger() \ |
| 153 | NDN_LOG_INIT_FUNCTION_BODY(name) \ |
| 154 | } \ |
| 155 | struct ndn_cxx_allow_trailing_semicolon |
| 156 | |
| 157 | /** \brief Define a member log module. |
| 158 | * |
| 159 | * This macro should only be used to define a log module as a class or struct member. |
Davide Pesavento | d95274b | 2018-04-12 17:42:20 -0400 | [diff] [blame^] | 160 | * It is recommended to place this macro in the private or protected section of the |
| 161 | * class or struct definition. Use #NDN_LOG_INIT to define a non-member log module. |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 162 | * |
| 163 | * \param name the logger name |
| 164 | * \note The logger name is restricted to alphanumeric characters and a select set of |
| 165 | * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with |
| 166 | * a dot (`.`), or contain multiple consecutive dots. |
| 167 | */ |
| 168 | #define NDN_LOG_MEMBER_INIT(name) \ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 169 | static ::ndn::util::Logger& ndn_cxx_getLogger() \ |
| 170 | NDN_LOG_INIT_FUNCTION_BODY(name) \ |
| 171 | struct ndn_cxx_allow_trailing_semicolon |
| 172 | |
| 173 | /** \brief Forward-declare a member log module, without fully defining it. |
| 174 | * |
| 175 | * This macro can be used to declare a log module as a member of a class template. |
| 176 | * Use this macro in conjunction with #NDN_LOG_MEMBER_DECL_SPECIALIZED and |
| 177 | * #NDN_LOG_MEMBER_INIT_SPECIALIZED to provide different loggers for different |
| 178 | * template specializations. |
| 179 | */ |
| 180 | #define NDN_LOG_MEMBER_DECL() \ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 181 | static ::ndn::util::Logger& ndn_cxx_getLogger() |
| 182 | |
| 183 | /** \brief Declare an explicit specialization of a member log module of a class template. |
| 184 | * |
| 185 | * \param cls fully specialized class name; wrap in parentheses if it contains commas |
| 186 | */ |
| 187 | #define NDN_LOG_MEMBER_DECL_SPECIALIZED(cls) \ |
Davide Pesavento | d95274b | 2018-04-12 17:42:20 -0400 | [diff] [blame^] | 188 | template<> \ |
| 189 | ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger() |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 190 | |
| 191 | /** \brief Define an explicit specialization of a member log module of a class template. |
| 192 | * |
| 193 | * \param cls fully specialized class name; wrap in parentheses if it contains commas |
| 194 | * \param name the logger name |
| 195 | * \note The logger name is restricted to alphanumeric characters and a select set of |
| 196 | * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with |
| 197 | * a dot (`.`), or contain multiple consecutive dots. |
| 198 | */ |
| 199 | #define NDN_LOG_MEMBER_INIT_SPECIALIZED(cls, name) \ |
Davide Pesavento | d95274b | 2018-04-12 17:42:20 -0400 | [diff] [blame^] | 200 | template<> inline \ |
| 201 | ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger() \ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 202 | NDN_LOG_INIT_FUNCTION_BODY(name) \ |
| 203 | struct ndn_cxx_allow_trailing_semicolon |
| 204 | |
| 205 | /** \cond */ |
| 206 | #if BOOST_VERSION == 105900 |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 207 | // workaround Boost bug 11549 |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 208 | #define NDN_BOOST_LOG(x) BOOST_LOG(x) << "" |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 209 | #else |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 210 | #define NDN_BOOST_LOG(x) BOOST_LOG(x) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 211 | #endif |
| 212 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 213 | #define NDN_LOG_INTERNAL(lvl, lvlstr, expression) \ |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 214 | do { \ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 215 | if (ndn_cxx_getLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \ |
| 216 | NDN_BOOST_LOG(ndn_cxx_getLogger()) << ::ndn::util::detail::LoggerTimestamp{} \ |
| 217 | << " " BOOST_STRINGIZE(lvlstr) ": [" << ndn_cxx_getLogger().getModuleName() << "] " \ |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 218 | << expression; \ |
| 219 | } \ |
| 220 | } while (false) |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 221 | /** \endcond */ |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 222 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 223 | /** \brief Log at TRACE level. |
| 224 | * \pre A log module must be declared in the same translation unit, class, struct, or namespace. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 225 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 226 | #define NDN_LOG_TRACE(expression) NDN_LOG_INTERNAL(TRACE, TRACE, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 227 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 228 | /** \brief Log at DEBUG level. |
| 229 | * \pre A log module must be declared in the same translation unit, class, struct, or namespace. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 230 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 231 | #define NDN_LOG_DEBUG(expression) NDN_LOG_INTERNAL(DEBUG, DEBUG, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 232 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 233 | /** \brief Log at INFO level. |
| 234 | * \pre A log module must be declared in the same translation unit, class, struct, or namespace. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 235 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 236 | #define NDN_LOG_INFO(expression) NDN_LOG_INTERNAL(INFO, INFO, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 237 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 238 | /** \brief Log at WARN level. |
| 239 | * \pre A log module must be declared in the same translation unit, class, struct, or namespace. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 240 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 241 | #define NDN_LOG_WARN(expression) NDN_LOG_INTERNAL(WARN, WARNING, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 242 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 243 | /** \brief Log at ERROR level. |
| 244 | * \pre A log module must be declared in the same translation unit, class, struct, or namespace. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 245 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 246 | #define NDN_LOG_ERROR(expression) NDN_LOG_INTERNAL(ERROR, ERROR, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 247 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 248 | /** \brief Log at FATAL level. |
| 249 | * \pre A log module must be declared in the same translation unit, class, struct, or namespace. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 250 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 251 | #define NDN_LOG_FATAL(expression) NDN_LOG_INTERNAL(FATAL, FATAL, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 252 | |
| 253 | } // namespace util |
| 254 | } // namespace ndn |
| 255 | |
Alexander Afanasyev | ed1e99d | 2016-11-05 09:59:35 -0600 | [diff] [blame] | 256 | #endif // HAVE_NDN_CXX_CUSTOM_LOGGER |
| 257 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 258 | #endif // NDN_UTIL_LOGGER_HPP |