USRP Hardware Driver and USRP Manual  Version: 3.13.0.HEAD-0-gf114cfa0
UHD and USRP Manual
log.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2011 Ettus Research LLC
3 // Copyright 2018 Ettus Research, a National Instruments Company
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 
8 #ifndef INCLUDED_UHD_UTILS_LOG_HPP
9 #define INCLUDED_UHD_UTILS_LOG_HPP
10 
11 #include <uhd/config.hpp>
12 #include <boost/current_function.hpp>
13 #include <boost/thread/thread.hpp>
14 #include <ostream>
15 #include <string>
16 #include <sstream>
17 #include <iostream>
18 
86 /*
87  * Advanced logging macros
88  * UHD_LOG_MIN_LEVEL definitions
89  * trace: 0
90  * debug: 1
91  * info: 2
92  * warning: 3
93  * error: 4
94  * fatal: 5
95  */
96 
97 namespace uhd {
98  namespace log {
105  trace = 0,
106  debug = 1,
107  info = 2,
108  warning = 3,
109  error = 4,
110  fatal = 5,
111  off = 6,
112  };
113 
121  : verbosity(uhd::log::off) {}
123  const boost::posix_time::ptime &time_,
124  const uhd::log::severity_level &verbosity_,
125  const std::string &file_,
126  const size_t &line_,
127  const std::string &component_,
128  const boost::thread::id &thread_id_
129  ) : time(time_),
130  verbosity(verbosity_),
131  file(file_),
132  line(line_),
133  component(component_),
134  thread_id(thread_id_)
135  { /* nop */ }
136 
137  boost::posix_time::ptime time;
139  std::string file;
140  unsigned int line;
141  std::string component;
142  boost::thread::id thread_id;
143  std::string message;
144  };
145 
153 
159 
165 
173  UHD_API void set_logger_level(const std::string &logger, uhd::log::severity_level level);
174  }
175 }
176 
179 #define _UHD_LOG_INTERNAL(component, level) \
180  uhd::_log::log(level, __FILE__, __LINE__, component, boost::this_thread::get_id())
181 
183 // macro-style logging (compile-time determined)
184 #if UHD_LOG_MIN_LEVEL < 1
185 #define UHD_LOG_TRACE(component, message) \
186  _UHD_LOG_INTERNAL(component, uhd::log::trace) << message;
187 #else
188 #define UHD_LOG_TRACE(component, message)
189 #endif
190 
191 #if UHD_LOG_MIN_LEVEL < 2
192 #define UHD_LOG_DEBUG(component, message) \
193  _UHD_LOG_INTERNAL(component, uhd::log::debug) << message;
194 #else
195 #define UHD_LOG_DEBUG(component, message)
196 #endif
197 
198 #if UHD_LOG_MIN_LEVEL < 3
199 #define UHD_LOG_INFO(component, message) \
200  _UHD_LOG_INTERNAL(component, uhd::log::info) << message;
201 #else
202 #define UHD_LOG_INFO(component, message)
203 #endif
204 
205 #if UHD_LOG_MIN_LEVEL < 4
206 #define UHD_LOG_WARNING(component, message) \
207  _UHD_LOG_INTERNAL(component, uhd::log::warning) << message;
208 #else
209 #define UHD_LOG_WARNING(component, message)
210 #endif
211 
212 #if UHD_LOG_MIN_LEVEL < 5
213 #define UHD_LOG_ERROR(component, message) \
214  _UHD_LOG_INTERNAL(component, uhd::log::error) << message;
215 #else
216 #define UHD_LOG_ERROR(component, message)
217 #endif
218 
219 #if UHD_LOG_MIN_LEVEL < 6
220 #define UHD_LOG_FATAL(component, message) \
221  _UHD_LOG_INTERNAL(component, uhd::log::fatal) << message;
222 #else
223 #define UHD_LOG_FATAL(component, message)
224 #endif
225 
226 #ifndef UHD_LOG_FASTPATH_DISABLE
227 // No metadata is tracked. Only the message is displayed. This does not go
229 // through the regular backends. Mostly used for printing the UOSDL characters
230 // during streaming.
231 #define UHD_LOG_FASTPATH(message) \
232  uhd::_log::log_fastpath(message);
233 #else
234 #define UHD_LOG_FASTPATH(message)
235 #endif
236 
237 // iostream-style logging
238 #define UHD_LOGGER_TRACE(component) _UHD_LOG_INTERNAL(component, uhd::log::trace)
239 #define UHD_LOGGER_DEBUG(component) _UHD_LOG_INTERNAL(component, uhd::log::debug)
240 #define UHD_LOGGER_INFO(component) _UHD_LOG_INTERNAL(component, uhd::log::info)
241 #define UHD_LOGGER_WARNING(component) _UHD_LOG_INTERNAL(component, uhd::log::warning)
242 #define UHD_LOGGER_ERROR(component) _UHD_LOG_INTERNAL(component, uhd::log::error)
243 #define UHD_LOGGER_FATAL(component) _UHD_LOG_INTERNAL(component, uhd::log::fatal)
244 
245 
246 #if defined(__GNUG__)
247 #define UHD_HERE() \
249  UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__ << " (" << __PRETTY_FUNCTION__ << ")";
250 #else
251 #define UHD_HERE() \
253  UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__;
254 #endif
255 
257 #define UHD_VAR(var) \
258  UHD_LOGGER_DEBUG("DEBUG") << #var << " = " << var;
259 
261 #define UHD_HEX(var) \
262  UHD_LOGGER_DEBUG("DEBUG") << #var << " = 0x" << std::hex << std::setfill('0') << std::setw(8) << var << std::dec;
263 
265 namespace uhd{ namespace _log {
266 
268  void UHD_API log_fastpath(const std::string &);
269 
271  class UHD_API log {
272  public:
273  log(
274  const uhd::log::severity_level verbosity,
275  const std::string &file,
276  const unsigned int line,
277  const std::string &component,
278  const boost::thread::id thread_id
279  );
280 
281  ~log(void);
282 
283  // Macro for overloading insertion operators to avoid costly
284  // conversion of types if not logging.
285  #define INSERTION_OVERLOAD(x) log& operator<< (x) \
286  { \
287  if(_log_it) { \
288  _ss << val ; \
289  } \
290  return *this; \
291  }
292 
293  // General insertion overload
294  template <typename T>
295  INSERTION_OVERLOAD(T val)
296 
297  // Insertion overloads for std::ostream manipulators
298  INSERTION_OVERLOAD(std::ostream& (*val)(std::ostream&))
299  INSERTION_OVERLOAD(std::ios& (*val)(std::ios&))
300  INSERTION_OVERLOAD(std::ios_base& (*val)(std::ios_base&))
301 
302  private:
303  uhd::log::logging_info _log_info;
304  std::ostringstream _ss;
305  const bool _log_it;
306  };
307 
308 } //namespace uhd::_log
310 } /* namespace uhd */
311 
312 #endif /* INCLUDED_UHD_UTILS_LOG_HPP */
boost::posix_time::ptime time
Definition: log.hpp:137
Definition: log.hpp:107
std::string file
Definition: log.hpp:139
unsigned int line
Definition: log.hpp:140
logging_info()
Definition: log.hpp:120
severity_level
Definition: log.hpp:104
Definition: log.hpp:105
Definition: log.hpp:111
logging_info(const boost::posix_time::ptime &time_, const uhd::log::severity_level &verbosity_, const std::string &file_, const size_t &line_, const std::string &component_, const boost::thread::id &thread_id_)
Definition: log.hpp:122
Definition: build_info.hpp:14
UHD_API void set_file_level(uhd::log::severity_level level)
uhd::log::severity_level verbosity
Definition: log.hpp:138
Definition: log.hpp:119
UHD_API void set_log_level(uhd::log::severity_level level)
UHD_API void set_logger_level(const std::string &logger, uhd::log::severity_level level)
boost::thread::id thread_id
Definition: log.hpp:142
Definition: log.hpp:109
Definition: log.hpp:108
Definition: log.hpp:106
#define UHD_API
Definition: config.h:63
std::string message
Definition: log.hpp:143
UHD_API void set_console_level(uhd::log::severity_level level)
std::string component
Definition: log.hpp:141
Definition: log.hpp:110