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-2024 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_CXX_UTIL_LOGGER_HPP
23 #define NDN_CXX_UTIL_LOGGER_HPP
24 
25 #include "ndn-cxx/detail/config.hpp"
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/expressions/keyword.hpp>
33 #include <boost/log/sources/severity_logger.hpp>
34 
35 #include <atomic>
36 #include <iosfwd>
37 #include <string>
38 #include <string_view>
39 
40 namespace ndn::util {
41 
45 enum class LogLevel {
46  FATAL = -1,
47  NONE = 0,
48  ERROR = 1,
49  WARN = 2,
50  INFO = 3,
51  DEBUG = 4,
52  TRACE = 5,
53  ALL = 255
54 };
55 
60 std::ostream&
61 operator<<(std::ostream& os, LogLevel level);
62 
68 parseLogLevel(std::string_view s);
69 
70 namespace log {
71 
72 BOOST_LOG_ATTRIBUTE_KEYWORD(module, "Module", std::string)
73 BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", LogLevel)
74 
75 } // namespace log
76 
83 class Logger : public boost::log::sources::severity_logger_mt<LogLevel>
84 {
85 public:
86  explicit
87  Logger(const char* name);
88 
89  static void
90  registerModuleName(const char* name);
91 
92  const std::string&
93  getModuleName() const
94  {
95  return m_moduleName;
96  }
97 
98  bool
99  isLevelEnabled(LogLevel level) const
100  {
101  return m_currentLevel.load(std::memory_order_relaxed) >= level;
102  }
103 
104  void
106  {
107  m_currentLevel.store(level, std::memory_order_relaxed);
108  }
109 
110 private:
111  const std::string m_moduleName;
112  std::atomic<LogLevel> m_currentLevel;
113 };
114 
115 namespace detail {
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, class U>
128 struct ExtractArgument<T(U)&>
129 {
130  using type = U;
131 };
132 
133 template<class T>
134 using ArgumentType = typename ExtractArgument<T>::type;
137 } // namespace detail
138 
140 // implementation detail
141 #define NDN_LOG_REGISTER_NAME(name) \
142  []() -> bool { \
143  ::ndn::util::Logger::registerModuleName(BOOST_STRINGIZE(name)); \
144  return true; \
145  }()
146 
147 // implementation detail
148 #define NDN_LOG_INIT_FUNCTION_BODY(name) \
149  { \
150  static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \
151  return logger; \
152  }
169 #define NDN_LOG_INIT(name) \
170  namespace { \
171  const bool ndn_cxx_loggerRegistration __attribute__((used)) = NDN_LOG_REGISTER_NAME(name); \
172  ::ndn::util::Logger& ndn_cxx_getLogger() \
173  NDN_LOG_INIT_FUNCTION_BODY(name) \
174  } \
175  struct ndn_cxx_allow_trailing_semicolon
176 
187 #define NDN_LOG_MEMBER_DECL() \
188  static ::ndn::util::Logger& ndn_cxx_getLogger(); \
189  private: \
190  static const bool ndn_cxx_loggerRegistration
191 
204 #define NDN_LOG_MEMBER_INIT(cls, name) \
205  const bool ::ndn::util::detail::ArgumentType<void(cls&)>::ndn_cxx_loggerRegistration = \
206  NDN_LOG_REGISTER_NAME(name); \
207  ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls&)>::ndn_cxx_getLogger() \
208  NDN_LOG_INIT_FUNCTION_BODY(name) \
209  struct ndn_cxx_allow_trailing_semicolon
210 
215 #define NDN_LOG_MEMBER_DECL_SPECIALIZED(cls) \
216  template<> \
217  const bool ::ndn::util::detail::ArgumentType<void(cls&)>::ndn_cxx_loggerRegistration; \
218  template<> \
219  ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls&)>::ndn_cxx_getLogger()
220 
232 #define NDN_LOG_MEMBER_INIT_SPECIALIZED(cls, name) \
233  template<> \
234  const bool ::ndn::util::detail::ArgumentType<void(cls&)>::ndn_cxx_loggerRegistration = \
235  NDN_LOG_REGISTER_NAME(name); \
236  template<> \
237  ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls&)>::ndn_cxx_getLogger() \
238  NDN_LOG_INIT_FUNCTION_BODY(name) \
239  struct ndn_cxx_allow_trailing_semicolon
240 
242 // implementation detail
243 #define NDN_LOG_INTERNAL(lvl, expression) \
244  do { \
245  if (ndn_cxx_getLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \
246  BOOST_LOG_SEV(ndn_cxx_getLogger(), ::ndn::util::LogLevel::lvl) \
247  << expression; \
248  } \
249  } while (false)
255 #define NDN_LOG_TRACE(expression) NDN_LOG_INTERNAL(TRACE, expression)
256 
260 #define NDN_LOG_DEBUG(expression) NDN_LOG_INTERNAL(DEBUG, expression)
261 
265 #define NDN_LOG_INFO(expression) NDN_LOG_INTERNAL(INFO, expression)
266 
270 #define NDN_LOG_WARN(expression) NDN_LOG_INTERNAL(WARN, expression)
271 
275 #define NDN_LOG_ERROR(expression) NDN_LOG_INTERNAL(ERROR, expression)
276 
280 #define NDN_LOG_FATAL(expression) NDN_LOG_INTERNAL(FATAL, expression)
281 
282 } // namespace ndn::util
283 
284 #endif // HAVE_NDN_CXX_CUSTOM_LOGGER
285 
286 #endif // NDN_CXX_UTIL_LOGGER_HPP
Represents a log module in the logging facility.
Definition: logger.hpp:84
Logger(const char *name)
Definition: logger.cpp:87
bool isLevelEnabled(LogLevel level) const
Definition: logger.hpp:99
static void registerModuleName(const char *name)
Definition: logger.cpp:99
const std::string & getModuleName() const
Definition: logger.hpp:93
void setLevel(LogLevel level)
Definition: logger.hpp:105
LogLevel parseLogLevel(std::string_view s)
Parse LogLevel from a string.
Definition: logger.cpp:53
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
Definition: logger.cpp:28
LogLevel
Indicates the severity level of a log message.
Definition: logger.hpp:45
@ FATAL
fatal (will be logged unconditionally)
@ TRACE
trace messages (most verbose)
@ WARN
warning messages
@ INFO
informational messages
@ ALL
all messages
@ NONE
no messages
@ ERROR
serious error messages
@ DEBUG
debug messages