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 #include <boost/log/common.hpp>
28 #include <boost/log/sources/logger.hpp>
29 #include <atomic>
30 
31 namespace ndn {
32 namespace util {
33 
36 enum class LogLevel {
37  FATAL = -1,
38  NONE = 0,
39  ERROR = 1,
40  WARN = 2,
41  INFO = 3,
42  DEBUG = 4,
43  TRACE = 5,
44  ALL = 255
45 };
46 
50 std::ostream&
51 operator<<(std::ostream& os, LogLevel level);
52 
57 parseLogLevel(const std::string& s);
58 
62 class Logger : public boost::log::sources::logger_mt
63 {
64 public:
65  explicit
66  Logger(const std::string& name);
67 
68  const std::string&
69  getModuleName() const
70  {
71  return m_moduleName;
72  }
73 
74  bool
75  isLevelEnabled(LogLevel level) const
76  {
77  return m_currentLevel.load(std::memory_order_relaxed) >= level;
78  }
79 
80  void
82  {
83  m_currentLevel.store(level, std::memory_order_relaxed);
84  }
85 
86 private:
87  const std::string m_moduleName;
88  std::atomic<LogLevel> m_currentLevel;
89 };
90 
93 #define NDN_LOG_INIT(name) \
94  namespace { \
95  inline ::ndn::util::Logger& getNdnCxxLogger() \
96  { \
97  static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \
98  return logger; \
99  } \
100  } \
101  struct ndn_cxx__allow_trailing_semicolon
102 
109 {
110 };
111 
115 std::ostream&
116 operator<<(std::ostream& os, const LoggerTimestamp&);
117 
118 #if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106000)
119 // workaround Boost bug 11549
120 #define NDN_BOOST_LOG(x) BOOST_LOG(x) << ""
121 #else
122 #define NDN_BOOST_LOG(x) BOOST_LOG(x)
123 #endif
124 
125 #define NDN_LOG(lvl, lvlstr, expression) \
126  do { \
127  if (getNdnCxxLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \
128  NDN_BOOST_LOG(getNdnCxxLogger()) << ::ndn::util::LoggerTimestamp{} \
129  << " " BOOST_STRINGIZE(lvlstr) ": [" << getNdnCxxLogger().getModuleName() << "] " \
130  << expression; \
131  } \
132  } while (false)
133 
137 #define NDN_LOG_TRACE(expression) NDN_LOG(TRACE, TRACE, expression)
138 
142 #define NDN_LOG_DEBUG(expression) NDN_LOG(DEBUG, DEBUG, expression)
143 
147 #define NDN_LOG_INFO(expression) NDN_LOG(INFO, INFO, expression)
148 
152 #define NDN_LOG_WARN(expression) NDN_LOG(WARN, WARNING, expression)
153 
157 #define NDN_LOG_ERROR(expression) NDN_LOG(ERROR, ERROR, expression)
158 
162 #define NDN_LOG_FATAL(expression) NDN_LOG(FATAL, FATAL, expression)
163 
164 } // namespace util
165 } // namespace ndn
166 
167 #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:108
informational messages
LogLevel
indicates the severity level of a log message
Definition: logger.hpp:36
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:160
represents a logger in logging facility
Definition: logger.hpp:62
fatal (will be logged unconditionally)
const std::string & getModuleName() const
Definition: logger.hpp:69
void setLevel(LogLevel level)
Definition: logger.hpp:81
bool isLevelEnabled(LogLevel level) const
Definition: logger.hpp:75