USRP Hardware Driver and USRP Manual  Version: 3.15.0.HEAD-0-g6563c537
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 <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 
97 namespace uhd { namespace log {
104  trace = 0,
105  debug = 1,
106  info = 2,
107  warning = 3,
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 
172 UHD_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())
179 
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 #ifndef UHD_LOG_FASTPATH_DISABLE
225 // No metadata is tracked. Only the message is displayed. This does not go
227 // through the regular backends. Mostly used for printing the UOSDL characters
228 // during streaming.
229 # define UHD_LOG_FASTPATH(message) uhd::_log::log_fastpath(message);
230 #else
231 # define UHD_LOG_FASTPATH(message)
232 #endif
233 
234 // iostream-style logging
235 #define UHD_LOGGER_TRACE(component) _UHD_LOG_INTERNAL(component, uhd::log::trace)
236 #define UHD_LOGGER_DEBUG(component) _UHD_LOG_INTERNAL(component, uhd::log::debug)
237 #define UHD_LOGGER_INFO(component) _UHD_LOG_INTERNAL(component, uhd::log::info)
238 #define UHD_LOGGER_WARNING(component) _UHD_LOG_INTERNAL(component, uhd::log::warning)
239 #define UHD_LOGGER_ERROR(component) _UHD_LOG_INTERNAL(component, uhd::log::error)
240 #define UHD_LOGGER_FATAL(component) _UHD_LOG_INTERNAL(component, uhd::log::fatal)
241 
242 
243 #if defined(__GNUG__)
244 # define UHD_HERE() \
246  UHD_LOGGER_DEBUG("DEBUG") \
247  << __FILE__ << ":" << __LINE__ << " (" << __PRETTY_FUNCTION__ << ")";
248 #else
249 # define UHD_HERE() UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__;
251 #endif
252 
254 #define UHD_VAR(var) UHD_LOGGER_DEBUG("DEBUG") << #var << " = " << var;
255 
257 #define UHD_HEX(var) \
258  UHD_LOGGER_DEBUG("DEBUG") << #var << " = 0x" << std::hex << std::setfill('0') \
259  << std::setw(8) << var << std::dec;
260 
262 namespace uhd {
263 namespace _log {
264 
266 void UHD_API log_fastpath(const std::string&);
267 
269 class UHD_API log
270 {
271 public:
272  log(const uhd::log::severity_level verbosity,
273  const std::string& file,
274  const unsigned int line,
275  const std::string& component,
276  const boost::thread::id thread_id);
277 
278  ~log(void);
279 
280 // Macro for overloading insertion operators to avoid costly
281 // conversion of types if not logging.
282 #define INSERTION_OVERLOAD(x) \
283  log& operator<<(x) \
284  { \
285  if (_log_it) { \
286  _ss << val; \
287  } \
288  return *this; \
289  }
290 
291  // General insertion overload
292  template <typename T>
293  INSERTION_OVERLOAD(T val)
294 
295  // Insertion overloads for std::ostream manipulators
296  INSERTION_OVERLOAD(std::ostream& (*val)(std::ostream&))
297  INSERTION_OVERLOAD(std::ios& (*val)(std::ios&))
298  INSERTION_OVERLOAD(std::ios_base& (*val)(std::ios_base&))
299 
300  private : uhd::log::logging_info _log_info;
301  std::ostringstream _ss;
302  const bool _log_it;
303 };
304 
305 } // namespace _log
307 } /* namespace uhd */
308 
309 #endif /* INCLUDED_UHD_UTILS_LOG_HPP */
boost::posix_time::ptime time
Definition: log.hpp:136
Definition: log.hpp:106
std::string file
Definition: log.hpp:138
unsigned int line
Definition: log.hpp:139
logging_info()
Definition: log.hpp:120
severity_level
Definition: log.hpp:103
Definition: log.hpp:104
Definition: log.hpp:110
Definition: build_info.hpp:13
UHD_API void set_file_level(uhd::log::severity_level level)
uhd::log::severity_level verbosity
Definition: log.hpp:137
Definition: log.hpp:118
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)
boost::thread::id thread_id
Definition: log.hpp:141
Definition: log.hpp:108
Definition: log.hpp:107
Definition: log.hpp:105
#define UHD_API
Definition: config.h:68
std::string message
Definition: log.hpp:142
UHD_API void set_console_level(uhd::log::severity_level level)
std::string component
Definition: log.hpp:140
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
Definition: log.hpp:109