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 #include <boost/log/sinks.hpp>
28 #include <mutex>
29 #include <unordered_map>
30 
31 namespace ndn {
32 namespace util {
33 
34 enum class LogLevel;
35 class Logger;
36 
42 class Logging : noncopyable
43 {
44 public:
48  static void
49  addLogger(Logger& logger);
50 
60  static void
61  setLevel(const std::string& moduleName, LogLevel level);
62 
77  static void
78  setLevel(const std::string& config);
79 
85  static void
86  setDestination(shared_ptr<std::ostream> os);
87 
94  static void
95  setDestination(std::ostream& os);
96 
101  static void
102  flush();
103 
104 private:
105  Logging();
106 
107  void
108  addLoggerImpl(Logger& logger);
109 
110  void
111  setLevelImpl(const std::string& moduleName, LogLevel level);
112 
113  void
114  setDefaultLevel(LogLevel level);
115 
116  void
117  setLevelImpl(const std::string& config);
118 
119  void
120  setDestinationImpl(shared_ptr<std::ostream> os);
121 
122  void
123  flushImpl();
124 
126  static Logging&
127  get();
128 
129 #ifdef NDN_CXX_HAVE_TESTS
130  bool
131  removeLogger(Logger& logger);
132 
133  std::string
134  getLevels() const;
135 
136  void
137  resetLevels();
138 
139  shared_ptr<std::ostream>
140  getDestination();
141 #endif // NDN_CXX_HAVE_TESTS
142 
143 private:
144  std::mutex m_mutex;
145  std::unordered_map<std::string, LogLevel> m_enabledLevel;
146  std::unordered_multimap<std::string, Logger*> m_loggers;
147 
148  shared_ptr<std::ostream> m_destination;
149  typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend> Sink;
150  boost::shared_ptr<Sink> m_sink;
151 };
152 
153 inline void
155 {
156  get().addLoggerImpl(logger);
157 }
158 
159 inline void
160 Logging::setLevel(const std::string& moduleName, LogLevel level)
161 {
162  get().setLevelImpl(moduleName, level);
163 }
164 
165 inline void
166 Logging::setLevel(const std::string& config)
167 {
168  get().setLevelImpl(config);
169 }
170 
171 inline void
172 Logging::setDestination(shared_ptr<std::ostream> os)
173 {
174  get().setDestinationImpl(os);
175 }
176 
177 inline void
179 {
180  get().flushImpl();
181 }
182 
183 
184 } // namespace util
185 } // namespace ndn
186 
187 #endif // NDN_UTIL_LOGGING_HPP
controls the logging facility
Definition: logging.hpp:42
static void addLogger(Logger &logger)
register a new logger
Definition: logging.hpp:154
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:172
#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:160
LogLevel
indicates the severity level of a log message
Definition: logger.hpp:36
static void flush()
flush log backend
Definition: logging.hpp:178
represents a logger in logging facility
Definition: logger.hpp:62