blob: 62443a83e5aa42433d6415ef8b091c6f5d8054bd [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 Pesavento0c145ec2018-04-10 21:33:57 -040051/** \brief Output LogLevel as 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 Pesavento0c145ec2018-04-10 21:33:57 -040057/** \brief Parse LogLevel from 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.
160 * Use #NDN_LOG_INIT to define a non-member log module.
161 *
162 * \param name the logger name
163 * \note The logger name is restricted to alphanumeric characters and a select set of
164 * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with
165 * a dot (`.`), or contain multiple consecutive dots.
166 */
167#define NDN_LOG_MEMBER_INIT(name) \
168 private: \
169 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() \
181 private: \
182 static ::ndn::util::Logger& ndn_cxx_getLogger()
183
184/** \brief Declare an explicit specialization of a member log module of a class template.
185 *
186 * \param cls fully specialized class name; wrap in parentheses if it contains commas
187 */
188#define NDN_LOG_MEMBER_DECL_SPECIALIZED(cls) \
189 template<> ::ndn::util::Logger& detail::ArgumentType<void(cls)>::ndn_cxx_getLogger()
190
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) \
200 template<> inline ::ndn::util::Logger& detail::ArgumentType<void(cls)>::ndn_cxx_getLogger() \
201 NDN_LOG_INIT_FUNCTION_BODY(name) \
202 struct ndn_cxx_allow_trailing_semicolon
203
204/** \cond */
205#if BOOST_VERSION == 105900
Junxiao Shi7d054272016-08-04 17:00:41 +0000206// workaround Boost bug 11549
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000207#define NDN_BOOST_LOG(x) BOOST_LOG(x) << ""
Junxiao Shi7d054272016-08-04 17:00:41 +0000208#else
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000209#define NDN_BOOST_LOG(x) BOOST_LOG(x)
Junxiao Shi7d054272016-08-04 17:00:41 +0000210#endif
211
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400212#define NDN_LOG_INTERNAL(lvl, lvlstr, expression) \
Junxiao Shi7d054272016-08-04 17:00:41 +0000213 do { \
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400214 if (ndn_cxx_getLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \
215 NDN_BOOST_LOG(ndn_cxx_getLogger()) << ::ndn::util::detail::LoggerTimestamp{} \
216 << " " BOOST_STRINGIZE(lvlstr) ": [" << ndn_cxx_getLogger().getModuleName() << "] " \
Junxiao Shi7d054272016-08-04 17:00:41 +0000217 << expression; \
218 } \
219 } while (false)
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400220/** \endcond */
Junxiao Shi7d054272016-08-04 17:00:41 +0000221
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400222/** \brief Log at TRACE level.
223 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000224 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400225#define NDN_LOG_TRACE(expression) NDN_LOG_INTERNAL(TRACE, TRACE, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000226
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400227/** \brief Log at DEBUG level.
228 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000229 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400230#define NDN_LOG_DEBUG(expression) NDN_LOG_INTERNAL(DEBUG, DEBUG, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000231
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400232/** \brief Log at INFO level.
233 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000234 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400235#define NDN_LOG_INFO(expression) NDN_LOG_INTERNAL(INFO, INFO, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000236
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400237/** \brief Log at WARN level.
238 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000239 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400240#define NDN_LOG_WARN(expression) NDN_LOG_INTERNAL(WARN, WARNING, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000241
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400242/** \brief Log at ERROR level.
243 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000244 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400245#define NDN_LOG_ERROR(expression) NDN_LOG_INTERNAL(ERROR, ERROR, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000246
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400247/** \brief Log at FATAL level.
248 * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
Junxiao Shi7d054272016-08-04 17:00:41 +0000249 */
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400250#define NDN_LOG_FATAL(expression) NDN_LOG_INTERNAL(FATAL, FATAL, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000251
252} // namespace util
253} // namespace ndn
254
Alexander Afanasyeved1e99d2016-11-05 09:59:35 -0600255#endif // HAVE_NDN_CXX_CUSTOM_LOGGER
256
Junxiao Shi7d054272016-08-04 17:00:41 +0000257#endif // NDN_UTIL_LOGGER_HPP