USRP Hardware Driver and USRP Manual Version: 4.0.0.0
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/thread/thread.hpp>
13#include <iomanip>
14#include <iostream>
15#include <ostream>
16#include <sstream>
17#include <string>
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
97namespace uhd { namespace log {
104 trace = 0,
105 debug = 1,
106 info = 2,
108 error = 4,
109 fatal = 5,
110 off = 6,
111};
112
119{
120 logging_info() : verbosity(uhd::log::off) {}
121 logging_info(const boost::posix_time::ptime& time_,
122 const uhd::log::severity_level& verbosity_,
123 const std::string& file_,
124 const unsigned int& line_,
125 const std::string& component_,
126 const boost::thread::id& thread_id_)
127 : time(time_)
128 , verbosity(verbosity_)
129 , file(file_)
130 , line(line_)
131 , component(component_)
132 , thread_id(thread_id_)
133 { /* nop */
134 }
135
136 boost::posix_time::ptime time;
138 std::string file;
139 unsigned int line;
140 std::string component;
141 boost::thread::id thread_id;
142 std::string message;
143};
144
152
158
164
172UHD_API void set_logger_level(const std::string& logger, uhd::log::severity_level level);
173}} // namespace uhd::log
174
177#define _UHD_LOG_INTERNAL(component, level) \
178 uhd::_log::log(level, __FILE__, __LINE__, component, boost::this_thread::get_id())
180
181// macro-style logging (compile-time determined)
182#if UHD_LOG_MIN_LEVEL < 1
183# define UHD_LOG_TRACE(component, message) \
184 _UHD_LOG_INTERNAL(component, uhd::log::trace) << message;
185#else
186# define UHD_LOG_TRACE(component, message)
187#endif
188
189#if UHD_LOG_MIN_LEVEL < 2
190# define UHD_LOG_DEBUG(component, message) \
191 _UHD_LOG_INTERNAL(component, uhd::log::debug) << message;
192#else
193# define UHD_LOG_DEBUG(component, message)
194#endif
195
196#if UHD_LOG_MIN_LEVEL < 3
197# define UHD_LOG_INFO(component, message) \
198 _UHD_LOG_INTERNAL(component, uhd::log::info) << message;
199#else
200# define UHD_LOG_INFO(component, message)
201#endif
202
203#if UHD_LOG_MIN_LEVEL < 4
204# define UHD_LOG_WARNING(component, message) \
205 _UHD_LOG_INTERNAL(component, uhd::log::warning) << message;
206#else
207# define UHD_LOG_WARNING(component, message)
208#endif
209
210#if UHD_LOG_MIN_LEVEL < 5
211# define UHD_LOG_ERROR(component, message) \
212 _UHD_LOG_INTERNAL(component, uhd::log::error) << message;
213#else
214# define UHD_LOG_ERROR(component, message)
215#endif
216
217#if UHD_LOG_MIN_LEVEL < 6
218# define UHD_LOG_FATAL(component, message) \
219 _UHD_LOG_INTERNAL(component, uhd::log::fatal) << message;
220#else
221# define UHD_LOG_FATAL(component, message)
222#endif
223
224#define RFNOC_LOG_TRACE(message) UHD_LOG_TRACE(this->get_unique_id(), message)
225#define RFNOC_LOG_DEBUG(message) UHD_LOG_DEBUG(this->get_unique_id(), message)
226#define RFNOC_LOG_INFO(message) UHD_LOG_INFO(this->get_unique_id(), message)
227#define RFNOC_LOG_WARNING(message) UHD_LOG_WARNING(this->get_unique_id(), message)
228#define RFNOC_LOG_ERROR(message) UHD_LOG_ERROR(this->get_unique_id(), message)
229#define RFNOC_LOG_FATAL(message) UHD_LOG_FATAL(this->get_unique_id(), message)
230
231#ifndef UHD_LOG_FASTPATH_DISABLE
233// No metadata is tracked. Only the message is displayed. This does not go
234// through the regular backends. Mostly used for printing the UOSDL characters
235// during streaming.
236# define UHD_LOG_FASTPATH(message) uhd::_log::log_fastpath(message);
237#else
238# define UHD_LOG_FASTPATH(message)
239#endif
240
241// iostream-style logging
242#define UHD_LOGGER_TRACE(component) _UHD_LOG_INTERNAL(component, uhd::log::trace)
243#define UHD_LOGGER_DEBUG(component) _UHD_LOG_INTERNAL(component, uhd::log::debug)
244#define UHD_LOGGER_INFO(component) _UHD_LOG_INTERNAL(component, uhd::log::info)
245#define UHD_LOGGER_WARNING(component) _UHD_LOG_INTERNAL(component, uhd::log::warning)
246#define UHD_LOGGER_ERROR(component) _UHD_LOG_INTERNAL(component, uhd::log::error)
247#define UHD_LOGGER_FATAL(component) _UHD_LOG_INTERNAL(component, uhd::log::fatal)
248
249
250#if defined(__GNUG__)
252# define UHD_HERE() \
253 UHD_LOGGER_DEBUG("DEBUG") \
254 << __FILE__ << ":" << __LINE__ << " (" << __PRETTY_FUNCTION__ << ")";
255#else
257# define UHD_HERE() UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__;
258#endif
259
261#define UHD_VAR(var) UHD_LOGGER_DEBUG("DEBUG") << #var << " = " << var;
262
264#define UHD_HEX(var) \
265 UHD_LOGGER_DEBUG("DEBUG") << #var << " = 0x" << std::hex << std::setfill('0') \
266 << std::setw(8) << var << std::dec;
267
269namespace uhd {
270namespace _log {
271
273void UHD_API log_fastpath(const std::string&);
274
276class UHD_API log
277{
278public:
279 log(const uhd::log::severity_level verbosity,
280 const std::string& file,
281 const unsigned int line,
282 const std::string& component,
283 const boost::thread::id thread_id);
284
285 ~log(void);
286
287// Macro for overloading insertion operators to avoid costly
288// conversion of types if not logging.
289#define INSERTION_OVERLOAD(x) \
290 log& operator<<(x) \
291 { \
292 if (_log_it) { \
293 _ss << val; \
294 } \
295 return *this; \
296 }
297
298 // General insertion overload
299 template <typename T>
300 INSERTION_OVERLOAD(T val)
301
302 // Insertion overloads for std::ostream manipulators
303 INSERTION_OVERLOAD(std::ostream& (*val)(std::ostream&))
304 INSERTION_OVERLOAD(std::ios& (*val)(std::ios&))
305 INSERTION_OVERLOAD(std::ios_base& (*val)(std::ios_base&))
306
307 private : uhd::log::logging_info _log_info;
308 std::ostringstream _ss;
309 const bool _log_it;
310};
311
312} // namespace _log
314} /* namespace uhd */
#define UHD_API
Definition: config.h:67
UHD_API void set_logger_level(const std::string &logger, uhd::log::severity_level level)
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:103
@ warning
Definition: log.hpp:107
@ fatal
Definition: log.hpp:109
@ error
Definition: log.hpp:108
@ trace
Definition: log.hpp:104
@ off
Definition: log.hpp:110
@ info
Definition: log.hpp:106
@ debug
Definition: log.hpp:105
Definition: build_info.hpp:12
Definition: log.hpp:119
std::string file
Definition: log.hpp:138
logging_info()
Definition: log.hpp:120
std::string component
Definition: log.hpp:140
boost::posix_time::ptime time
Definition: log.hpp:136
unsigned int line
Definition: log.hpp:139
uhd::log::severity_level verbosity
Definition: log.hpp:137
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 boost::thread::id &thread_id_)
Definition: log.hpp:121
boost::thread::id thread_id
Definition: log.hpp:141
std::string message
Definition: log.hpp:142