USRP Hardware Driver and USRP Manual  Version: 4.1.0.0-264-g3e5e4eb06
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 #pragma once
9 
10 #include <uhd/config.hpp>
11 #include <boost/current_function.hpp>
12 #include <boost/date_time/posix_time/posix_time_types.hpp>
13 #include <iomanip>
14 #include <iostream>
15 #include <ostream>
16 #include <sstream>
17 #include <string>
18 #include <thread>
19 
99 /*
100  * Advanced logging macros
101  * UHD_LOG_MIN_LEVEL definitions
102  * trace: 0
103  * debug: 1
104  * info: 2
105  * warning: 3
106  * error: 4
107  * fatal: 5
108  */
109 
110 namespace uhd { namespace log {
117  trace = 0,
118  debug = 1,
119  info = 2,
120  warning = 3,
121  error = 4,
122  fatal = 5,
123  off = 6,
124 };
125 
132 {
133  logging_info() : verbosity(uhd::log::off) {}
134  logging_info(const boost::posix_time::ptime& time_,
135  const uhd::log::severity_level& verbosity_,
136  const std::string& file_,
137  const unsigned int& line_,
138  const std::string& component_,
139  const std::thread::id& thread_id_)
140  : time(time_)
141  , verbosity(verbosity_)
142  , file(file_)
143  , line(line_)
144  , component(component_)
145  , thread_id(thread_id_)
146  { /* nop */
147  }
148 
149  boost::posix_time::ptime time;
151  std::string file;
152  unsigned int line;
153  std::string component;
154  std::thread::id thread_id;
155  std::string message;
156 };
157 
165 
171 
177 
185 UHD_API void set_logger_level(const std::string& logger, uhd::log::severity_level level);
186 }} // namespace uhd::log
187 
190 #define _UHD_LOG_INTERNAL(component, level) \
191  uhd::_log::log(level, __FILE__, __LINE__, component, std::this_thread::get_id())
192 
194 // macro-style logging (compile-time determined)
195 #if UHD_LOG_MIN_LEVEL < 1
196 # define UHD_LOG_TRACE(component, message) \
197  _UHD_LOG_INTERNAL(component, uhd::log::trace) << message;
198 #else
199 # define UHD_LOG_TRACE(component, message)
200 #endif
201 
202 #if UHD_LOG_MIN_LEVEL < 2
203 # define UHD_LOG_DEBUG(component, message) \
204  _UHD_LOG_INTERNAL(component, uhd::log::debug) << message;
205 #else
206 # define UHD_LOG_DEBUG(component, message)
207 #endif
208 
209 #if UHD_LOG_MIN_LEVEL < 3
210 # define UHD_LOG_INFO(component, message) \
211  _UHD_LOG_INTERNAL(component, uhd::log::info) << message;
212 #else
213 # define UHD_LOG_INFO(component, message)
214 #endif
215 
216 #if UHD_LOG_MIN_LEVEL < 4
217 # define UHD_LOG_WARNING(component, message) \
218  _UHD_LOG_INTERNAL(component, uhd::log::warning) << message;
219 #else
220 # define UHD_LOG_WARNING(component, message)
221 #endif
222 
223 #if UHD_LOG_MIN_LEVEL < 5
224 # define UHD_LOG_ERROR(component, message) \
225  _UHD_LOG_INTERNAL(component, uhd::log::error) << message;
226 #else
227 # define UHD_LOG_ERROR(component, message)
228 #endif
229 
230 #if UHD_LOG_MIN_LEVEL < 6
231 # define UHD_LOG_FATAL(component, message) \
232  _UHD_LOG_INTERNAL(component, uhd::log::fatal) << message;
233 #else
234 # define UHD_LOG_FATAL(component, message)
235 #endif
236 
237 #define RFNOC_LOG_TRACE(message) UHD_LOG_TRACE(this->get_unique_id(), message)
238 #define RFNOC_LOG_DEBUG(message) UHD_LOG_DEBUG(this->get_unique_id(), message)
239 #define RFNOC_LOG_INFO(message) UHD_LOG_INFO(this->get_unique_id(), message)
240 #define RFNOC_LOG_WARNING(message) UHD_LOG_WARNING(this->get_unique_id(), message)
241 #define RFNOC_LOG_ERROR(message) UHD_LOG_ERROR(this->get_unique_id(), message)
242 #define RFNOC_LOG_FATAL(message) UHD_LOG_FATAL(this->get_unique_id(), message)
243 
244 #ifndef UHD_LOG_FASTPATH_DISABLE
245 // No metadata is tracked. Only the message is displayed. This does not go
247 // through the regular backends. Mostly used for printing the UOSDL characters
248 // during streaming.
249 # define UHD_LOG_FASTPATH(message) uhd::_log::log_fastpath(message);
250 #else
251 # define UHD_LOG_FASTPATH(message)
252 #endif
253 
254 // iostream-style logging
255 #define UHD_LOGGER_TRACE(component) _UHD_LOG_INTERNAL(component, uhd::log::trace)
256 #define UHD_LOGGER_DEBUG(component) _UHD_LOG_INTERNAL(component, uhd::log::debug)
257 #define UHD_LOGGER_INFO(component) _UHD_LOG_INTERNAL(component, uhd::log::info)
258 #define UHD_LOGGER_WARNING(component) _UHD_LOG_INTERNAL(component, uhd::log::warning)
259 #define UHD_LOGGER_ERROR(component) _UHD_LOG_INTERNAL(component, uhd::log::error)
260 #define UHD_LOGGER_FATAL(component) _UHD_LOG_INTERNAL(component, uhd::log::fatal)
261 
262 
263 #if defined(__GNUG__)
264 # define UHD_HERE() \
266  UHD_LOGGER_DEBUG("DEBUG") \
267  << __FILE__ << ":" << __LINE__ << " (" << __PRETTY_FUNCTION__ << ")";
268 #else
269 # define UHD_HERE() UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__;
271 #endif
272 
274 #define UHD_VAR(var) UHD_LOGGER_DEBUG("DEBUG") << #var << " = " << var;
275 
277 #define UHD_HEX(var) \
278  UHD_LOGGER_DEBUG("DEBUG") << #var << " = 0x" << std::hex << std::setfill('0') \
279  << std::setw(8) << var << std::dec;
280 
282 namespace uhd {
283 namespace _log {
284 
286 void UHD_API log_fastpath(const std::string&);
287 
289 class UHD_API log
290 {
291 public:
292  log(const uhd::log::severity_level verbosity,
293  const std::string& file,
294  const unsigned int line,
295  const std::string& component,
296  const std::thread::id thread_id);
297 
298  ~log(void);
299 
300 // Macro for overloading insertion operators to avoid costly
301 // conversion of types if not logging.
302 #define INSERTION_OVERLOAD(x) \
303  log& operator<<(x) \
304  { \
305  if (_log_it) { \
306  _ss << val; \
307  } \
308  return *this; \
309  }
310 
311  // General insertion overload
312  template <typename T>
313  INSERTION_OVERLOAD(T val)
314 
315  // Insertion overloads for std::ostream manipulators
316  INSERTION_OVERLOAD(std::ostream& (*val)(std::ostream&))
317  INSERTION_OVERLOAD(std::ios& (*val)(std::ios&))
318  INSERTION_OVERLOAD(std::ios_base& (*val)(std::ios_base&))
319 
320  private : uhd::log::logging_info _log_info;
321  std::ostringstream _ss;
322  const bool _log_it;
323 };
324 
325 } // namespace _log
327 } /* namespace uhd */
boost::posix_time::ptime time
Definition: log.hpp:149
Definition: log.hpp:119
std::string file
Definition: log.hpp:151
unsigned int line
Definition: log.hpp:152
logging_info()
Definition: log.hpp:133
severity_level
Definition: log.hpp:116
Definition: log.hpp:117
Definition: log.hpp:123
Definition: build_info.hpp:12
std::thread::id thread_id
Definition: log.hpp:154
UHD_API void set_file_level(uhd::log::severity_level level)
uhd::log::severity_level verbosity
Definition: log.hpp:150
Definition: log.hpp:131
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)
Definition: log.hpp:121
Definition: log.hpp:120
Definition: log.hpp:118
#define UHD_API
Definition: config.h:70
logging_info(const boost::posix_time::ptime &time_, const uhd::log::severity_level &verbosity_, const std::string &file_, const unsigned int &line_, const std::string &component_, const std::thread::id &thread_id_)
Definition: log.hpp:134
std::string message
Definition: log.hpp:155
UHD_API void set_console_level(uhd::log::severity_level level)
std::string component
Definition: log.hpp:153
Definition: log.hpp:122