Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 70db992 | 2014-11-16 21:12:03 -0700 | [diff] [blame] | 24 | */ |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 25 | |
| 26 | #ifndef NFD_CORE_LOGGER_HPP |
| 27 | #define NFD_CORE_LOGGER_HPP |
| 28 | |
| 29 | #include "common.hpp" |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 31 | #ifdef HAVE_CUSTOM_LOGGER |
| 32 | #include "custom-logger.hpp" |
| 33 | #else |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 34 | #include <boost/log/common.hpp> |
| 35 | #include <boost/log/sources/logger.hpp> |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 36 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 37 | namespace nfd { |
| 38 | |
Junxiao Shi | 70db992 | 2014-11-16 21:12:03 -0700 | [diff] [blame] | 39 | /** \brief indicates a log level |
| 40 | * \note This type is internal. Logger should be accessed through NFD_LOG_* macros. |
| 41 | */ |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 42 | enum LogLevel { |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 43 | LOG_FATAL = -1, // fatal (will be logged unconditionally) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 44 | LOG_NONE = 0, // no messages |
| 45 | LOG_ERROR = 1, // serious error messages |
| 46 | LOG_WARN = 2, // warning messages |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 47 | LOG_INFO = 3, // informational messages |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 48 | LOG_DEBUG = 4, // debug messages |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 49 | LOG_TRACE = 5, // trace messages (most verbose) |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 50 | LOG_ALL = 255 // all messages |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
Junxiao Shi | 70db992 | 2014-11-16 21:12:03 -0700 | [diff] [blame] | 53 | /** \brief provides logging for a module |
| 54 | * \note This type is internal. Logger should be accessed through NFD_LOG_* macros. |
| 55 | * \note This type is copyable because logger can be declared as a field of |
| 56 | * (usually template) classes, and shouldn't prevent those classes to be copyable. |
| 57 | */ |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 58 | class Logger |
| 59 | { |
| 60 | public: |
Junxiao Shi | 70db992 | 2014-11-16 21:12:03 -0700 | [diff] [blame] | 61 | Logger(const std::string& name, LogLevel level); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 62 | |
| 63 | bool |
| 64 | isEnabled(LogLevel level) const |
| 65 | { |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 66 | return m_enabledLogLevel >= level; |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 70 | setLogLevel(LogLevel level) |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 71 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 72 | m_enabledLogLevel = level; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 73 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 74 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 75 | const std::string& |
| 76 | getName() const |
| 77 | { |
| 78 | return m_moduleName; |
| 79 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 80 | |
| 81 | void |
| 82 | setName(const std::string& name) |
| 83 | { |
| 84 | m_moduleName = name; |
| 85 | } |
| 86 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 87 | public: |
| 88 | boost::log::sources::logger boostLogger; |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 89 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 90 | private: |
| 91 | std::string m_moduleName; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 92 | LogLevel m_enabledLogLevel; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 95 | inline std::ostream& |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 96 | operator<<(std::ostream& output, const Logger& logger) |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 97 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 98 | output << logger.getName(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 99 | return output; |
| 100 | } |
| 101 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 102 | /** |
| 103 | * \brief a tag that writes a timestamp upon stream output |
| 104 | */ |
| 105 | struct LoggerTimestamp |
| 106 | { |
| 107 | }; |
| 108 | |
| 109 | /** |
| 110 | * \brief write a timestamp to \p os |
| 111 | * \note This function is thread-safe. |
| 112 | */ |
| 113 | std::ostream& |
| 114 | operator<<(std::ostream& os, const LoggerTimestamp&); |
| 115 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 116 | } // namespace nfd |
| 117 | |
| 118 | #include "core/logger-factory.hpp" |
| 119 | |
| 120 | namespace nfd { |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 121 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 122 | #define NFD_LOG_INIT(name) \ |
Alexander Afanasyev | 6ca6cf7 | 2016-06-15 13:22:16 -0700 | [diff] [blame^] | 123 | static ::nfd::Logger& g_logger = ::nfd::LoggerFactory::create(name) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 124 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 125 | #define NFD_LOG_INCLASS_DECLARE() \ |
Alexander Afanasyev | 6ca6cf7 | 2016-06-15 13:22:16 -0700 | [diff] [blame^] | 126 | static ::nfd::Logger& g_logger |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 127 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 128 | #define NFD_LOG_INCLASS_DEFINE(cls, name) \ |
Alexander Afanasyev | 6ca6cf7 | 2016-06-15 13:22:16 -0700 | [diff] [blame^] | 129 | ::nfd::Logger& cls::g_logger = ::nfd::LoggerFactory::create(name) |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 130 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 131 | #define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \ |
| 132 | template<class T> \ |
Alexander Afanasyev | 6ca6cf7 | 2016-06-15 13:22:16 -0700 | [diff] [blame^] | 133 | ::nfd::Logger& cls<T>::g_logger = ::nfd::LoggerFactory::create(name) |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 134 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 135 | #define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \ |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 136 | template<> \ |
Alexander Afanasyev | 6ca6cf7 | 2016-06-15 13:22:16 -0700 | [diff] [blame^] | 137 | ::nfd::Logger& cls<specialization>::g_logger = ::nfd::LoggerFactory::create(name) |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 139 | #define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \ |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 140 | template<> \ |
Alexander Afanasyev | 6ca6cf7 | 2016-06-15 13:22:16 -0700 | [diff] [blame^] | 141 | ::nfd::Logger& cls<s1, s2>::g_logger = ::nfd::LoggerFactory::create(name) |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 142 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 143 | #define NFD_LOG_LINE(msg, expression) \ |
Alexander Afanasyev | 6ca6cf7 | 2016-06-15 13:22:16 -0700 | [diff] [blame^] | 144 | ::nfd::LoggerTimestamp{} << " "#msg": " << "[" << g_logger << "] " << expression |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 145 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 146 | #define NFD_LOG(level, msg, expression) \ |
| 147 | do { \ |
| 148 | if (g_logger.isEnabled(::nfd::LOG_##level)) { \ |
| 149 | BOOST_LOG(g_logger.boostLogger) << NFD_LOG_LINE(msg, expression); \ |
| 150 | } \ |
| 151 | } while (false) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 152 | |
Junxiao Shi | 70db992 | 2014-11-16 21:12:03 -0700 | [diff] [blame] | 153 | #define NFD_LOG_TRACE(expression) NFD_LOG(TRACE, TRACE, expression) |
| 154 | #define NFD_LOG_DEBUG(expression) NFD_LOG(DEBUG, DEBUG, expression) |
| 155 | #define NFD_LOG_INFO(expression) NFD_LOG(INFO, INFO, expression) |
| 156 | #define NFD_LOG_WARN(expression) NFD_LOG(WARN, WARNING, expression) |
| 157 | #define NFD_LOG_ERROR(expression) NFD_LOG(ERROR, ERROR, expression) |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 158 | #define NFD_LOG_FATAL(expression) NFD_LOG(FATAL, FATAL, expression) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 159 | |
Junxiao Shi | 70db992 | 2014-11-16 21:12:03 -0700 | [diff] [blame] | 160 | } // namespace nfd |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 161 | |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 162 | #endif // HAVE_CUSTOM_LOGGER |
| 163 | |
Junxiao Shi | 70db992 | 2014-11-16 21:12:03 -0700 | [diff] [blame] | 164 | #endif // NFD_CORE_LOGGER_HPP |