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. |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 64 | * |
| 65 | * \note Normally, loggers should be defined using #NDN_LOG_INIT, #NDN_LOG_MEMBER_INIT, |
| 66 | * or #NDN_LOG_MEMBER_INIT_SPECIALIZED. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 67 | */ |
| 68 | class Logger : public boost::log::sources::logger_mt |
| 69 | { |
| 70 | public: |
| 71 | explicit |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 72 | Logger(const char* name); |
| 73 | |
| 74 | static void |
| 75 | registerModuleName(const char* name); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 76 | |
| 77 | const std::string& |
| 78 | getModuleName() const |
| 79 | { |
| 80 | return m_moduleName; |
| 81 | } |
| 82 | |
| 83 | bool |
| 84 | isLevelEnabled(LogLevel level) const |
| 85 | { |
| 86 | return m_currentLevel.load(std::memory_order_relaxed) >= level; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | setLevel(LogLevel level) |
| 91 | { |
| 92 | m_currentLevel.store(level, std::memory_order_relaxed); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | const std::string m_moduleName; |
| 97 | std::atomic<LogLevel> m_currentLevel; |
| 98 | }; |
| 99 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 100 | namespace detail { |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 101 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 102 | /** \brief A tag type used to output a timestamp to a stream. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 103 | * \code |
| 104 | * std::clog << LoggerTimestamp(); |
| 105 | * \endcode |
| 106 | */ |
| 107 | struct LoggerTimestamp |
| 108 | { |
| 109 | }; |
| 110 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 111 | /** \brief Write a timestamp to \p os. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 112 | * \note This function is thread-safe. |
| 113 | */ |
| 114 | std::ostream& |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 115 | operator<<(std::ostream& os, LoggerTimestamp); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 116 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 117 | /** \cond */ |
| 118 | template<class T> |
| 119 | struct ExtractArgument; |
| 120 | |
| 121 | template<class T, class U> |
| 122 | struct ExtractArgument<T(U)> |
| 123 | { |
| 124 | using type = U; |
| 125 | }; |
| 126 | |
| 127 | template<class T> |
| 128 | using ArgumentType = typename ExtractArgument<T>::type; |
| 129 | /** \endcond */ |
| 130 | |
| 131 | } // namespace detail |
| 132 | |
| 133 | /** \cond */ |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 134 | // implementation detail |
| 135 | #define NDN_LOG_REGISTER_NAME(name) \ |
| 136 | []() -> bool { \ |
| 137 | ::ndn::util::Logger::registerModuleName(BOOST_STRINGIZE(name)); \ |
| 138 | return true; \ |
| 139 | }() |
| 140 | |
| 141 | // implementation detail |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 142 | #define NDN_LOG_INIT_FUNCTION_BODY(name) \ |
| 143 | { \ |
| 144 | static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \ |
| 145 | return logger; \ |
| 146 | } |
| 147 | /** \endcond */ |
| 148 | |
| 149 | /** \brief Define a non-member log module. |
| 150 | * |
| 151 | * This macro can be used in global scope to define a log module for an entire translation |
| 152 | * unit, or in namespace scope to define a log module for the enclosing namespace. |
| 153 | * Use #NDN_LOG_MEMBER_INIT to define a log module as a class or struct member. |
| 154 | * |
| 155 | * \warning Do not use this macro in header files unless you know what you're doing, |
| 156 | * as it can easily trigger ODR violations if used incorrectly. |
| 157 | * |
| 158 | * \param name the logger name |
| 159 | * \note The logger name is restricted to alphanumeric characters and a select set of |
| 160 | * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with |
| 161 | * a dot (`.`), or contain multiple consecutive dots. |
| 162 | */ |
| 163 | #define NDN_LOG_INIT(name) \ |
| 164 | namespace { \ |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 165 | const bool ndn_cxx_loggerRegistration __attribute__((used)) = NDN_LOG_REGISTER_NAME(name); \ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 166 | ::ndn::util::Logger& ndn_cxx_getLogger() \ |
| 167 | NDN_LOG_INIT_FUNCTION_BODY(name) \ |
| 168 | } \ |
| 169 | struct ndn_cxx_allow_trailing_semicolon |
| 170 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 171 | /** \brief Declare a member log module, without initializing it. |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 172 | * |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 173 | * This macro should only be used to declare a log module as a class or struct member. |
Davide Pesavento | d95274b | 2018-04-12 17:42:20 -0400 | [diff] [blame] | 174 | * It is recommended to place this macro in the private or protected section of the |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 175 | * class or struct definition. Use #NDN_LOG_INIT to declare a non-member log module. |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 176 | * |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 177 | * If the enclosing class is a template, this macro can be used in conjunction with |
| 178 | * #NDN_LOG_MEMBER_DECL_SPECIALIZED and #NDN_LOG_MEMBER_INIT_SPECIALIZED to provide |
| 179 | * different loggers for different template specializations. |
| 180 | */ |
| 181 | #define NDN_LOG_MEMBER_DECL() \ |
| 182 | static ::ndn::util::Logger& ndn_cxx_getLogger(); \ |
| 183 | private: \ |
| 184 | static const bool ndn_cxx_loggerRegistration |
| 185 | |
| 186 | /** \brief Initialize a member log module. |
| 187 | * |
| 188 | * This macro should only be used to initialize a previously declared member log module. |
| 189 | * It must be placed in a .cpp file (NOT in a header file), in the same namespace as |
| 190 | * the class or struct that contains the log module. |
| 191 | * |
| 192 | * \param cls class name; wrap in parentheses if it contains commas |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 193 | * \param name the logger name |
| 194 | * \note The logger name is restricted to alphanumeric characters and a select set of |
| 195 | * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with |
| 196 | * a dot (`.`), or contain multiple consecutive dots. |
| 197 | */ |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 198 | #define NDN_LOG_MEMBER_INIT(cls, name) \ |
| 199 | const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration = \ |
| 200 | NDN_LOG_REGISTER_NAME(name); \ |
| 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 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 205 | /** \brief Declare an explicit specialization of a member log module of a class template. |
| 206 | * |
| 207 | * \param cls fully specialized class name; wrap in parentheses if it contains commas |
| 208 | */ |
| 209 | #define NDN_LOG_MEMBER_DECL_SPECIALIZED(cls) \ |
Davide Pesavento | d95274b | 2018-04-12 17:42:20 -0400 | [diff] [blame] | 210 | template<> \ |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 211 | const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration; \ |
| 212 | template<> \ |
Davide Pesavento | d95274b | 2018-04-12 17:42:20 -0400 | [diff] [blame] | 213 | ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger() |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 214 | |
| 215 | /** \brief Define an explicit specialization of a member log module of a class template. |
| 216 | * |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 217 | * This macro must be placed in a .cpp file (NOT in a header file), in the same namespace |
| 218 | * as the class template that contains the log module. |
| 219 | * |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 220 | * \param cls fully specialized class name; wrap in parentheses if it contains commas |
| 221 | * \param name the logger name |
| 222 | * \note The logger name is restricted to alphanumeric characters and a select set of |
| 223 | * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with |
| 224 | * a dot (`.`), or contain multiple consecutive dots. |
| 225 | */ |
| 226 | #define NDN_LOG_MEMBER_INIT_SPECIALIZED(cls, name) \ |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 227 | template<> \ |
| 228 | const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration = \ |
| 229 | NDN_LOG_REGISTER_NAME(name); \ |
| 230 | template<> \ |
Davide Pesavento | d95274b | 2018-04-12 17:42:20 -0400 | [diff] [blame] | 231 | ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger() \ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 232 | NDN_LOG_INIT_FUNCTION_BODY(name) \ |
| 233 | struct ndn_cxx_allow_trailing_semicolon |
| 234 | |
| 235 | /** \cond */ |
| 236 | #if BOOST_VERSION == 105900 |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 237 | // workaround Boost bug 11549 |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 238 | #define NDN_BOOST_LOG(x) BOOST_LOG(x) << "" |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 239 | #else |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 240 | #define NDN_BOOST_LOG(x) BOOST_LOG(x) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 241 | #endif |
| 242 | |
Davide Pesavento | 1c9c96c | 2018-04-25 10:45:08 -0400 | [diff] [blame] | 243 | // implementation detail |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 244 | #define NDN_LOG_INTERNAL(lvl, lvlstr, expression) \ |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 245 | do { \ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 246 | if (ndn_cxx_getLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \ |
| 247 | NDN_BOOST_LOG(ndn_cxx_getLogger()) << ::ndn::util::detail::LoggerTimestamp{} \ |
| 248 | << " " BOOST_STRINGIZE(lvlstr) ": [" << ndn_cxx_getLogger().getModuleName() << "] " \ |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 249 | << expression; \ |
| 250 | } \ |
| 251 | } while (false) |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 252 | /** \endcond */ |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 253 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 254 | /** \brief Log at TRACE level. |
| 255 | * \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] | 256 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 257 | #define NDN_LOG_TRACE(expression) NDN_LOG_INTERNAL(TRACE, TRACE, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 258 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 259 | /** \brief Log at DEBUG level. |
| 260 | * \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] | 261 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 262 | #define NDN_LOG_DEBUG(expression) NDN_LOG_INTERNAL(DEBUG, DEBUG, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 263 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 264 | /** \brief Log at INFO level. |
| 265 | * \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] | 266 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 267 | #define NDN_LOG_INFO(expression) NDN_LOG_INTERNAL(INFO, INFO, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 268 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 269 | /** \brief Log at WARN level. |
| 270 | * \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] | 271 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 272 | #define NDN_LOG_WARN(expression) NDN_LOG_INTERNAL(WARN, WARNING, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 273 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 274 | /** \brief Log at ERROR level. |
| 275 | * \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] | 276 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 277 | #define NDN_LOG_ERROR(expression) NDN_LOG_INTERNAL(ERROR, ERROR, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 278 | |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 279 | /** \brief Log at FATAL level. |
| 280 | * \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] | 281 | */ |
Davide Pesavento | 0c145ec | 2018-04-10 21:33:57 -0400 | [diff] [blame] | 282 | #define NDN_LOG_FATAL(expression) NDN_LOG_INTERNAL(FATAL, FATAL, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 283 | |
| 284 | } // namespace util |
| 285 | } // namespace ndn |
| 286 | |
Alexander Afanasyev | ed1e99d | 2016-11-05 09:59:35 -0600 | [diff] [blame] | 287 | #endif // HAVE_NDN_CXX_CUSTOM_LOGGER |
| 288 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 289 | #endif // NDN_UTIL_LOGGER_HPP |