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