logging.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 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_LOGGING_HPP
23 #define NDN_UTIL_LOGGING_HPP
24 
25 #include "../common.hpp"
26 
27 #ifdef HAVE_NDN_CXX_CUSTOM_LOGGER
28 #include "ndn-cxx-custom-logging.hpp"
29 #else
30 
31 #include <boost/log/sinks.hpp>
32 #include <mutex>
33 #include <unordered_map>
34 
35 namespace ndn {
36 namespace util {
37 
38 enum class LogLevel;
39 class Logger;
40 
46 class Logging : noncopyable
47 {
48 public:
52  static void
53  addLogger(Logger& logger);
54 
57  static std::set<std::string>
59 
68  static void
69  setLevel(const std::string& prefix, LogLevel level);
70 
85  static void
86  setLevel(const std::string& config);
87 
93  static void
94  setDestination(shared_ptr<std::ostream> os);
95 
102  static void
103  setDestination(std::ostream& os);
104 
109  static void
110  flush();
111 
112 private:
113  Logging();
114 
115  void
116  addLoggerImpl(Logger& logger);
117 
118  std::set<std::string>
119  getLoggerNamesImpl() const;
120 
133  LogLevel
134  findLevel(const std::string& moduleName) const;
135 
136  void
137  setLevelImpl(const std::string& prefix, LogLevel level);
138 
139  void
140  setLevelImpl(const std::string& config);
141 
142  void
143  setDestinationImpl(shared_ptr<std::ostream> os);
144 
145  void
146  flushImpl();
147 
149  static Logging&
150  get();
151 
152 #ifdef NDN_CXX_HAVE_TESTS
153  bool
154  removeLogger(Logger& logger);
155 
156  void
157  resetLevels();
158 
159  shared_ptr<std::ostream>
160  getDestination() const;
161 
162  void
163  setLevelImpl(const std::unordered_map<std::string, LogLevel>& prefixRules);
164 
165  const std::unordered_map<std::string, LogLevel>&
166  getLevels() const;
167 #endif // NDN_CXX_HAVE_TESTS
168 
169 private:
170  mutable std::mutex m_mutex;
171  std::unordered_map<std::string, LogLevel> m_enabledLevel;
172  std::unordered_multimap<std::string, Logger*> m_loggers;
173 
174  shared_ptr<std::ostream> m_destination;
175  typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend> Sink;
176  boost::shared_ptr<Sink> m_sink;
177 };
178 
179 inline void
181 {
182  get().addLoggerImpl(logger);
183 }
184 
185 inline std::set<std::string>
187 {
188  return get().getLoggerNamesImpl();
189 }
190 
191 inline void
192 Logging::setLevel(const std::string& prefix, LogLevel level)
193 {
194  get().setLevelImpl(prefix, level);
195 }
196 
197 inline void
198 Logging::setLevel(const std::string& config)
199 {
200  get().setLevelImpl(config);
201 }
202 
203 inline void
204 Logging::setDestination(shared_ptr<std::ostream> os)
205 {
206  get().setDestinationImpl(std::move(os));
207 }
208 
209 inline void
211 {
212  get().flushImpl();
213 }
214 
215 } // namespace util
216 } // namespace ndn
217 
218 #endif // HAVE_NDN_CXX_CUSTOM_LOGGER
219 
220 #endif // NDN_UTIL_LOGGING_HPP
controls the logging facility
Definition: logging.hpp:46
static void addLogger(Logger &logger)
register a new logger
Definition: logging.hpp:180
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
static void setLevel(const std::string &prefix, LogLevel level)
set severity level
Definition: logging.hpp:192
static void setDestination(shared_ptr< std::ostream > os)
set log destination
Definition: logging.hpp:204
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:43
LogLevel
indicates the severity level of a log message
Definition: logger.hpp:40
static void flush()
flush log backend
Definition: logging.hpp:210
represents a logger in logging facility
Definition: logger.hpp:66
static std::set< std::string > getLoggerNames()
get list of names of all registered loggers
Definition: logging.hpp:186