blob: 87e317bffae795fbefe8db858b1cc8f988e55d8c [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.
64 * \note New loggers should be defined using #NDN_LOG_INIT or #NDN_LOG_MEMBER_INIT.
Junxiao Shi7d054272016-08-04 17:00:41 +000065 */
66class Logger : public boost::log::sources::logger_mt
67{
68public:
69 explicit
70 Logger(const std::string& name);
71
72 const std::string&
73 getModuleName() const
74 {
75 return m_moduleName;
76 }
77
78 bool
79 isLevelEnabled(LogLevel level) const
80 {
81 return m_currentLevel.load(std::memory_order_relaxed) >= level;
82 }
83
84 void
85 setLevel(LogLevel level)
86 {
87 m_currentLevel.store(level, std::memory_order_relaxed);
88 }
89
90private:
91 const std::string m_moduleName;
92 std::atomic<LogLevel> m_currentLevel;
93};
94
Davide Pesavento0c145ec2018-04-10 21:33:57 -040095namespace detail {
Junxiao Shi7d054272016-08-04 17:00:41 +000096
Davide Pesavento0c145ec2018-04-10 21:33:57 -040097/** \brief A tag type used to output a timestamp to a stream.
Junxiao Shi7d054272016-08-04 17:00:41 +000098 * \code
99 * std::clog << LoggerTimestamp();
100 * \endcode
101 */
102struct LoggerTimestamp
103{
104};
105
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400106/** \brief Write a timestamp to \p os.
Junxiao Shi7d054272016-08-04 17:00:41 +0000107 * \note This function is thread-safe.
108 */
109std::ostream&
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400110operator<<(std::ostream& os, LoggerTimestamp);
Junxiao Shi7d054272016-08-04 17:00:41 +0000111
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400112/** \cond */
113template<class T>
114struct ExtractArgument;
115
116template<class T, class U>
117struct ExtractArgument<T(U)>
118{
119 using type = U;
120};
121
122template<class T>
123using ArgumentType = typename ExtractArgument<T>::type;
124/** \endcond */
125
126} // namespace detail
127
128/** \cond */
129#define NDN_LOG_INIT_FUNCTION_BODY(name) \
130 { \
131 static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \
132 return logger; \
133 }
134/** \endcond */
135
136/** \brief Define a non-member log module.
137 *
138 * This macro can be used in global scope to define a log module for an entire translation
139 * unit, or in namespace scope to define a log module for the enclosing namespace.
140 * Use #NDN_LOG_MEMBER_INIT to define a log module as a class or struct member.
141 *
142 * \warning Do not use this macro in header files unless you know what you're doing,
143 * as it can easily trigger ODR violations if used incorrectly.
144 *
145 * \param name the logger name
146 * \note The logger name is restricted to alphanumeric characters and a select set of
147 * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with
148 * a dot (`.`), or contain multiple consecutive dots.
149 */
150#define NDN_LOG_INIT(name) \
151 namespace { \
152 ::ndn::util::Logger& ndn_cxx_getLogger() \
153 NDN_LOG_INIT_FUNCTION_BODY(name) \
154 } \
155 struct ndn_cxx_allow_trailing_semicolon
156
157/** \brief Define a member log module.
158 *
159 * This macro should only be used to define a log module as a class or struct member.
Davide Pesaventod95274b2018-04-12 17:42:20 -0400160 * It is recommended to place this macro in the private or protected section of the
161 * class or struct definition. Use #NDN_LOG_INIT to define a non-member log module.
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400162 *
163 * \param name the logger name
164 * \note The logger name is restricted to alphanumeric characters and a select set of
165 * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with
166 * a dot (`.`), or contain multiple consecutive dots.
167 */
168#define NDN_LOG_MEMBER_INIT(name) \
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400169 static ::ndn::util::Logger& ndn_cxx_getLogger() \
170 NDN_LOG_INIT_FUNCTION_BODY(name) \
171 struct ndn_cxx_allow_trailing_semicolon
172
173/** \brief Forward-declare a member log module, without fully defining it.
174 *
175 * This macro can be used to declare a log module as a member of a class template.
176 * Use this macro in conjunction with #NDN_LOG_MEMBER_DECL_SPECIALIZED and
177 * #NDN_LOG_MEMBER_INIT_SPECIALIZED to provide different loggers for different
178 * template specializations.
179 */
180#define NDN_LOG_MEMBER_DECL() \
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400181 static ::ndn::util::Logger& ndn_cxx_getLogger()
182
183/** \brief Declare an explicit specialization of a member log module of a class template.
184 *
185 * \param cls fully specialized class name; wrap in parentheses if it contains commas
186 */
187#define NDN_LOG_MEMBER_DECL_SPECIALIZED(cls) \
Davide Pesaventod95274b2018-04-12 17:42:20 -0400188 template<> \
189 ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger()
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400190
191/** \brief Define an explicit specialization of a member log module of a class template.
192 *
193 * \param cls fully specialized class name; wrap in parentheses if it contains commas
194 * \param name the logger name
195 * \note The logger name is restricted to alphanumeric characters and a select set of
196 * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with
197 * a dot (`.`), or contain multiple consecutive dots.
198 */
199#define NDN_LOG_MEMBER_INIT_SPECIALIZED(cls, name) \
Davide Pesaventod95274b2018-04-12 17:42:20 -0400200 template<> inline \
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
205/** \cond */
206#if BOOST_VERSION == 105900
Junxiao Shi7d054272016-08-04 17:00:41 +0000207// workaround Boost bug 11549
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000208#define NDN_BOOST_LOG(x) BOOST_LOG(x) << ""
Junxiao Shi7d054272016-08-04 17:00:41 +0000209#else
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000210#define NDN_BOOST_LOG(x) BOOST_LOG(x)
Junxiao Shi7d054272016-08-04 17:00:41 +0000211#endif
212
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400213#define NDN_LOG_INTERNAL(lvl, lvlstr, expression) \
Junxiao Shi7d054272016-08-04 17:00:41 +0000214 do { \
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400215 if (ndn_cxx_getLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \
216 NDN_BOOST_LOG(ndn_cxx_getLogger()) << ::ndn::util::detail::LoggerTimestamp{} \
217 << " " BOOST_STRINGIZE(lvlstr) ": [" << ndn_cxx_getLogger().getModuleName() << "] " \
Junxiao Shi7d054272016-08-04 17:00:41 +0000218 << expression; \
219 } \
220 } while (false)
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400221/** \endcond */
Junxiao Shi7d054272016-08-04 17:00:41 +0000222
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400223/** \brief Log at TRACE level.
224 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000225 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400226#define NDN_LOG_TRACE(expression) NDN_LOG_INTERNAL(TRACE, TRACE, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000227
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400228/** \brief Log at DEBUG level.
229 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000230 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400231#define NDN_LOG_DEBUG(expression) NDN_LOG_INTERNAL(DEBUG, DEBUG, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000232
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400233/** \brief Log at INFO level.
234 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000235 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400236#define NDN_LOG_INFO(expression) NDN_LOG_INTERNAL(INFO, INFO, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000237
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400238/** \brief Log at WARN level.
239 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000240 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400241#define NDN_LOG_WARN(expression) NDN_LOG_INTERNAL(WARN, WARNING, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000242
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400243/** \brief Log at ERROR level.
244 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000245 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400246#define NDN_LOG_ERROR(expression) NDN_LOG_INTERNAL(ERROR, ERROR, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000247
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400248/** \brief Log at FATAL level.
249 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000250 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400251#define NDN_LOG_FATAL(expression) NDN_LOG_INTERNAL(FATAL, FATAL, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000252
253} // namespace util
254} // namespace ndn
255
Alexander Afanasyeved1e99d2016-11-05 09:59:35 -0600256#endif // HAVE_NDN_CXX_CUSTOM_LOGGER
257
Junxiao Shi7d054272016-08-04 17:00:41 +0000258#endif // NDN_UTIL_LOGGER_HPP