Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 Regents of the University of California, |
| 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 |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 9 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame^] | 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 24 | |
| 25 | #ifndef NFD_CORE_LOGGER_HPP |
| 26 | #define NFD_CORE_LOGGER_HPP |
| 27 | |
| 28 | #include "common.hpp" |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 29 | #include <ndn-cpp-dev/util/time.hpp> |
| 30 | |
| 31 | /// \todo use when we enable C++11 (see todo in now()) |
| 32 | // #include <cinttypes> |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 33 | |
| 34 | namespace nfd { |
| 35 | |
| 36 | enum LogLevel { |
| 37 | LOG_NONE = 0, // no messages |
| 38 | LOG_ERROR = 1, // serious error messages |
| 39 | LOG_WARN = 2, // warning messages |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 40 | LOG_INFO = 3, // informational messages |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 41 | LOG_DEBUG = 4, // debug messages |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 42 | LOG_TRACE = 5, // trace messages (most verbose) |
| 43 | // LOG_FATAL is not a level and is logged unconditionally |
| 44 | LOG_ALL = 255, // all messages |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | class Logger |
| 48 | { |
| 49 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 50 | |
| 51 | Logger(const std::string& name, LogLevel level) |
| 52 | : m_moduleName(name) |
| 53 | , m_enabledLogLevel(level) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 54 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool |
| 58 | isEnabled(LogLevel level) const |
| 59 | { |
| 60 | // std::cerr << m_moduleName << |
| 61 | // " enabled = " << m_enabledLogLevel |
| 62 | // << " level = " << level << std::endl; |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 63 | return (m_enabledLogLevel >= level); |
| 64 | } |
| 65 | |
| 66 | void |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 67 | setLogLevel(LogLevel level) |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 68 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 69 | m_enabledLogLevel = level; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 70 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 71 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 72 | const std::string& |
| 73 | getName() const |
| 74 | { |
| 75 | return m_moduleName; |
| 76 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 77 | |
| 78 | void |
| 79 | setName(const std::string& name) |
| 80 | { |
| 81 | m_moduleName = name; |
| 82 | } |
| 83 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 84 | |
| 85 | /// \brief return a string representation of time since epoch: seconds.microseconds |
| 86 | static std::string |
| 87 | now() |
| 88 | { |
| 89 | using namespace ndn::time; |
| 90 | |
| 91 | static const microseconds::rep ONE_SECOND = 1000000; |
| 92 | |
| 93 | // 10 (whole seconds) + '.' + 6 (fraction) + 1 (\0) |
| 94 | char buffer[10 + 1 + 6 + 1]; |
| 95 | |
| 96 | microseconds::rep microseconds_since_epoch = |
| 97 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 98 | |
| 99 | ::snprintf(buffer, sizeof(buffer), "%lld.%06lld", |
| 100 | static_cast<long long int>(microseconds_since_epoch / ONE_SECOND), |
| 101 | static_cast<long long int>(microseconds_since_epoch % ONE_SECOND)); |
| 102 | |
| 103 | /// \todo use this version when we enable C++11 to avoid casting |
| 104 | // ::snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64, |
| 105 | // microseconds_since_epoch / ONE_SECOND, |
| 106 | // microseconds_since_epoch % ONE_SECOND); |
| 107 | |
| 108 | return std::string(buffer); |
| 109 | } |
| 110 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 111 | private: |
| 112 | std::string m_moduleName; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 113 | LogLevel m_enabledLogLevel; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 116 | inline std::ostream& |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 117 | operator<<(std::ostream& output, const Logger& logger) |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 118 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 119 | output << logger.getName(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 120 | return output; |
| 121 | } |
| 122 | |
| 123 | } // namespace nfd |
| 124 | |
| 125 | #include "core/logger-factory.hpp" |
| 126 | |
| 127 | namespace nfd { |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 128 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 129 | #define NFD_LOG_INIT(name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 130 | static nfd::Logger& g_logger = nfd::LoggerFactory::create(name); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 131 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 132 | #define NFD_LOG_INCLASS_DECLARE() \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 133 | static nfd::Logger& g_logger; |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 134 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 135 | #define NFD_LOG_INCLASS_DEFINE(cls, name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 136 | nfd::Logger& cls::g_logger = nfd::LoggerFactory::create(name); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 137 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 138 | #define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 139 | template<class T> \ |
| 140 | nfd::Logger& cls<T>::g_logger = nfd::LoggerFactory::create(name); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 141 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 142 | #define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \ |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 143 | template<> \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 144 | nfd::Logger& cls<specialization>::g_logger = nfd::LoggerFactory::create(name); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 146 | #define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 147 | template<> \ |
| 148 | nfd::Logger& cls<s1, s2>::g_logger = nfd::LoggerFactory::create(name); |
| 149 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 150 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 151 | #define NFD_LOG(level, expression) \ |
| 152 | do { \ |
| 153 | if (g_logger.isEnabled(::nfd::LOG_##level)) \ |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame] | 154 | std::clog << ::nfd::Logger::now() << " "#level": " \ |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 155 | << "[" << g_logger << "] " << expression << "\n"; \ |
| 156 | } while (false) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 157 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 158 | #define NFD_LOG_TRACE(expression) NFD_LOG(TRACE, expression) |
| 159 | #define NFD_LOG_DEBUG(expression) NFD_LOG(DEBUG, expression) |
| 160 | #define NFD_LOG_INFO(expression) NFD_LOG(INFO, expression) |
| 161 | #define NFD_LOG_ERROR(expression) NFD_LOG(ERROR, expression) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 162 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 163 | // specialize WARN because the message is "WARNING" instead of "WARN" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 164 | #define NFD_LOG_WARN(expression) \ |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 165 | do { \ |
| 166 | if (g_logger.isEnabled(::nfd::LOG_WARN)) \ |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame] | 167 | std::clog << ::nfd::Logger::now() << " WARNING: " \ |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 168 | << "[" << g_logger << "] " << expression << "\n"; \ |
| 169 | } while (false) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 170 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 171 | #define NFD_LOG_FATAL(expression) \ |
| 172 | do { \ |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame] | 173 | std::clog << ::nfd::Logger::now() << " FATAL: " \ |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 174 | << "[" << g_logger << "] " << expression << "\n"; \ |
| 175 | } while (false) |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 176 | |
| 177 | } //namespace nfd |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 178 | |
| 179 | #endif |