blob: 75d86b44221c84be00c4dbc369eedc26c402f38a [file] [log] [blame]
Junxiao Shi7d054272016-08-04 17:00:41 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
dmcoomese062a182017-06-12 11:10:31 -05002/*
Davide Pesavento0c145ec2018-04-10 21:33:57 -04003 * Copyright (c) 2013-2018 Regents of the University of California.
Junxiao Shi7d054272016-08-04 17:00:41 +00004 *
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
25#include "../common.hpp"
26
Alexander Afanasyeved1e99d2016-11-05 09:59:35 -060027#ifdef HAVE_NDN_CXX_CUSTOM_LOGGER
28#include "ndn-cxx-custom-logger.hpp"
29#else
30
Junxiao Shi7d054272016-08-04 17:00:41 +000031#include <boost/log/common.hpp>
32#include <boost/log/sources/logger.hpp>
33#include <atomic>
34
35namespace ndn {
36namespace util {
37
Davide Pesavento0c145ec2018-04-10 21:33:57 -040038/** \brief Indicates the severity level of a log message.
Junxiao Shi7d054272016-08-04 17:00:41 +000039 */
40enum class LogLevel {
41 FATAL = -1, ///< fatal (will be logged unconditionally)
42 NONE = 0, ///< no messages
43 ERROR = 1, ///< serious error messages
44 WARN = 2, ///< warning messages
45 INFO = 3, ///< informational messages
46 DEBUG = 4, ///< debug messages
47 TRACE = 5, ///< trace messages (most verbose)
48 ALL = 255 ///< all messages
49};
50
Davide Pesaventod95274b2018-04-12 17:42:20 -040051/** \brief Output LogLevel as a string.
Junxiao Shi7d054272016-08-04 17:00:41 +000052 * \throw std::invalid_argument unknown \p level
53 */
54std::ostream&
55operator<<(std::ostream& os, LogLevel level);
56
Davide Pesaventod95274b2018-04-12 17:42:20 -040057/** \brief Parse LogLevel from a string.
Junxiao Shi7d054272016-08-04 17:00:41 +000058 * \throw std::invalid_argument unknown level name
59 */
60LogLevel
61parseLogLevel(const std::string& s);
62
Davide Pesavento0c145ec2018-04-10 21:33:57 -040063/** \brief Represents a log module in the logging facility.
Davide Pesavento1c9c96c2018-04-25 10:45:08 -040064 *
65 * \note Normally, loggers should be defined using #NDN_LOG_INIT, #NDN_LOG_MEMBER_INIT,
66 * or #NDN_LOG_MEMBER_INIT_SPECIALIZED.
Junxiao Shi7d054272016-08-04 17:00:41 +000067 */
68class Logger : public boost::log::sources::logger_mt
69{
70public:
71 explicit
Davide Pesavento1c9c96c2018-04-25 10:45:08 -040072 Logger(const char* name);
73
74 static void
75 registerModuleName(const char* name);
Junxiao Shi7d054272016-08-04 17:00:41 +000076
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
90 setLevel(LogLevel level)
91 {
92 m_currentLevel.store(level, std::memory_order_relaxed);
93 }
94
95private:
96 const std::string m_moduleName;
97 std::atomic<LogLevel> m_currentLevel;
98};
99
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400100namespace detail {
Junxiao Shi7d054272016-08-04 17:00:41 +0000101
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400102/** \brief A tag type used to output a timestamp to a stream.
Junxiao Shi7d054272016-08-04 17:00:41 +0000103 * \code
104 * std::clog << LoggerTimestamp();
105 * \endcode
106 */
107struct LoggerTimestamp
108{
109};
110
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400111/** \brief Write a timestamp to \p os.
Junxiao Shi7d054272016-08-04 17:00:41 +0000112 * \note This function is thread-safe.
113 */
114std::ostream&
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400115operator<<(std::ostream& os, LoggerTimestamp);
Junxiao Shi7d054272016-08-04 17:00:41 +0000116
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400117/** \cond */
118template<class T>
119struct ExtractArgument;
120
121template<class T, class U>
122struct ExtractArgument<T(U)>
123{
124 using type = U;
125};
126
127template<class T>
128using ArgumentType = typename ExtractArgument<T>::type;
129/** \endcond */
130
131} // namespace detail
132
133/** \cond */
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400134// 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
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400142#define NDN_LOG_INIT_FUNCTION_BODY(name) \
143 { \
144 static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \
145 return logger; \
146 }
147/** \endcond */
148
149/** \brief Define a non-member log module.
150 *
151 * This macro can be used in global scope to define a log module for an entire translation
152 * unit, or in namespace scope to define a log module for the enclosing namespace.
153 * Use #NDN_LOG_MEMBER_INIT to define a log module as a class or struct member.
154 *
155 * \warning Do not use this macro in header files unless you know what you're doing,
156 * as it can easily trigger ODR violations if used incorrectly.
157 *
158 * \param name the logger name
159 * \note The logger name is restricted to alphanumeric characters and a select set of
160 * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with
161 * a dot (`.`), or contain multiple consecutive dots.
162 */
163#define NDN_LOG_INIT(name) \
164 namespace { \
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400165 const bool ndn_cxx_loggerRegistration __attribute__((used)) = NDN_LOG_REGISTER_NAME(name); \
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400166 ::ndn::util::Logger& ndn_cxx_getLogger() \
167 NDN_LOG_INIT_FUNCTION_BODY(name) \
168 } \
169 struct ndn_cxx_allow_trailing_semicolon
170
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400171/** \brief Declare a member log module, without initializing it.
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400172 *
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400173 * This macro should only be used to declare a log module as a class or struct member.
Davide Pesaventod95274b2018-04-12 17:42:20 -0400174 * It is recommended to place this macro in the private or protected section of the
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400175 * class or struct definition. Use #NDN_LOG_INIT to declare a non-member log module.
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400176 *
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400177 * If the enclosing class is a template, this macro can be used in conjunction with
178 * #NDN_LOG_MEMBER_DECL_SPECIALIZED and #NDN_LOG_MEMBER_INIT_SPECIALIZED to provide
179 * different loggers for different template specializations.
180 */
181#define NDN_LOG_MEMBER_DECL() \
182 static ::ndn::util::Logger& ndn_cxx_getLogger(); \
183 private: \
184 static const bool ndn_cxx_loggerRegistration
185
186/** \brief Initialize a member log module.
187 *
188 * This macro should only be used to initialize a previously declared member log module.
189 * It must be placed in a .cpp file (NOT in a header file), in the same namespace as
190 * the class or struct that contains the log module.
191 *
192 * \param cls class name; wrap in parentheses if it contains commas
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400193 * \param name the logger name
194 * \note The logger name is restricted to alphanumeric characters and a select set of
195 * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with
196 * a dot (`.`), or contain multiple consecutive dots.
197 */
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400198#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() \
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400202 NDN_LOG_INIT_FUNCTION_BODY(name) \
203 struct ndn_cxx_allow_trailing_semicolon
204
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400205/** \brief Declare an explicit specialization of a member log module of a class template.
206 *
207 * \param cls fully specialized class name; wrap in parentheses if it contains commas
208 */
209#define NDN_LOG_MEMBER_DECL_SPECIALIZED(cls) \
Davide Pesaventod95274b2018-04-12 17:42:20 -0400210 template<> \
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400211 const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration; \
212 template<> \
Davide Pesaventod95274b2018-04-12 17:42:20 -0400213 ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger()
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400214
215/** \brief Define an explicit specialization of a member log module of a class template.
216 *
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400217 * This macro must be placed in a .cpp file (NOT in a header file), in the same namespace
218 * as the class template that contains the log module.
219 *
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400220 * \param cls fully specialized class name; wrap in parentheses if it contains commas
221 * \param name the logger name
222 * \note The logger name is restricted to alphanumeric characters and a select set of
223 * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with
224 * a dot (`.`), or contain multiple consecutive dots.
225 */
226#define NDN_LOG_MEMBER_INIT_SPECIALIZED(cls, name) \
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400227 template<> \
228 const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration = \
229 NDN_LOG_REGISTER_NAME(name); \
230 template<> \
Davide Pesaventod95274b2018-04-12 17:42:20 -0400231 ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger() \
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400232 NDN_LOG_INIT_FUNCTION_BODY(name) \
233 struct ndn_cxx_allow_trailing_semicolon
234
235/** \cond */
236#if BOOST_VERSION == 105900
Junxiao Shi7d054272016-08-04 17:00:41 +0000237// workaround Boost bug 11549
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000238#define NDN_BOOST_LOG(x) BOOST_LOG(x) << ""
Junxiao Shi7d054272016-08-04 17:00:41 +0000239#else
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000240#define NDN_BOOST_LOG(x) BOOST_LOG(x)
Junxiao Shi7d054272016-08-04 17:00:41 +0000241#endif
242
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400243// implementation detail
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400244#define NDN_LOG_INTERNAL(lvl, lvlstr, expression) \
Junxiao Shi7d054272016-08-04 17:00:41 +0000245 do { \
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400246 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() << "] " \
Junxiao Shi7d054272016-08-04 17:00:41 +0000249 << expression; \
250 } \
251 } while (false)
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400252/** \endcond */
Junxiao Shi7d054272016-08-04 17:00:41 +0000253
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400254/** \brief Log at TRACE level.
255 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000256 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400257#define NDN_LOG_TRACE(expression) NDN_LOG_INTERNAL(TRACE, TRACE, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000258
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400259/** \brief Log at DEBUG level.
260 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000261 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400262#define NDN_LOG_DEBUG(expression) NDN_LOG_INTERNAL(DEBUG, DEBUG, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000263
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400264/** \brief Log at INFO level.
265 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000266 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400267#define NDN_LOG_INFO(expression) NDN_LOG_INTERNAL(INFO, INFO, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000268
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400269/** \brief Log at WARN level.
270 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000271 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400272#define NDN_LOG_WARN(expression) NDN_LOG_INTERNAL(WARN, WARNING, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000273
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400274/** \brief Log at ERROR level.
275 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000276 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400277#define NDN_LOG_ERROR(expression) NDN_LOG_INTERNAL(ERROR, ERROR, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000278
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400279/** \brief Log at FATAL level.
280 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000281 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400282#define NDN_LOG_FATAL(expression) NDN_LOG_INTERNAL(FATAL, FATAL, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000283
284} // namespace util
285} // namespace ndn
286
Alexander Afanasyeved1e99d2016-11-05 09:59:35 -0600287#endif // HAVE_NDN_CXX_CUSTOM_LOGGER
288
Junxiao Shi7d054272016-08-04 17:00:41 +0000289#endif // NDN_UTIL_LOGGER_HPP