USRP Hardware Driver and USRP Manual  Version: 4.2.0.0-143-g2ad30980f
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/date_time/posix_time/posix_time_types.hpp>
12 #include <boost/optional.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 
129 boost::optional<uhd::log::severity_level> UHD_API parse_log_level_from_string(
130  const std::string& log_level_str);
131 
138 {
139  logging_info() : verbosity(uhd::log::off) {}
140  logging_info(const boost::posix_time::ptime& time_,
141  const uhd::log::severity_level& verbosity_,
142  const std::string& file_,
143  const unsigned int& line_,
144  const std::string& component_,
145  const std::thread::id& thread_id_)
146  : time(time_)
147  , verbosity(verbosity_)
148  , file(file_)
149  , line(line_)
150  , component(component_)
151  , thread_id(thread_id_)
152  { /* nop */
153  }
154 
155  boost::posix_time::ptime time;
157  std::string file;
158  unsigned int line;
159  std::string component;
160  std::thread::id thread_id;
161  std::string message;
162 };
163 
171 
177 
183 
191 UHD_API void set_logger_level(const std::string& logger, uhd::log::severity_level level);
192 }} // namespace uhd::log
193 
196 #define _UHD_LOG_INTERNAL(component, level) \
197  uhd::_log::log(level, __FILE__, __LINE__, component, std::this_thread::get_id())
198 
200 // macro-style logging (compile-time determined)
201 #if UHD_LOG_MIN_LEVEL < 1
202 # define UHD_LOG_TRACE(component, message) \
203  _UHD_LOG_INTERNAL(component, uhd::log::trace) << message;
204 #else
205 # define UHD_LOG_TRACE(component, message)
206 #endif
207 
208 #if UHD_LOG_MIN_LEVEL < 2
209 # define UHD_LOG_DEBUG(component, message) \
210  _UHD_LOG_INTERNAL(component, uhd::log::debug) << message;
211 #else
212 # define UHD_LOG_DEBUG(component, message)
213 #endif
214 
215 #if UHD_LOG_MIN_LEVEL < 3
216 # define UHD_LOG_INFO(component, message) \
217  _UHD_LOG_INTERNAL(component, uhd::log::info) << message;
218 #else
219 # define UHD_LOG_INFO(component, message)
220 #endif
221 
222 #if UHD_LOG_MIN_LEVEL < 4
223 # define UHD_LOG_WARNING(component, message) \
224  _UHD_LOG_INTERNAL(component, uhd::log::warning) << message;
225 #else
226 # define UHD_LOG_WARNING(component, message)
227 #endif
228 
229 #if UHD_LOG_MIN_LEVEL < 5
230 # define UHD_LOG_ERROR(component, message) \
231  _UHD_LOG_INTERNAL(component, uhd::log::error) << message;
232 #else
233 # define UHD_LOG_ERROR(component, message)
234 #endif
235 
236 #define UHD_LOG_THROW(exception_type, component, message) \
237  { \
238  std::ostringstream __ss; \
239  __ss << message; \
240  UHD_LOG_ERROR(component, __ss.str()); \
241  throw exception_type(__ss.str()); \
242  }
243 
244 #if UHD_LOG_MIN_LEVEL < 6
245 # define UHD_LOG_FATAL(component, message) \
246  _UHD_LOG_INTERNAL(component, uhd::log::fatal) << message;
247 #else
248 # define UHD_LOG_FATAL(component, message)
249 #endif
250 
251 #define RFNOC_LOG_TRACE(message) UHD_LOG_TRACE(this->get_unique_id(), message)
252 #define RFNOC_LOG_DEBUG(message) UHD_LOG_DEBUG(this->get_unique_id(), message)
253 #define RFNOC_LOG_INFO(message) UHD_LOG_INFO(this->get_unique_id(), message)
254 #define RFNOC_LOG_WARNING(message) UHD_LOG_WARNING(this->get_unique_id(), message)
255 #define RFNOC_LOG_ERROR(message) UHD_LOG_ERROR(this->get_unique_id(), message)
256 #define RFNOC_LOG_FATAL(message) UHD_LOG_FATAL(this->get_unique_id(), message)
257 
258 #ifndef UHD_LOG_FASTPATH_DISABLE
259 // No metadata is tracked. Only the message is displayed. This does not go
261 // through the regular backends. Mostly used for printing the UOSDL characters
262 // during streaming.
263 # define UHD_LOG_FASTPATH(message) uhd::_log::log_fastpath(message);
264 #else
265 # define UHD_LOG_FASTPATH(message)
266 #endif
267 
268 // iostream-style logging
269 #define UHD_LOGGER_TRACE(component) _UHD_LOG_INTERNAL(component, uhd::log::trace)
270 #define UHD_LOGGER_DEBUG(component) _UHD_LOG_INTERNAL(component, uhd::log::debug)
271 #define UHD_LOGGER_INFO(component) _UHD_LOG_INTERNAL(component, uhd::log::info)
272 #define UHD_LOGGER_WARNING(component) _UHD_LOG_INTERNAL(component, uhd::log::warning)
273 #define UHD_LOGGER_ERROR(component) _UHD_LOG_INTERNAL(component, uhd::log::error)
274 #define UHD_LOGGER_FATAL(component) _UHD_LOG_INTERNAL(component, uhd::log::fatal)
275 
276 
277 #if defined(__GNUG__)
278 # define UHD_HERE() \
280  UHD_LOGGER_DEBUG("DEBUG") \
281  << __FILE__ << ":" << __LINE__ << " (" << UHD_PRETTY_FUNCTION << ")";
282 #else
283 # define UHD_HERE() UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__;
285 #endif
286 
288 #define UHD_VAR(var) UHD_LOGGER_DEBUG("DEBUG") << #var << " = " << var;
289 
291 #define UHD_HEX(var) \
292  UHD_LOGGER_DEBUG("DEBUG") << #var << " = 0x" << std::hex << std::setfill('0') \
293  << std::setw(8) << var << std::dec;
294 
296 namespace uhd {
297 namespace _log {
298 
300 void UHD_API log_fastpath(const std::string&);
301 
303 class UHD_API log
304 {
305 public:
306  log(const uhd::log::severity_level verbosity,
307  const std::string& file,
308  const unsigned int line,
309  const std::string& component,
310  const std::thread::id thread_id);
311 
312  ~log(void);
313 
314 // Macro for overloading insertion operators to avoid costly
315 // conversion of types if not logging.
316 #define INSERTION_OVERLOAD(x) \
317  log& operator<<(x) \
318  { \
319  if (_log_it) { \
320  _ss << val; \
321  } \
322  return *this; \
323  }
324 
325  // General insertion overload
326  template <typename T>
327  INSERTION_OVERLOAD(T val)
328 
329  // Insertion overloads for std::ostream manipulators
330  INSERTION_OVERLOAD(std::ostream& (*val)(std::ostream&))
331  INSERTION_OVERLOAD(std::ios& (*val)(std::ios&))
332  INSERTION_OVERLOAD(std::ios_base& (*val)(std::ios_base&))
333 
334  private : uhd::log::logging_info _log_info;
335  std::ostringstream _ss;
336  const bool _log_it;
337 };
338 
339 } // namespace _log
341 } /* namespace uhd */
boost::posix_time::ptime time
Definition: log.hpp:155
Definition: log.hpp:119
std::string file
Definition: log.hpp:157
unsigned int line
Definition: log.hpp:158
logging_info()
Definition: log.hpp:139
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:160
UHD_API void set_file_level(uhd::log::severity_level level)
uhd::log::severity_level verbosity
Definition: log.hpp:156
Definition: log.hpp:137
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:87
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:140
std::string message
Definition: log.hpp:161
UHD_API void set_console_level(uhd::log::severity_level level)
std::string component
Definition: log.hpp:159
Definition: log.hpp:122
boost::optional< uhd::log::severity_level > UHD_API parse_log_level_from_string(const std::string &log_level_str)