Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
| 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 | |
| 27 | #include <boost/log/common.hpp> |
| 28 | #include <boost/log/sources/logger.hpp> |
| 29 | #include <atomic> |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace util { |
| 33 | |
| 34 | /** \brief indicates the severity level of a log message |
| 35 | */ |
| 36 | enum class LogLevel { |
| 37 | FATAL = -1, ///< fatal (will be logged unconditionally) |
| 38 | NONE = 0, ///< no messages |
| 39 | ERROR = 1, ///< serious error messages |
| 40 | WARN = 2, ///< warning messages |
| 41 | INFO = 3, ///< informational messages |
| 42 | DEBUG = 4, ///< debug messages |
| 43 | TRACE = 5, ///< trace messages (most verbose) |
| 44 | ALL = 255 ///< all messages |
| 45 | }; |
| 46 | |
| 47 | /** \brief output LogLevel as string |
| 48 | * \throw std::invalid_argument unknown \p level |
| 49 | */ |
| 50 | std::ostream& |
| 51 | operator<<(std::ostream& os, LogLevel level); |
| 52 | |
| 53 | /** \brief parse LogLevel from string |
| 54 | * \throw std::invalid_argument unknown level name |
| 55 | */ |
| 56 | LogLevel |
| 57 | parseLogLevel(const std::string& s); |
| 58 | |
| 59 | /** \brief represents a logger in logging facility |
| 60 | * \note User should declare a new logger with \p NDN_CXX_LOG_INIT macro. |
| 61 | */ |
| 62 | class Logger : public boost::log::sources::logger_mt |
| 63 | { |
| 64 | public: |
| 65 | explicit |
| 66 | Logger(const std::string& name); |
| 67 | |
| 68 | const std::string& |
| 69 | getModuleName() const |
| 70 | { |
| 71 | return m_moduleName; |
| 72 | } |
| 73 | |
| 74 | bool |
| 75 | isLevelEnabled(LogLevel level) const |
| 76 | { |
| 77 | return m_currentLevel.load(std::memory_order_relaxed) >= level; |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | setLevel(LogLevel level) |
| 82 | { |
| 83 | m_currentLevel.store(level, std::memory_order_relaxed); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | const std::string m_moduleName; |
| 88 | std::atomic<LogLevel> m_currentLevel; |
| 89 | }; |
| 90 | |
| 91 | /** \brief declare a log module |
| 92 | */ |
| 93 | #define NDN_CXX_LOG_INIT(name) \ |
| 94 | namespace { \ |
| 95 | inline ::ndn::util::Logger& getNdnCxxLogger() \ |
| 96 | { \ |
| 97 | static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \ |
| 98 | return logger; \ |
| 99 | } \ |
| 100 | } \ |
| 101 | struct ndn_cxx__allow_trailing_semicolon |
| 102 | |
| 103 | /** \brief a tag that writes a timestamp upon stream output |
| 104 | * \code |
| 105 | * std::clog << LoggerTimestamp(); |
| 106 | * \endcode |
| 107 | */ |
| 108 | struct LoggerTimestamp |
| 109 | { |
| 110 | }; |
| 111 | |
| 112 | /** \brief write a timestamp to \p os |
| 113 | * \note This function is thread-safe. |
| 114 | */ |
| 115 | std::ostream& |
| 116 | operator<<(std::ostream& os, const LoggerTimestamp&); |
| 117 | |
| 118 | #if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106000) |
| 119 | // workaround Boost bug 11549 |
| 120 | #define NDN_CXX_BOOST_LOG(x) BOOST_LOG(x) << "" |
| 121 | #else |
| 122 | #define NDN_CXX_BOOST_LOG(x) BOOST_LOG(x) |
| 123 | #endif |
| 124 | |
| 125 | #define NDN_CXX_LOG(lvl, lvlstr, expression) \ |
| 126 | do { \ |
| 127 | if (getNdnCxxLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \ |
| 128 | NDN_CXX_BOOST_LOG(getNdnCxxLogger()) << ::ndn::util::LoggerTimestamp{} \ |
| 129 | << " " BOOST_STRINGIZE(lvlstr) ": [" << getNdnCxxLogger().getModuleName() << "] " \ |
| 130 | << expression; \ |
| 131 | } \ |
| 132 | } while (false) |
| 133 | |
| 134 | /** \brief log at TRACE level |
| 135 | * \pre A log module must be declared in the same translation unit. |
| 136 | */ |
| 137 | #define NDN_CXX_LOG_TRACE(expression) NDN_CXX_LOG(TRACE, TRACE, expression) |
| 138 | |
| 139 | /** \brief log at DEBUG level |
| 140 | * \pre A log module must be declared in the same translation unit. |
| 141 | */ |
| 142 | #define NDN_CXX_LOG_DEBUG(expression) NDN_CXX_LOG(DEBUG, DEBUG, expression) |
| 143 | |
| 144 | /** \brief log at INFO level |
| 145 | * \pre A log module must be declared in the same translation unit. |
| 146 | */ |
| 147 | #define NDN_CXX_LOG_INFO(expression) NDN_CXX_LOG(INFO, INFO, expression) |
| 148 | |
| 149 | /** \brief log at WARN level |
| 150 | * \pre A log module must be declared in the same translation unit. |
| 151 | */ |
| 152 | #define NDN_CXX_LOG_WARN(expression) NDN_CXX_LOG(WARN, WARNING, expression) |
| 153 | |
| 154 | /** \brief log at ERROR level |
| 155 | * \pre A log module must be declared in the same translation unit. |
| 156 | */ |
| 157 | #define NDN_CXX_LOG_ERROR(expression) NDN_CXX_LOG(ERROR, ERROR, expression) |
| 158 | |
| 159 | /** \brief log at FATAL level |
| 160 | * \pre A log module must be declared in the same translation unit. |
| 161 | */ |
| 162 | #define NDN_CXX_LOG_FATAL(expression) NDN_CXX_LOG(FATAL, FATAL, expression) |
| 163 | |
| 164 | } // namespace util |
| 165 | } // namespace ndn |
| 166 | |
| 167 | #endif // NDN_UTIL_LOGGER_HPP |