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