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" |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 13 | |
| 14 | namespace nfd { |
| 15 | |
| 16 | enum LogLevel { |
| 17 | LOG_NONE = 0, // no messages |
| 18 | LOG_ERROR = 1, // serious error messages |
| 19 | LOG_WARN = 2, // warning messages |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 20 | LOG_INFO = 3, // informational messages |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 21 | LOG_DEBUG = 4, // debug messages |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 22 | LOG_TRACE = 5, // trace messages (most verbose) |
| 23 | // LOG_FATAL is not a level and is logged unconditionally |
| 24 | LOG_ALL = 255, // all messages |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | class Logger |
| 28 | { |
| 29 | public: |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 30 | |
| 31 | Logger(const std::string& name, LogLevel level) |
| 32 | : m_moduleName(name) |
| 33 | , m_enabledLogLevel(level) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 34 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 35 | } |
| 36 | |
| 37 | bool |
| 38 | isEnabled(LogLevel level) const |
| 39 | { |
| 40 | // std::cerr << m_moduleName << |
| 41 | // " enabled = " << m_enabledLogLevel |
| 42 | // << " level = " << level << std::endl; |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 43 | return (m_enabledLogLevel >= level); |
| 44 | } |
| 45 | |
| 46 | void |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 47 | setLogLevel(LogLevel level) |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 48 | { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 49 | m_enabledLogLevel = level; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 50 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 51 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 52 | const std::string& |
| 53 | getName() const |
| 54 | { |
| 55 | return m_moduleName; |
| 56 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 57 | |
| 58 | void |
| 59 | setName(const std::string& name) |
| 60 | { |
| 61 | m_moduleName = name; |
| 62 | } |
| 63 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 64 | private: |
| 65 | std::string m_moduleName; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 66 | LogLevel m_enabledLogLevel; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 69 | inline std::ostream& |
| 70 | operator<<(std::ostream& output, const Logger& obj) |
| 71 | { |
| 72 | output << obj.getName(); |
| 73 | return output; |
| 74 | } |
| 75 | |
| 76 | } // namespace nfd |
| 77 | |
| 78 | #include "core/logger-factory.hpp" |
| 79 | |
| 80 | namespace nfd { |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 81 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 82 | #define NFD_LOG_INIT(name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 83 | static nfd::Logger& g_logger = nfd::LoggerFactory::create(name); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 85 | #define NFD_LOG_INCLASS_DECLARE() \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 86 | static nfd::Logger& g_logger; |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 87 | |
| 88 | #define NFD_LOG_INCLASS_DEFINE(cls, name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 89 | nfd::Logger& cls::g_logger = nfd::LoggerFactory::create(name); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 90 | |
| 91 | #define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 92 | template<class T> \ |
| 93 | nfd::Logger& cls<T>::g_logger = nfd::LoggerFactory::create(name); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 94 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 95 | #define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \ |
| 96 | template<> \ |
| 97 | nfd::Logger& cls<specialization>::g_logger = nfd::LoggerFactory::create(name); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 99 | #define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 100 | template<> \ |
| 101 | nfd::Logger& cls<s1, s2>::g_logger = nfd::LoggerFactory::create(name); |
| 102 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 103 | |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 104 | #define NFD_LOG_TRACE(expression) \ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 105 | if (g_logger.isEnabled(nfd::LOG_TRACE)) \ |
| 106 | std::cerr<<"TRACE: "<<"["<<g_logger<<"] " << expression << "\n" |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 107 | |
| 108 | #define NFD_LOG_DEBUG(expression)\ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 109 | if (g_logger.isEnabled(nfd::LOG_DEBUG)) \ |
| 110 | std::cerr<<"DEBUG: "<<"["<<g_logger<<"] " << expression <<"\n" |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 111 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 112 | #define NFD_LOG_WARN(expression) \ |
| 113 | if (g_logger.isEnabled(nfd::LOG_WARN)) \ |
| 114 | std::cerr<<"WARNING: "<<"["<<g_logger<<"] " << expression <<"\n" |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 115 | |
| 116 | #define NFD_LOG_INFO(expression)\ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 117 | if (g_logger.isEnabled(nfd::LOG_INFO)) \ |
| 118 | std::cerr<<"INFO: "<<"["<<g_logger<<"] " << expression <<"\n" |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 119 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame^] | 120 | #define NFD_LOG_ERROR(expression) \ |
| 121 | if (g_logger.isEnabled(nfd::LOG_ERROR)) \ |
| 122 | std::cerr<<"ERROR: "<<"["<<g_logger<<"] " << expression <<"\n" |
| 123 | |
| 124 | #define NFD_LOG_FATAL(expression)\ |
| 125 | std::cerr<<"FATAL: "<<"["<<g_logger<<"] " << expression <<"\n" |
| 126 | |
| 127 | } //namespace nfd |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 128 | |
| 129 | #endif |