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