USRP Hardware Driver and Device Manual  Version: 4.11.0.0-0-g0d7ed3b1
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 <type_traits>
12 #include <boost/none.hpp>
13 #include <boost/optional.hpp>
14 #include <chrono>
15 #include <iomanip>
16 #include <iostream>
17 #include <optional>
18 #include <ostream>
19 #include <sstream>
20 #include <string>
21 #include <thread>
22 
102 /*
103  * Advanced logging macros
104  * UHD_LOG_MIN_LEVEL definitions
105  * trace: 0
106  * debug: 1
107  * info: 2
108  * warning: 3
109  * error: 4
110  * fatal: 5
111  */
112 
113 namespace uhd { namespace log {
120  trace = 0,
121  debug = 1,
122  info = 2,
123  warning = 3,
124  error = 4,
125  fatal = 5,
126  off = 6,
127 };
128 
129 namespace detail {
136 UHD_API std::optional<uhd::log::severity_level> parse_log_level_from_string_impl(
137  const std::string& log_level_str);
138 
145 {
146  logging_info() : verbosity(uhd::log::off), line(0) {}
147  logging_info(const std::chrono::system_clock::time_point& time_,
148  const uhd::log::severity_level& verbosity_,
149  const std::string& file_,
150  const unsigned int& line_,
151  const std::string& component_,
152  const std::thread::id& thread_id_)
153  : time(time_)
154  , verbosity(verbosity_)
155  , file(file_)
156  , line(line_)
157  , component(component_)
158  , thread_id(thread_id_)
159  { /* nop */
160  }
161 
162  std::chrono::system_clock::time_point time;
164  std::string file;
165  unsigned int line;
166  std::string component;
167  std::thread::id thread_id;
168  std::string message;
169 };
170 
171 } // namespace detail
172 
173 template <typename T = boost::optional<uhd::log::severity_level>,
174  std::enable_if_t<
175  std::is_same_v<T,
176  boost::optional<uhd::log::
177  severity_level>> || std::is_same_v<T, std::optional<uhd::log::severity_level>>,
178  int> = 0>
179 T parse_log_level_from_string(const std::string& log_level_str)
180 {
181  if constexpr (std::is_same_v<T, boost::optional<uhd::log::severity_level>>) {
182  const auto level = detail::parse_log_level_from_string_impl(log_level_str);
183  return bool(level) ? boost::make_optional(*level) : boost::none;
184  } else {
185  return detail::parse_log_level_from_string_impl(log_level_str);
186  }
187 }
188 
196 
202 
208 
216 UHD_API void set_logger_level(const std::string& logger, uhd::log::severity_level level);
217 }} // namespace uhd::log
218 
221 #define _UHD_LOG_INTERNAL(component, level) \
222  uhd::_log::log(level, __FILE__, __LINE__, component, std::this_thread::get_id())
224 
225 // macro-style logging (compile-time determined)
226 #if UHD_LOG_MIN_LEVEL < 1
227 # define UHD_LOG_TRACE(component, message) \
228  _UHD_LOG_INTERNAL(component, uhd::log::trace) << message;
229 #else
230 # define UHD_LOG_TRACE(component, message)
231 #endif
232 
233 #if UHD_LOG_MIN_LEVEL < 2
234 # define UHD_LOG_DEBUG(component, message) \
235  _UHD_LOG_INTERNAL(component, uhd::log::debug) << message;
236 #else
237 # define UHD_LOG_DEBUG(component, message)
238 #endif
239 
240 #if UHD_LOG_MIN_LEVEL < 3
241 # define UHD_LOG_INFO(component, message) \
242  _UHD_LOG_INTERNAL(component, uhd::log::info) << message;
243 #else
244 # define UHD_LOG_INFO(component, message)
245 #endif
246 
247 #if UHD_LOG_MIN_LEVEL < 4
248 # define UHD_LOG_WARNING(component, message) \
249  _UHD_LOG_INTERNAL(component, uhd::log::warning) << message;
250 #else
251 # define UHD_LOG_WARNING(component, message)
252 #endif
253 
254 #if UHD_LOG_MIN_LEVEL < 5
255 # define UHD_LOG_ERROR(component, message) \
256  _UHD_LOG_INTERNAL(component, uhd::log::error) << message;
257 #else
258 # define UHD_LOG_ERROR(component, message)
259 #endif
260 
261 #define UHD_LOG_THROW(exception_type, component, message) \
262  { \
263  std::ostringstream __ss; \
264  __ss << message; \
265  UHD_LOG_ERROR(component, __ss.str()); \
266  throw exception_type(__ss.str()); \
267  }
268 
269 #if UHD_LOG_MIN_LEVEL < 6
270 # define UHD_LOG_FATAL(component, message) \
271  _UHD_LOG_INTERNAL(component, uhd::log::fatal) << message;
272 #else
273 # define UHD_LOG_FATAL(component, message)
274 #endif
275 
276 #define RFNOC_LOG_TRACE(message) UHD_LOG_TRACE(this->get_unique_id(), message)
277 #define RFNOC_LOG_DEBUG(message) UHD_LOG_DEBUG(this->get_unique_id(), message)
278 #define RFNOC_LOG_INFO(message) UHD_LOG_INFO(this->get_unique_id(), message)
279 #define RFNOC_LOG_WARNING(message) UHD_LOG_WARNING(this->get_unique_id(), message)
280 #define RFNOC_LOG_ERROR(message) UHD_LOG_ERROR(this->get_unique_id(), message)
281 #define RFNOC_LOG_FATAL(message) UHD_LOG_FATAL(this->get_unique_id(), message)
282 
283 #ifndef UHD_LOG_FASTPATH_DISABLE
290 # define UHD_LOG_FASTPATH(message) uhd::_log::log_fastpath(message);
291 #else
292 # define UHD_LOG_FASTPATH(message)
293 #endif
294 
295 // iostream-style logging
296 #define UHD_LOGGER_TRACE(component) _UHD_LOG_INTERNAL(component, uhd::log::trace)
297 #define UHD_LOGGER_DEBUG(component) _UHD_LOG_INTERNAL(component, uhd::log::debug)
298 #define UHD_LOGGER_INFO(component) _UHD_LOG_INTERNAL(component, uhd::log::info)
299 #define UHD_LOGGER_WARNING(component) _UHD_LOG_INTERNAL(component, uhd::log::warning)
300 #define UHD_LOGGER_ERROR(component) _UHD_LOG_INTERNAL(component, uhd::log::error)
301 #define UHD_LOGGER_FATAL(component) _UHD_LOG_INTERNAL(component, uhd::log::fatal)
302 
303 
304 #if defined(__GNUG__)
306 # define UHD_HERE() \
307  UHD_LOGGER_DEBUG("DEBUG") \
308  << __FILE__ << ":" << __LINE__ << " (" << UHD_PRETTY_FUNCTION << ")";
309 #else
311 # define UHD_HERE() UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__;
312 #endif
313 
315 #define UHD_VAR(var) UHD_LOGGER_DEBUG("DEBUG") << #var << " = " << var;
316 
318 #define UHD_HEX(var) \
319  UHD_LOGGER_DEBUG("DEBUG") << #var << " = 0x" << std::hex << std::setfill('0') \
320  << std::setw(8) << var << std::dec;
321 
323 namespace uhd {
324 namespace _log {
325 
327 void UHD_API log_fastpath(const std::string&);
328 
330 class UHD_API log
331 {
332 public:
333  log(const uhd::log::severity_level verbosity,
334  const std::string& file,
335  const unsigned int line,
336  const std::string& component,
337  const std::thread::id thread_id);
338 
339  ~log(void);
340 
341 // Macro for overloading insertion operators to avoid costly
342 // conversion of types if not logging.
343 #define INSERTION_OVERLOAD(x) \
344  log& operator<<(x) \
345  { \
346  if (_log_it) { \
347  _ss << val; \
348  } \
349  return *this; \
350  }
351 
352  // General insertion overload
353  template <typename T>
354  INSERTION_OVERLOAD(T val);
355 
356  // Insertion overloads for std::ostream manipulators
357  INSERTION_OVERLOAD(std::ostream& (*val)(std::ostream&));
358  INSERTION_OVERLOAD(std::ios& (*val)(std::ios&));
359  INSERTION_OVERLOAD(std::ios_base& (*val)(std::ios_base&));
360 
361 private:
363  std::ostringstream _ss;
364  const bool _log_it;
365 };
366 
367 } // namespace _log
369 } /* namespace uhd */
#define UHD_API
Definition: config.h:87
UHD_API std::optional< uhd::log::severity_level > parse_log_level_from_string_impl(const std::string &log_level_str)
UHD_API void set_logger_level(const std::string &logger, uhd::log::severity_level level)
T parse_log_level_from_string(const std::string &log_level_str)
Definition: log.hpp:179
UHD_API void set_console_level(uhd::log::severity_level level)
UHD_API void set_log_level(uhd::log::severity_level level)
UHD_API void set_file_level(uhd::log::severity_level level)
severity_level
Definition: log.hpp:119
@ warning
Definition: log.hpp:123
@ fatal
Definition: log.hpp:125
@ error
Definition: log.hpp:124
@ trace
Definition: log.hpp:120
@ off
Definition: log.hpp:126
@ info
Definition: log.hpp:122
@ debug
Definition: log.hpp:121
Definition: build_info.hpp:12
Definition: log.hpp:145
std::string message
Definition: log.hpp:168
uhd::log::severity_level verbosity
Definition: log.hpp:163
unsigned int line
Definition: log.hpp:165
std::chrono::system_clock::time_point time
Definition: log.hpp:162
std::string file
Definition: log.hpp:164
std::string component
Definition: log.hpp:166
std::thread::id thread_id
Definition: log.hpp:167
logging_info()
Definition: log.hpp:146
logging_info(const std::chrono::system_clock::time_point &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:147