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-2021 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_CXX_UTIL_LOGGING_HPP
23 #define NDN_CXX_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 
94  static void
95  setDestination(boost::shared_ptr<boost::log::sinks::sink> destination);
96 
105  static void
106  setDestination(std::ostream& os, bool wantAutoFlush);
107 
112  static void
113  flush();
114 
117  static boost::shared_ptr<boost::log::sinks::sink>
118  makeDefaultStreamDestination(shared_ptr<std::ostream> os, bool wantAutoFlush = true);
119 
120 private:
121  Logging();
122 
123  void
124  addLoggerImpl(Logger& logger);
125 
126  void
127  registerLoggerNameImpl(std::string name);
128 
129  std::set<std::string>
130  getLoggerNamesImpl() const;
131 
144  LogLevel
145  findLevel(std::string moduleName) const;
146 
147  void
148  setLevelImpl(const std::string& prefix, LogLevel level);
149 
150  void
151  setLevelImpl(const std::string& config);
152 
153  void
154  setDestinationImpl(boost::shared_ptr<boost::log::sinks::sink> sink);
155 
156  void
157  flushImpl();
158 
160  static Logging&
161  get();
162 
163 #ifdef NDN_CXX_HAVE_TESTS
164  bool
165  removeLogger(Logger& logger);
166 
167  void
168  resetLevels();
169 
170  boost::shared_ptr<boost::log::sinks::sink>
171  getDestination() const;
172 
173  void
174  setLevelImpl(const std::unordered_map<std::string, LogLevel>& prefixRules);
175 
176  const std::unordered_map<std::string, LogLevel>&
177  getLevels() const;
178 #endif // NDN_CXX_HAVE_TESTS
179 
180 private:
181  friend Logger;
182 
183  mutable std::mutex m_mutex;
184  std::unordered_map<std::string, LogLevel> m_enabledLevel;
185  std::unordered_multimap<std::string, Logger*> m_loggers;
186 
187  boost::shared_ptr<boost::log::sinks::sink> m_destination;
188 };
189 
190 inline std::set<std::string>
192 {
193  return get().getLoggerNamesImpl();
194 }
195 
196 inline void
197 Logging::setLevel(const std::string& prefix, LogLevel level)
198 {
199  get().setLevelImpl(prefix, level);
200 }
201 
202 inline void
203 Logging::setLevel(const std::string& config)
204 {
205  get().setLevelImpl(config);
206 }
207 
208 inline void
209 Logging::setDestination(boost::shared_ptr<boost::log::sinks::sink> destination)
210 {
211  get().setDestinationImpl(std::move(destination));
212 }
213 
214 inline void
216 {
217  get().flushImpl();
218 }
219 
220 } // namespace util
221 } // namespace ndn
222 
223 #endif // HAVE_NDN_CXX_CUSTOM_LOGGER
224 
225 #endif // NDN_CXX_UTIL_LOGGING_HPP
Represents a log module in the logging facility.
Definition: logger.hpp:78
Controls the logging facility.
Definition: logging.hpp:47
static void flush()
Flush log backend.
Definition: logging.hpp:215
static void setLevel(const std::string &prefix, LogLevel level)
Set severity level.
Definition: logging.hpp:197
static void setDestination(boost::shared_ptr< boost::log::sinks::sink > destination)
Set or replace log destination.
Definition: logging.hpp:209
static std::set< std::string > getLoggerNames()
Get list of all registered logger names.
Definition: logging.hpp:191
static boost::shared_ptr< boost::log::sinks::sink > makeDefaultStreamDestination(shared_ptr< std::ostream > os, bool wantAutoFlush=true)
Create stream log destination using default formatting.
Definition: logging.cpp:281
Common includes and macros used throughout the library.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
LogLevel
Indicates the severity level of a log message.
Definition: logger.hpp:42
Definition: data.cpp:25