blob: 4f0c35b7d3a319c307a3ed583997b230d1d8ae5c [file] [log] [blame]
Junxiao Shi7d054272016-08-04 17:00:41 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 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_UTIL_LOGGER_HPP
23#define NDN_UTIL_LOGGER_HPP
24
25#include "../common.hpp"
26
27#include <boost/log/common.hpp>
28#include <boost/log/sources/logger.hpp>
29#include <atomic>
30
31namespace ndn {
32namespace util {
33
34/** \brief indicates the severity level of a log message
35 */
36enum class LogLevel {
37 FATAL = -1, ///< fatal (will be logged unconditionally)
38 NONE = 0, ///< no messages
39 ERROR = 1, ///< serious error messages
40 WARN = 2, ///< warning messages
41 INFO = 3, ///< informational messages
42 DEBUG = 4, ///< debug messages
43 TRACE = 5, ///< trace messages (most verbose)
44 ALL = 255 ///< all messages
45};
46
47/** \brief output LogLevel as string
48 * \throw std::invalid_argument unknown \p level
49 */
50std::ostream&
51operator<<(std::ostream& os, LogLevel level);
52
53/** \brief parse LogLevel from string
54 * \throw std::invalid_argument unknown level name
55 */
56LogLevel
57parseLogLevel(const std::string& s);
58
59/** \brief represents a logger in logging facility
Junxiao Shi1fe7ce52016-08-08 05:48:02 +000060 * \note User should declare a new logger with \p NDN_LOG_INIT macro.
Junxiao Shi7d054272016-08-04 17:00:41 +000061 */
62class Logger : public boost::log::sources::logger_mt
63{
64public:
65 explicit
66 Logger(const std::string& name);
67
68 const std::string&
69 getModuleName() const
70 {
71 return m_moduleName;
72 }
73
74 bool
75 isLevelEnabled(LogLevel level) const
76 {
77 return m_currentLevel.load(std::memory_order_relaxed) >= level;
78 }
79
80 void
81 setLevel(LogLevel level)
82 {
83 m_currentLevel.store(level, std::memory_order_relaxed);
84 }
85
86private:
87 const std::string m_moduleName;
88 std::atomic<LogLevel> m_currentLevel;
89};
90
91/** \brief declare a log module
92 */
Junxiao Shi1fe7ce52016-08-08 05:48:02 +000093#define NDN_LOG_INIT(name) \
Junxiao Shi7d054272016-08-04 17:00:41 +000094 namespace { \
95 inline ::ndn::util::Logger& getNdnCxxLogger() \
96 { \
97 static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \
98 return logger; \
99 } \
100 } \
101 struct ndn_cxx__allow_trailing_semicolon
102
103/** \brief a tag that writes a timestamp upon stream output
104 * \code
105 * std::clog << LoggerTimestamp();
106 * \endcode
107 */
108struct LoggerTimestamp
109{
110};
111
112/** \brief write a timestamp to \p os
113 * \note This function is thread-safe.
114 */
115std::ostream&
116operator<<(std::ostream& os, const LoggerTimestamp&);
117
118#if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106000)
119// workaround Boost bug 11549
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000120#define NDN_BOOST_LOG(x) BOOST_LOG(x) << ""
Junxiao Shi7d054272016-08-04 17:00:41 +0000121#else
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000122#define NDN_BOOST_LOG(x) BOOST_LOG(x)
Junxiao Shi7d054272016-08-04 17:00:41 +0000123#endif
124
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000125#define NDN_LOG(lvl, lvlstr, expression) \
Junxiao Shi7d054272016-08-04 17:00:41 +0000126 do { \
127 if (getNdnCxxLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000128 NDN_BOOST_LOG(getNdnCxxLogger()) << ::ndn::util::LoggerTimestamp{} \
Junxiao Shi7d054272016-08-04 17:00:41 +0000129 << " " BOOST_STRINGIZE(lvlstr) ": [" << getNdnCxxLogger().getModuleName() << "] " \
130 << expression; \
131 } \
132 } while (false)
133
134/** \brief log at TRACE level
135 * \pre A log module must be declared in the same translation unit.
136 */
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000137#define NDN_LOG_TRACE(expression) NDN_LOG(TRACE, TRACE, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000138
139/** \brief log at DEBUG level
140 * \pre A log module must be declared in the same translation unit.
141 */
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000142#define NDN_LOG_DEBUG(expression) NDN_LOG(DEBUG, DEBUG, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000143
144/** \brief log at INFO level
145 * \pre A log module must be declared in the same translation unit.
146 */
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000147#define NDN_LOG_INFO(expression) NDN_LOG(INFO, INFO, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000148
149/** \brief log at WARN level
150 * \pre A log module must be declared in the same translation unit.
151 */
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000152#define NDN_LOG_WARN(expression) NDN_LOG(WARN, WARNING, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000153
154/** \brief log at ERROR level
155 * \pre A log module must be declared in the same translation unit.
156 */
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000157#define NDN_LOG_ERROR(expression) NDN_LOG(ERROR, ERROR, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000158
159/** \brief log at FATAL level
160 * \pre A log module must be declared in the same translation unit.
161 */
Junxiao Shi1fe7ce52016-08-08 05:48:02 +0000162#define NDN_LOG_FATAL(expression) NDN_LOG(FATAL, FATAL, expression)
Junxiao Shi7d054272016-08-04 17:00:41 +0000163
164} // namespace util
165} // namespace ndn
166
167#endif // NDN_UTIL_LOGGER_HPP