logging.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
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 
64  static void
65  setLevel(const std::string& moduleName, LogLevel level);
66 
81  static void
82  setLevel(const std::string& config);
83 
89  static void
90  setDestination(shared_ptr<std::ostream> os);
91 
98  static void
99  setDestination(std::ostream& os);
100 
105  static void
106  flush();
107 
108 private:
109  Logging();
110 
111  void
112  addLoggerImpl(Logger& logger);
113 
114  void
115  setLevelImpl(const std::string& moduleName, LogLevel level);
116 
117  void
118  setDefaultLevel(LogLevel level);
119 
120  void
121  setLevelImpl(const std::string& config);
122 
123  void
124  setDestinationImpl(shared_ptr<std::ostream> os);
125 
126  void
127  flushImpl();
128 
130  static Logging&
131  get();
132 
133 #ifdef NDN_CXX_HAVE_TESTS
134  bool
135  removeLogger(Logger& logger);
136 
137  std::string
138  getLevels() const;
139 
140  void
141  resetLevels();
142 
143  shared_ptr<std::ostream>
144  getDestination();
145 #endif // NDN_CXX_HAVE_TESTS
146 
147 private:
148  std::mutex m_mutex;
149  std::unordered_map<std::string, LogLevel> m_enabledLevel;
150  std::unordered_multimap<std::string, Logger*> m_loggers;
151 
152  shared_ptr<std::ostream> m_destination;
153  typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend> Sink;
154  boost::shared_ptr<Sink> m_sink;
155 };
156 
157 inline void
159 {
160  get().addLoggerImpl(logger);
161 }
162 
163 inline void
164 Logging::setLevel(const std::string& moduleName, LogLevel level)
165 {
166  get().setLevelImpl(moduleName, level);
167 }
168 
169 inline void
170 Logging::setLevel(const std::string& config)
171 {
172  get().setLevelImpl(config);
173 }
174 
175 inline void
176 Logging::setDestination(shared_ptr<std::ostream> os)
177 {
178  get().setDestinationImpl(os);
179 }
180 
181 inline void
183 {
184  get().flushImpl();
185 }
186 
187 
188 } // namespace util
189 } // namespace ndn
190 
191 #endif // HAVE_NDN_CXX_CUSTOM_LOGGER
192 
193 #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:158
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
static void setDestination(shared_ptr< std::ostream > os)
set log destination
Definition: logging.hpp:176
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:43
static void setLevel(const std::string &moduleName, LogLevel level)
set severity level
Definition: logging.hpp:164
LogLevel
indicates the severity level of a log message
Definition: logger.hpp:40
static void flush()
flush log backend
Definition: logging.hpp:182
represents a logger in logging facility
Definition: logger.hpp:66