logger.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_UTIL_LOGGER_HPP
23 #define NDN_UTIL_LOGGER_HPP
24 
25 #include "../common.hpp"
26 
27 #ifdef HAVE_NDN_CXX_CUSTOM_LOGGER
28 #include "ndn-cxx-custom-logger.hpp"
29 #else
30 
31 #include <boost/log/common.hpp>
32 #include <boost/log/sources/logger.hpp>
33 #include <atomic>
34 
35 namespace ndn {
36 namespace util {
37 
40 enum class LogLevel {
41  FATAL = -1,
42  NONE = 0,
43  ERROR = 1,
44  WARN = 2,
45  INFO = 3,
46  DEBUG = 4,
47  TRACE = 5,
48  ALL = 255
49 };
50 
54 std::ostream&
55 operator<<(std::ostream& os, LogLevel level);
56 
61 parseLogLevel(const std::string& s);
62 
66 class Logger : public boost::log::sources::logger_mt
67 {
68 public:
69  explicit
70  Logger(const std::string& name);
71 
72  const std::string&
73  getModuleName() const
74  {
75  return m_moduleName;
76  }
77 
78  bool
79  isLevelEnabled(LogLevel level) const
80  {
81  return m_currentLevel.load(std::memory_order_relaxed) >= level;
82  }
83 
84  void
86  {
87  m_currentLevel.store(level, std::memory_order_relaxed);
88  }
89 
90 private:
91  const std::string m_moduleName;
92  std::atomic<LogLevel> m_currentLevel;
93 };
94 
97 #define NDN_LOG_INIT(name) \
98  namespace { \
99  inline ::ndn::util::Logger& getNdnCxxLogger() \
100  { \
101  static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \
102  return logger; \
103  } \
104  } \
105  struct ndn_cxx__allow_trailing_semicolon
106 
113 {
114 };
115 
119 std::ostream&
120 operator<<(std::ostream& os, const LoggerTimestamp&);
121 
122 #if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106000)
123 // workaround Boost bug 11549
124 #define NDN_BOOST_LOG(x) BOOST_LOG(x) << ""
125 #else
126 #define NDN_BOOST_LOG(x) BOOST_LOG(x)
127 #endif
128 
129 #define NDN_LOG(lvl, lvlstr, expression) \
130  do { \
131  if (getNdnCxxLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \
132  NDN_BOOST_LOG(getNdnCxxLogger()) << ::ndn::util::LoggerTimestamp{} \
133  << " " BOOST_STRINGIZE(lvlstr) ": [" << getNdnCxxLogger().getModuleName() << "] " \
134  << expression; \
135  } \
136  } while (false)
137 
141 #define NDN_LOG_TRACE(expression) NDN_LOG(TRACE, TRACE, expression)
142 
146 #define NDN_LOG_DEBUG(expression) NDN_LOG(DEBUG, DEBUG, expression)
147 
151 #define NDN_LOG_INFO(expression) NDN_LOG(INFO, INFO, expression)
152 
156 #define NDN_LOG_WARN(expression) NDN_LOG(WARN, WARNING, expression)
157 
161 #define NDN_LOG_ERROR(expression) NDN_LOG(ERROR, ERROR, expression)
162 
166 #define NDN_LOG_FATAL(expression) NDN_LOG(FATAL, FATAL, expression)
167 
168 } // namespace util
169 } // namespace ndn
170 
171 #endif // HAVE_NDN_CXX_CUSTOM_LOGGER
172 
173 #endif // NDN_UTIL_LOGGER_HPP
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
trace messages (most verbose)
serious error messages
a tag that writes a timestamp upon stream output
Definition: logger.hpp:112
informational messages
LogLevel
indicates the severity level of a log message
Definition: logger.hpp:40
LogLevel parseLogLevel(const std::string &s)
parse LogLevel from string
Definition: logger.cpp:60
Logger(const std::string &name)
Definition: logger.cpp:82
warning messages
std::ostream & operator<<(std::ostream &os, Digest< Hash > &digest)
Definition: digest.cpp:170
represents a logger in logging facility
Definition: logger.hpp:66
fatal (will be logged unconditionally)
const std::string & getModuleName() const
Definition: logger.hpp:73
void setLevel(LogLevel level)
Definition: logger.hpp:85
bool isLevelEnabled(LogLevel level) const
Definition: logger.hpp:79