Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | * |
| 6 | * Author: Ilya Moiseenko <iliamo@ucla.edu> |
| 7 | */ |
| 8 | |
| 9 | #ifndef NFD_CORE_LOGGER_HPP |
| 10 | #define NFD_CORE_LOGGER_HPP |
| 11 | |
| 12 | #include "common.hpp" |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 13 | #include <ndn-cpp-dev/util/time.hpp> |
| 14 | |
| 15 | /// \todo use when we enable C++11 (see todo in now()) |
| 16 | // #include <cinttypes> |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 17 | |
| 18 | namespace nfd { |
| 19 | |
| 20 | enum LogLevel { |
| 21 | LOG_NONE = 0, // no messages |
| 22 | LOG_ERROR = 1, // serious error messages |
| 23 | LOG_WARN = 2, // warning messages |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 24 | LOG_INFO = 3, // informational messages |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 25 | LOG_DEBUG = 4, // debug messages |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 26 | LOG_TRACE = 5, // trace messages (most verbose) |
| 27 | // LOG_FATAL is not a level and is logged unconditionally |
| 28 | LOG_ALL = 255, // all messages |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | class Logger |
| 32 | { |
| 33 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 34 | |
| 35 | Logger(const std::string& name, LogLevel level) |
| 36 | : m_moduleName(name) |
| 37 | , m_enabledLogLevel(level) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 38 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | bool |
| 42 | isEnabled(LogLevel level) const |
| 43 | { |
| 44 | // std::cerr << m_moduleName << |
| 45 | // " enabled = " << m_enabledLogLevel |
| 46 | // << " level = " << level << std::endl; |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 47 | return (m_enabledLogLevel >= level); |
| 48 | } |
| 49 | |
| 50 | void |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 51 | setLogLevel(LogLevel level) |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 52 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 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 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 56 | const std::string& |
| 57 | getName() const |
| 58 | { |
| 59 | return m_moduleName; |
| 60 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 61 | |
| 62 | void |
| 63 | setName(const std::string& name) |
| 64 | { |
| 65 | m_moduleName = name; |
| 66 | } |
| 67 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 68 | |
| 69 | /// \brief return a string representation of time since epoch: seconds.microseconds |
| 70 | static std::string |
| 71 | now() |
| 72 | { |
| 73 | using namespace ndn::time; |
| 74 | |
| 75 | static const microseconds::rep ONE_SECOND = 1000000; |
| 76 | |
| 77 | // 10 (whole seconds) + '.' + 6 (fraction) + 1 (\0) |
| 78 | char buffer[10 + 1 + 6 + 1]; |
| 79 | |
| 80 | microseconds::rep microseconds_since_epoch = |
| 81 | duration_cast<microseconds>(system_clock::now().time_since_epoch()).count(); |
| 82 | |
| 83 | ::snprintf(buffer, sizeof(buffer), "%lld.%06lld", |
| 84 | static_cast<long long int>(microseconds_since_epoch / ONE_SECOND), |
| 85 | static_cast<long long int>(microseconds_since_epoch % ONE_SECOND)); |
| 86 | |
| 87 | /// \todo use this version when we enable C++11 to avoid casting |
| 88 | // ::snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64, |
| 89 | // microseconds_since_epoch / ONE_SECOND, |
| 90 | // microseconds_since_epoch % ONE_SECOND); |
| 91 | |
| 92 | return std::string(buffer); |
| 93 | } |
| 94 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 95 | private: |
| 96 | std::string m_moduleName; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 97 | LogLevel m_enabledLogLevel; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 100 | inline std::ostream& |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 101 | operator<<(std::ostream& output, const Logger& logger) |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 102 | { |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 103 | output << logger.getName(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 104 | return output; |
| 105 | } |
| 106 | |
| 107 | } // namespace nfd |
| 108 | |
| 109 | #include "core/logger-factory.hpp" |
| 110 | |
| 111 | namespace nfd { |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 112 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 113 | #define NFD_LOG_INIT(name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 114 | static nfd::Logger& g_logger = nfd::LoggerFactory::create(name); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 115 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 116 | #define NFD_LOG_INCLASS_DECLARE() \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 117 | static nfd::Logger& g_logger; |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 118 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 119 | #define NFD_LOG_INCLASS_DEFINE(cls, name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 120 | nfd::Logger& cls::g_logger = nfd::LoggerFactory::create(name); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 121 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 122 | #define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 123 | template<class T> \ |
| 124 | nfd::Logger& cls<T>::g_logger = nfd::LoggerFactory::create(name); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 125 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 126 | #define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \ |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 127 | template<> \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 128 | nfd::Logger& cls<specialization>::g_logger = nfd::LoggerFactory::create(name); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 129 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 130 | #define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 131 | template<> \ |
| 132 | nfd::Logger& cls<s1, s2>::g_logger = nfd::LoggerFactory::create(name); |
| 133 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 134 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 135 | #define NFD_LOG(level, expression) \ |
| 136 | do { \ |
| 137 | if (g_logger.isEnabled(::nfd::LOG_##level)) \ |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame^] | 138 | std::clog << ::nfd::Logger::now() << " "#level": " \ |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 139 | << "[" << g_logger << "] " << expression << "\n"; \ |
| 140 | } while (false) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 141 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 142 | #define NFD_LOG_TRACE(expression) NFD_LOG(TRACE, expression) |
| 143 | #define NFD_LOG_DEBUG(expression) NFD_LOG(DEBUG, expression) |
| 144 | #define NFD_LOG_INFO(expression) NFD_LOG(INFO, expression) |
| 145 | #define NFD_LOG_ERROR(expression) NFD_LOG(ERROR, expression) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 146 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 147 | // specialize WARN because the message is "WARNING" instead of "WARN" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 148 | #define NFD_LOG_WARN(expression) \ |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 149 | do { \ |
| 150 | if (g_logger.isEnabled(::nfd::LOG_WARN)) \ |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame^] | 151 | std::clog << ::nfd::Logger::now() << " WARNING: " \ |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 152 | << "[" << g_logger << "] " << expression << "\n"; \ |
| 153 | } while (false) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 154 | |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 155 | #define NFD_LOG_FATAL(expression) \ |
| 156 | do { \ |
Steve DiBenedetto | 4d43a45 | 2014-04-06 18:55:29 -0600 | [diff] [blame^] | 157 | std::clog << ::nfd::Logger::now() << " FATAL: " \ |
Steve DiBenedetto | 3a61fb4 | 2014-04-04 10:32:51 -0600 | [diff] [blame] | 158 | << "[" << g_logger << "] " << expression << "\n"; \ |
| 159 | } while (false) |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 160 | |
| 161 | } //namespace nfd |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 162 | |
| 163 | #endif |