22 #ifndef NDN_CXX_UTIL_LOGGER_HPP
23 #define NDN_CXX_UTIL_LOGGER_HPP
25 #include "ndn-cxx/detail/config.hpp"
27 #ifdef HAVE_NDN_CXX_CUSTOM_LOGGER
28 #include "ndn-cxx/util/custom-logger.hpp"
31 #include <boost/log/common.hpp>
32 #include <boost/log/expressions/keyword.hpp>
33 #include <boost/log/sources/severity_logger.hpp>
38 #include <string_view>
72 BOOST_LOG_ATTRIBUTE_KEYWORD(module,
"Module", std::string)
73 BOOST_LOG_ATTRIBUTE_KEYWORD(severity,
"Severity",
LogLevel)
83 class Logger :
public boost::log::sources::severity_logger_mt<LogLevel>
101 return m_currentLevel.load(std::memory_order_relaxed) >= level;
107 m_currentLevel.store(level, std::memory_order_relaxed);
111 const std::string m_moduleName;
112 std::atomic<LogLevel> m_currentLevel;
119 struct ExtractArgument;
121 template<
class T,
class U>
122 struct ExtractArgument<T(U&)>
127 template<
class T,
class U>
128 struct ExtractArgument<T(U)&>
134 using ArgumentType =
typename ExtractArgument<T>::type;
141 #define NDN_LOG_REGISTER_NAME(name) \
143 ::ndn::util::Logger::registerModuleName(BOOST_STRINGIZE(name)); \
148 #define NDN_LOG_INIT_FUNCTION_BODY(name) \
150 static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \
169 #define NDN_LOG_INIT(name) \
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) \
175 struct ndn_cxx_allow_trailing_semicolon
187 #define NDN_LOG_MEMBER_DECL() \
188 static ::ndn::util::Logger& ndn_cxx_getLogger(); \
190 static const bool ndn_cxx_loggerRegistration
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
215 #define NDN_LOG_MEMBER_DECL_SPECIALIZED(cls) \
217 const bool ::ndn::util::detail::ArgumentType<void(cls&)>::ndn_cxx_loggerRegistration; \
219 ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls&)>::ndn_cxx_getLogger()
232 #define NDN_LOG_MEMBER_INIT_SPECIALIZED(cls, name) \
234 const bool ::ndn::util::detail::ArgumentType<void(cls&)>::ndn_cxx_loggerRegistration = \
235 NDN_LOG_REGISTER_NAME(name); \
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
243 #define NDN_LOG_INTERNAL(lvl, expression) \
245 if (ndn_cxx_getLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \
246 BOOST_LOG_SEV(ndn_cxx_getLogger(), ::ndn::util::LogLevel::lvl) \
255 #define NDN_LOG_TRACE(expression) NDN_LOG_INTERNAL(TRACE, expression)
260 #define NDN_LOG_DEBUG(expression) NDN_LOG_INTERNAL(DEBUG, expression)
265 #define NDN_LOG_INFO(expression) NDN_LOG_INTERNAL(INFO, expression)
270 #define NDN_LOG_WARN(expression) NDN_LOG_INTERNAL(WARN, expression)
275 #define NDN_LOG_ERROR(expression) NDN_LOG_INTERNAL(ERROR, expression)
280 #define NDN_LOG_FATAL(expression) NDN_LOG_INTERNAL(FATAL, expression)
Represents a log module in the logging facility.
bool isLevelEnabled(LogLevel level) const
static void registerModuleName(const char *name)
const std::string & getModuleName() const
void setLevel(LogLevel level)
LogLevel parseLogLevel(std::string_view s)
Parse LogLevel from a string.
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
LogLevel
Indicates the severity level of a log message.
@ FATAL
fatal (will be logged unconditionally)
@ TRACE
trace messages (most verbose)
@ INFO
informational messages
@ ERROR
serious error messages