blob: 459cf2beaa3a0f4127266db99d19bad9cb1e251c [file] [log] [blame]
Junxiao Shi7d054272016-08-04 17:00:41 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev354f3822017-03-27 15:26:41 -05003 * Copyright (c) 2013-2017 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_LOGGING_HPP
23#define NDN_UTIL_LOGGING_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-logging.hpp"
29#else
30
Junxiao Shi7d054272016-08-04 17:00:41 +000031#include <boost/log/sinks.hpp>
32#include <mutex>
33#include <unordered_map>
34
35namespace ndn {
36namespace util {
37
38enum class LogLevel;
39class Logger;
40
41/** \brief controls the logging facility
42 *
43 * \note Public static methods are thread safe.
44 * Non-public methods are not guaranteed to be thread safe.
45 */
46class Logging : noncopyable
47{
48public:
49 /** \brief register a new logger
Junxiao Shi1fe7ce52016-08-08 05:48:02 +000050 * \note App should declare a new logger with \p NDN_LOG_INIT macro.
Junxiao Shi7d054272016-08-04 17:00:41 +000051 */
52 static void
53 addLogger(Logger& logger);
54
Alexander Afanasyev354f3822017-03-27 15:26:41 -050055 /** \brief get list of names of all registered loggers
56 */
57 static std::set<std::string>
58 getLoggerNames();
59
Junxiao Shi7d054272016-08-04 17:00:41 +000060 /** \brief set severity level
61 * \param moduleName logger name, or "*" for default level
62 * \param level minimum severity level
63 *
64 * Log messages are output only if its severity is greater than the set minimum severity level.
65 * Initial default severity level is \p LogLevel::NONE which enables FATAL only.
66 *
67 * Changing the default level overwrites individual settings.
68 */
69 static void
70 setLevel(const std::string& moduleName, LogLevel level);
71
72 /** \brief set severity levels with a config string
73 * \param config colon-separate key=value pairs
74 * \throw std::invalid_argument config string is malformed
75 *
76 * \code
77 * Logging::setSeverityLevels("*=INFO:Face=DEBUG:NfdController=WARN");
78 * \endcode
79 * is equivalent to
80 * \code
81 * Logging::setSeverityLevel("*", LogLevel::INFO);
82 * Logging::setSeverityLevel("Face", LogLevel::DEBUG);
83 * Logging::setSeverityLevel("NfdController", LogLevel::WARN);
84 * \endcode
85 */
86 static void
87 setLevel(const std::string& config);
88
89 /** \brief set log destination
90 * \param os a stream for log output
91 *
92 * Initial destination is \p std::clog .
93 */
94 static void
95 setDestination(shared_ptr<std::ostream> os);
96
97 /** \brief set log destination
98 * \param os a stream for log output; caller must ensure this is valid
99 * until setDestination is invoked again or program exits
100 *
101 * This is equivalent to setDestination(shared_ptr<std::ostream>(&os, nullDeleter))
102 */
103 static void
104 setDestination(std::ostream& os);
105
106 /** \brief flush log backend
107 *
108 * This ensures log messages are written to the destination stream.
109 */
110 static void
111 flush();
112
113private:
114 Logging();
115
116 void
117 addLoggerImpl(Logger& logger);
118
Alexander Afanasyev354f3822017-03-27 15:26:41 -0500119 std::set<std::string>
120 getLoggerNamesImpl();
121
Junxiao Shi7d054272016-08-04 17:00:41 +0000122 void
123 setLevelImpl(const std::string& moduleName, LogLevel level);
124
125 void
126 setDefaultLevel(LogLevel level);
127
128 void
129 setLevelImpl(const std::string& config);
130
131 void
132 setDestinationImpl(shared_ptr<std::ostream> os);
133
134 void
135 flushImpl();
136
137NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
138 static Logging&
139 get();
140
141#ifdef NDN_CXX_HAVE_TESTS
142 bool
143 removeLogger(Logger& logger);
144
145 std::string
146 getLevels() const;
147
148 void
149 resetLevels();
150
151 shared_ptr<std::ostream>
152 getDestination();
153#endif // NDN_CXX_HAVE_TESTS
154
155private:
156 std::mutex m_mutex;
157 std::unordered_map<std::string, LogLevel> m_enabledLevel; ///< moduleName => minimum level
158 std::unordered_multimap<std::string, Logger*> m_loggers; ///< moduleName => logger
159
160 shared_ptr<std::ostream> m_destination;
161 typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend> Sink;
162 boost::shared_ptr<Sink> m_sink;
163};
164
165inline void
166Logging::addLogger(Logger& logger)
167{
168 get().addLoggerImpl(logger);
169}
170
Alexander Afanasyev354f3822017-03-27 15:26:41 -0500171inline std::set<std::string>
172Logging::getLoggerNames()
173{
174 return get().getLoggerNamesImpl();
175}
176
Junxiao Shi7d054272016-08-04 17:00:41 +0000177inline void
178Logging::setLevel(const std::string& moduleName, LogLevel level)
179{
180 get().setLevelImpl(moduleName, level);
181}
182
183inline void
184Logging::setLevel(const std::string& config)
185{
186 get().setLevelImpl(config);
187}
188
189inline void
190Logging::setDestination(shared_ptr<std::ostream> os)
191{
192 get().setDestinationImpl(os);
193}
194
195inline void
196Logging::flush()
197{
198 get().flushImpl();
199}
200
201
202} // namespace util
203} // namespace ndn
204
Alexander Afanasyeved1e99d2016-11-05 09:59:35 -0600205#endif // HAVE_NDN_CXX_CUSTOM_LOGGER
206
Junxiao Shi7d054272016-08-04 17:00:41 +0000207#endif // NDN_UTIL_LOGGING_HPP