logger.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_UTIL_LOGGER_HPP
23 #define NDN_UTIL_LOGGER_HPP
24 
26 
27 #ifdef HAVE_NDN_CXX_CUSTOM_LOGGER
28 #include "ndn-cxx/util/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 
68 class Logger : public boost::log::sources::logger_mt
69 {
70 public:
71  explicit
72  Logger(const char* name);
73 
74  static void
75  registerModuleName(const char* name);
76 
77  const std::string&
78  getModuleName() const
79  {
80  return m_moduleName;
81  }
82 
83  bool
84  isLevelEnabled(LogLevel level) const
85  {
86  return m_currentLevel.load(std::memory_order_relaxed) >= level;
87  }
88 
89  void
91  {
92  m_currentLevel.store(level, std::memory_order_relaxed);
93  }
94 
95 private:
96  const std::string m_moduleName;
97  std::atomic<LogLevel> m_currentLevel;
98 };
99 
100 namespace detail {
101 
108 {
109 };
110 
114 std::ostream&
115 operator<<(std::ostream& os, LoggerTimestamp);
116 
118 template<class T>
119 struct ExtractArgument;
120 
121 template<class T, class U>
122 struct ExtractArgument<T(U)>
123 {
124  using type = U;
125 };
126 
127 template<class T>
128 using ArgumentType = typename ExtractArgument<T>::type;
131 } // namespace detail
132 
134 // implementation detail
135 #define NDN_LOG_REGISTER_NAME(name) \
136  []() -> bool { \
137  ::ndn::util::Logger::registerModuleName(BOOST_STRINGIZE(name)); \
138  return true; \
139  }()
140 
141 // implementation detail
142 #define NDN_LOG_INIT_FUNCTION_BODY(name) \
143  { \
144  static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \
145  return logger; \
146  }
147 
163 #define NDN_LOG_INIT(name) \
164  namespace { \
165  const bool ndn_cxx_loggerRegistration __attribute__((used)) = NDN_LOG_REGISTER_NAME(name); \
166  ::ndn::util::Logger& ndn_cxx_getLogger() \
167  NDN_LOG_INIT_FUNCTION_BODY(name) \
168  } \
169  struct ndn_cxx_allow_trailing_semicolon
170 
181 #define NDN_LOG_MEMBER_DECL() \
182  static ::ndn::util::Logger& ndn_cxx_getLogger(); \
183  private: \
184  static const bool ndn_cxx_loggerRegistration
185 
198 #define NDN_LOG_MEMBER_INIT(cls, name) \
199  const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration = \
200  NDN_LOG_REGISTER_NAME(name); \
201  ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger() \
202  NDN_LOG_INIT_FUNCTION_BODY(name) \
203  struct ndn_cxx_allow_trailing_semicolon
204 
209 #define NDN_LOG_MEMBER_DECL_SPECIALIZED(cls) \
210  template<> \
211  const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration; \
212  template<> \
213  ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger()
214 
226 #define NDN_LOG_MEMBER_INIT_SPECIALIZED(cls, name) \
227  template<> \
228  const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration = \
229  NDN_LOG_REGISTER_NAME(name); \
230  template<> \
231  ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger() \
232  NDN_LOG_INIT_FUNCTION_BODY(name) \
233  struct ndn_cxx_allow_trailing_semicolon
234 
236 #if BOOST_VERSION == 105900
237 // workaround Boost bug 11549
238 #define NDN_BOOST_LOG(x) BOOST_LOG(x) << ""
239 #else
240 #define NDN_BOOST_LOG(x) BOOST_LOG(x)
241 #endif
242 
243 // implementation detail
244 #define NDN_LOG_INTERNAL(lvl, lvlstr, expression) \
245  do { \
246  if (ndn_cxx_getLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \
247  NDN_BOOST_LOG(ndn_cxx_getLogger()) << ::ndn::util::detail::LoggerTimestamp{} \
248  << " " BOOST_STRINGIZE(lvlstr) ": [" << ndn_cxx_getLogger().getModuleName() << "] " \
249  << expression; \
250  } \
251  } while (false)
252 
257 #define NDN_LOG_TRACE(expression) NDN_LOG_INTERNAL(TRACE, TRACE, expression)
258 
262 #define NDN_LOG_DEBUG(expression) NDN_LOG_INTERNAL(DEBUG, DEBUG, expression)
263 
267 #define NDN_LOG_INFO(expression) NDN_LOG_INTERNAL(INFO, INFO, expression)
268 
272 #define NDN_LOG_WARN(expression) NDN_LOG_INTERNAL(WARN, WARNING, expression)
273 
277 #define NDN_LOG_ERROR(expression) NDN_LOG_INTERNAL(ERROR, ERROR, expression)
278 
282 #define NDN_LOG_FATAL(expression) NDN_LOG_INTERNAL(FATAL, FATAL, expression)
283 
284 } // namespace util
285 } // namespace ndn
286 
287 #endif // HAVE_NDN_CXX_CUSTOM_LOGGER
288 
289 #endif // NDN_UTIL_LOGGER_HPP
Definition: data.cpp:26
trace messages (most verbose)
serious error messages
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
Definition: logger.cpp:35
informational messages
LogLevel
Indicates the severity level of a log message.
Definition: logger.hpp:40
import common constructs for ndn-cxx library internal use
LogLevel parseLogLevel(const std::string &s)
Parse LogLevel from a string.
Definition: logger.cpp:60
A tag type used to output a timestamp to a stream.
Definition: logger.hpp:107
warning messages
Represents a log module in the logging facility.
Definition: logger.hpp:68
fatal (will be logged unconditionally)
const std::string & getModuleName() const
Definition: logger.hpp:78
void setLevel(LogLevel level)
Definition: logger.hpp:90
bool isLevelEnabled(LogLevel level) const
Definition: logger.hpp:84