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