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-2024 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 <set>
34 #include <unordered_map>
35 
36 namespace ndn::util {
37 
38 enum class LogLevel;
39 class Logger;
40 
46 class Logging : noncopyable
47 {
48 public:
52  [[nodiscard]] static std::set<std::string>
54 
64  static void
65  setLevel(const std::string& prefix, LogLevel level);
66 
81  static void
82  setLevel(const std::string& config);
83 
95  static void
96  setDestination(boost::shared_ptr<boost::log::sinks::sink> destination);
97 
106  static void
107  setDestination(std::ostream& os, bool wantAutoFlush);
108 
114  static void
115  flush();
116 
120  [[nodiscard]] static boost::shared_ptr<boost::log::sinks::sink>
121  makeDefaultStreamDestination(shared_ptr<std::ostream> os, bool wantAutoFlush = true);
122 
123 private:
124  Logging();
125 
126  void
127  addLoggerImpl(Logger& logger);
128 
129  void
130  registerLoggerNameImpl(std::string name);
131 
132  [[nodiscard]] std::set<std::string>
133  getLoggerNamesImpl() const;
134 
147  LogLevel
148  findLevel(std::string moduleName) const;
149 
150  void
151  setLevelImpl(const std::string& prefix, LogLevel level);
152 
153  void
154  setLevelImpl(const std::string& config);
155 
156  void
157  setDestinationImpl(boost::shared_ptr<boost::log::sinks::sink> sink);
158 
159  void
160  flushImpl();
161 
163  static Logging&
164  get();
165 
166 #ifdef NDN_CXX_WITH_TESTS
167  bool
168  removeLogger(Logger& logger);
169 
170  void
171  resetLevels();
172 
173  boost::shared_ptr<boost::log::sinks::sink>
174  getDestination() const;
175 
176  void
177  setLevelImpl(const std::unordered_map<std::string, LogLevel>& prefixRules);
178 
179  const std::unordered_map<std::string, LogLevel>&
180  getLevels() const;
181 #endif // NDN_CXX_WITH_TESTS
182 
183 private:
184  friend Logger;
185 
186  mutable std::mutex m_mutex;
187  std::unordered_map<std::string, LogLevel> m_enabledLevel;
188  std::unordered_multimap<std::string, Logger*> m_loggers;
189 
190  boost::shared_ptr<boost::log::sinks::sink> m_destination;
191 };
192 
193 inline std::set<std::string>
195 {
196  return get().getLoggerNamesImpl();
197 }
198 
199 inline void
200 Logging::setLevel(const std::string& prefix, LogLevel level)
201 {
202  get().setLevelImpl(prefix, level);
203 }
204 
205 inline void
206 Logging::setLevel(const std::string& config)
207 {
208  get().setLevelImpl(config);
209 }
210 
211 inline void
212 Logging::setDestination(boost::shared_ptr<boost::log::sinks::sink> destination)
213 {
214  get().setDestinationImpl(std::move(destination));
215 }
216 
217 inline void
219 {
220  get().flushImpl();
221 }
222 
223 } // namespace ndn::util
224 
225 #endif // HAVE_NDN_CXX_CUSTOM_LOGGER
226 
227 #endif // NDN_CXX_UTIL_LOGGING_HPP
Represents a log module in the logging facility.
Definition: logger.hpp:84
Controls the logging facility.
Definition: logging.hpp:47
static void flush()
Flush log backend.
Definition: logging.hpp:218
static void setLevel(const std::string &prefix, LogLevel level)
Set severity level.
Definition: logging.hpp:200
static void setDestination(boost::shared_ptr< boost::log::sinks::sink > destination)
Set or replace log destination.
Definition: logging.hpp:212
static std::set< std::string > getLoggerNames()
Get list of all registered logger names.
Definition: logging.hpp:194
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:273
Common includes and macros used throughout the library.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:49
LogLevel
Indicates the severity level of a log message.
Definition: logger.hpp:45