logger.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #ifndef NFD_CORE_LOGGER_HPP
27 #define NFD_CORE_LOGGER_HPP
28 
29 #include "common.hpp"
30 
31 #ifdef HAVE_CUSTOM_LOGGER
32 #include "custom-logger.hpp"
33 #else
34 #include <boost/log/common.hpp>
35 #include <boost/log/sources/logger.hpp>
36 
37 namespace nfd {
38 
42 enum LogLevel {
43  LOG_FATAL = -1, // fatal (will be logged unconditionally)
44  LOG_NONE = 0, // no messages
45  LOG_ERROR = 1, // serious error messages
46  LOG_WARN = 2, // warning messages
47  LOG_INFO = 3, // informational messages
48  LOG_DEBUG = 4, // debug messages
49  LOG_TRACE = 5, // trace messages (most verbose)
50  LOG_ALL = 255 // all messages
51 };
52 
58 class Logger
59 {
60 public:
61  Logger(const std::string& name, LogLevel level);
62 
63  bool
64  isEnabled(LogLevel level) const
65  {
66  return m_enabledLogLevel >= level;
67  }
68 
69  void
71  {
72  m_enabledLogLevel = level;
73  }
74 
75  const std::string&
76  getName() const
77  {
78  return m_moduleName;
79  }
80 
81  void
82  setName(const std::string& name)
83  {
84  m_moduleName = name;
85  }
86 
87 public:
88  boost::log::sources::logger boostLogger;
89 
90 private:
91  std::string m_moduleName;
92  LogLevel m_enabledLogLevel;
93 };
94 
95 inline std::ostream&
96 operator<<(std::ostream& output, const Logger& logger)
97 {
98  output << logger.getName();
99  return output;
100 }
101 
106 {
107 };
108 
113 std::ostream&
114 operator<<(std::ostream& os, const LoggerTimestamp&);
115 
116 } // namespace nfd
117 
118 #include "core/logger-factory.hpp"
119 
120 namespace nfd {
121 
122 #define NFD_LOG_INIT(name) \
123 static ::nfd::Logger& g_logger = ::nfd::LoggerFactory::create(name)
124 
125 #define NFD_LOG_INCLASS_DECLARE() \
126 static ::nfd::Logger& g_logger
127 
128 #define NFD_LOG_INCLASS_DEFINE(cls, name) \
129 ::nfd::Logger& cls::g_logger = ::nfd::LoggerFactory::create(name)
130 
131 #define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \
132 template<class T> \
133 ::nfd::Logger& cls<T>::g_logger = ::nfd::LoggerFactory::create(name)
134 
135 #define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \
136 template<> \
137 ::nfd::Logger& cls<specialization>::g_logger = ::nfd::LoggerFactory::create(name)
138 
139 #define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \
140 template<> \
141 ::nfd::Logger& cls<s1, s2>::g_logger = ::nfd::LoggerFactory::create(name)
142 
143 #if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106000)
144 // workaround Boost bug 11549
145 #define NFD_BOOST_LOG(x) BOOST_LOG(x) << ""
146 #else
147 #define NFD_BOOST_LOG(x) BOOST_LOG(x)
148 #endif
149 
150 #define NFD_LOG_LINE(msg, expression) \
151 ::nfd::LoggerTimestamp{} << " "#msg": " << "[" << g_logger << "] " << expression
152 
153 #define NFD_LOG(level, msg, expression) \
154  do { \
155  if (g_logger.isEnabled(::nfd::LOG_##level)) { \
156  NFD_BOOST_LOG(g_logger.boostLogger) << NFD_LOG_LINE(msg, expression); \
157  } \
158  } while (false)
159 
160 #define NFD_LOG_TRACE(expression) NFD_LOG(TRACE, TRACE, expression)
161 #define NFD_LOG_DEBUG(expression) NFD_LOG(DEBUG, DEBUG, expression)
162 #define NFD_LOG_INFO(expression) NFD_LOG(INFO, INFO, expression)
163 #define NFD_LOG_WARN(expression) NFD_LOG(WARN, WARNING, expression)
164 #define NFD_LOG_ERROR(expression) NFD_LOG(ERROR, ERROR, expression)
165 #define NFD_LOG_FATAL(expression) NFD_LOG(FATAL, FATAL, expression)
166 
167 } // namespace nfd
168 
169 #endif // HAVE_CUSTOM_LOGGER
170 
171 #endif // NFD_CORE_LOGGER_HPP
a tag that writes a timestamp upon stream output
Definition: logger.hpp:105
boost::log::sources::logger boostLogger
Definition: logger.hpp:88
std::ostream & operator<<(std::ostream &os, const LoggerTimestamp &)
write a timestamp to os
Definition: logger.cpp:46
bool isEnabled(LogLevel level) const
Definition: logger.hpp:64
void setName(const std::string &name)
Definition: logger.hpp:82
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void setLogLevel(LogLevel level)
Definition: logger.hpp:70
Logger(const std::string &name, LogLevel level)
Definition: logger.cpp:39
LogLevel
indicates a log level
Definition: logger.hpp:42
provides logging for a module
Definition: logger.hpp:58
const std::string & getName() const
Definition: logger.hpp:76