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-2018 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 
26 
27 #ifdef HAVE_NDN_CXX_CUSTOM_LOGGER
28 #include "ndn-cxx/util/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:
51  static std::set<std::string>
53 
63  static void
64  setLevel(const std::string& prefix, LogLevel level);
65 
80  static void
81  setLevel(const std::string& config);
82 
88  static void
89  setDestination(shared_ptr<std::ostream> os);
90 
97  static void
98  setDestination(std::ostream& os);
99 
104  static void
105  flush();
106 
107 private:
108  Logging();
109 
110  void
111  addLoggerImpl(Logger& logger);
112 
113  void
114  registerLoggerNameImpl(std::string name);
115 
116  std::set<std::string>
117  getLoggerNamesImpl() const;
118 
131  LogLevel
132  findLevel(std::string moduleName) const;
133 
134  void
135  setLevelImpl(const std::string& prefix, LogLevel level);
136 
137  void
138  setLevelImpl(const std::string& config);
139 
140  void
141  setDestinationImpl(shared_ptr<std::ostream> os);
142 
143  void
144  flushImpl();
145 
147  static Logging&
148  get();
149 
150 #ifdef NDN_CXX_HAVE_TESTS
151  bool
152  removeLogger(Logger& logger);
153 
154  void
155  resetLevels();
156 
157  shared_ptr<std::ostream>
158  getDestination() const;
159 
160  void
161  setLevelImpl(const std::unordered_map<std::string, LogLevel>& prefixRules);
162 
163  const std::unordered_map<std::string, LogLevel>&
164  getLevels() const;
165 #endif // NDN_CXX_HAVE_TESTS
166 
167 private:
168  friend Logger;
169 
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  using Sink = boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>;
175  boost::shared_ptr<Sink> m_sink;
176  shared_ptr<std::ostream> m_destination;
177 };
178 
179 inline std::set<std::string>
181 {
182  return get().getLoggerNamesImpl();
183 }
184 
185 inline void
186 Logging::setLevel(const std::string& prefix, LogLevel level)
187 {
188  get().setLevelImpl(prefix, level);
189 }
190 
191 inline void
192 Logging::setLevel(const std::string& config)
193 {
194  get().setLevelImpl(config);
195 }
196 
197 inline void
198 Logging::setDestination(shared_ptr<std::ostream> os)
199 {
200  get().setDestinationImpl(std::move(os));
201 }
202 
203 inline void
205 {
206  get().flushImpl();
207 }
208 
209 } // namespace util
210 } // namespace ndn
211 
212 #endif // HAVE_NDN_CXX_CUSTOM_LOGGER
213 
214 #endif // NDN_UTIL_LOGGING_HPP
Controls the logging facility.
Definition: logging.hpp:46
Definition: data.cpp:26
static void setLevel(const std::string &prefix, LogLevel level)
Set severity level.
Definition: logging.hpp:186
static void setDestination(shared_ptr< std::ostream > os)
Set log destination.
Definition: logging.hpp:198
LogLevel
Indicates the severity level of a log message.
Definition: logger.hpp:40
import common constructs for ndn-cxx library internal use
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:43
static void flush()
Flush log backend.
Definition: logging.hpp:204
Represents a log module in the logging facility.
Definition: logger.hpp:68
static std::set< std::string > getLoggerNames()
Get list of all registered logger names.
Definition: logging.hpp:180