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 | /* |
| 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_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 | |
| 38 | /** \brief indicates the severity level of a log message |
| 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 | |
| 51 | /** \brief output LogLevel as string |
| 52 | * \throw std::invalid_argument unknown \p level |
| 53 | */ |
| 54 | std::ostream& |
| 55 | operator<<(std::ostream& os, LogLevel level); |
| 56 | |
| 57 | /** \brief parse LogLevel from string |
| 58 | * \throw std::invalid_argument unknown level name |
| 59 | */ |
| 60 | LogLevel |
| 61 | parseLogLevel(const std::string& s); |
| 62 | |
| 63 | /** \brief represents a logger in logging facility |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 64 | * \note User should declare a new logger with \p NDN_LOG_INIT macro. |
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 | |
| 95 | /** \brief declare a log module |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 96 | * |
| 97 | * \note Logger name is restricted to alphanumeric characters and a select set of |
| 98 | * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-` |
| 99 | * A logger name must not start or end with `.` or contain consecutive `.` |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 100 | */ |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 101 | #define NDN_LOG_INIT(name) \ |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 102 | namespace { \ |
| 103 | inline ::ndn::util::Logger& getNdnCxxLogger() \ |
| 104 | { \ |
| 105 | static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \ |
| 106 | return logger; \ |
| 107 | } \ |
| 108 | } \ |
| 109 | struct ndn_cxx__allow_trailing_semicolon |
| 110 | |
| 111 | /** \brief a tag that writes a timestamp upon stream output |
| 112 | * \code |
| 113 | * std::clog << LoggerTimestamp(); |
| 114 | * \endcode |
| 115 | */ |
| 116 | struct LoggerTimestamp |
| 117 | { |
| 118 | }; |
| 119 | |
| 120 | /** \brief write a timestamp to \p os |
| 121 | * \note This function is thread-safe. |
| 122 | */ |
| 123 | std::ostream& |
| 124 | operator<<(std::ostream& os, const LoggerTimestamp&); |
| 125 | |
| 126 | #if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106000) |
| 127 | // workaround Boost bug 11549 |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 128 | #define NDN_BOOST_LOG(x) BOOST_LOG(x) << "" |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 129 | #else |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 130 | #define NDN_BOOST_LOG(x) BOOST_LOG(x) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 131 | #endif |
| 132 | |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 133 | #define NDN_LOG(lvl, lvlstr, expression) \ |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 134 | do { \ |
| 135 | if (getNdnCxxLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \ |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 136 | NDN_BOOST_LOG(getNdnCxxLogger()) << ::ndn::util::LoggerTimestamp{} \ |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 137 | << " " BOOST_STRINGIZE(lvlstr) ": [" << getNdnCxxLogger().getModuleName() << "] " \ |
| 138 | << expression; \ |
| 139 | } \ |
| 140 | } while (false) |
| 141 | |
| 142 | /** \brief log at TRACE level |
| 143 | * \pre A log module must be declared in the same translation unit. |
| 144 | */ |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 145 | #define NDN_LOG_TRACE(expression) NDN_LOG(TRACE, TRACE, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 146 | |
| 147 | /** \brief log at DEBUG level |
| 148 | * \pre A log module must be declared in the same translation unit. |
| 149 | */ |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 150 | #define NDN_LOG_DEBUG(expression) NDN_LOG(DEBUG, DEBUG, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 151 | |
| 152 | /** \brief log at INFO level |
| 153 | * \pre A log module must be declared in the same translation unit. |
| 154 | */ |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 155 | #define NDN_LOG_INFO(expression) NDN_LOG(INFO, INFO, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 156 | |
| 157 | /** \brief log at WARN level |
| 158 | * \pre A log module must be declared in the same translation unit. |
| 159 | */ |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 160 | #define NDN_LOG_WARN(expression) NDN_LOG(WARN, WARNING, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 161 | |
| 162 | /** \brief log at ERROR level |
| 163 | * \pre A log module must be declared in the same translation unit. |
| 164 | */ |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 165 | #define NDN_LOG_ERROR(expression) NDN_LOG(ERROR, ERROR, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 166 | |
| 167 | /** \brief log at FATAL level |
| 168 | * \pre A log module must be declared in the same translation unit. |
| 169 | */ |
Junxiao Shi | 1fe7ce5 | 2016-08-08 05:48:02 +0000 | [diff] [blame] | 170 | #define NDN_LOG_FATAL(expression) NDN_LOG(FATAL, FATAL, expression) |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 171 | |
| 172 | } // namespace util |
| 173 | } // namespace ndn |
| 174 | |
Alexander Afanasyev | ed1e99d | 2016-11-05 09:59:35 -0600 | [diff] [blame] | 175 | #endif // HAVE_NDN_CXX_CUSTOM_LOGGER |
| 176 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 177 | #endif // NDN_UTIL_LOGGER_HPP |