USRP Hardware Driver and USRP Manual  Version: 3.11.0.HEAD-0-gdca39145
UHD and USRP Manual
log.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2011 Ettus Research LLC
3 //
4 // SPDX-License-Identifier: GPL-3.0
5 //
6 
7 #ifndef INCLUDED_UHD_UTILS_LOG_HPP
8 #define INCLUDED_UHD_UTILS_LOG_HPP
9 
10 #include <uhd/config.hpp>
11 #include <boost/current_function.hpp>
12 #include <boost/thread/thread.hpp>
13 #include <ostream>
14 #include <string>
15 #include <sstream>
16 #include <iostream>
17 
85 /*
86  * Advanced logging macros
87  * UHD_LOG_MIN_LEVEL definitions
88  * trace: 0
89  * debug: 1
90  * info: 2
91  * warning: 3
92  * error: 4
93  * fatal: 5
94  */
95 
96 namespace uhd {
97  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 
120  : verbosity(uhd::log::off) {}
122  const boost::posix_time::ptime &time_,
123  const uhd::log::severity_level &verbosity_,
124  const std::string &file_,
125  const size_t &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  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  }
174 }
175 
178 #define _UHD_LOG_INTERNAL(component, level) \
179  uhd::_log::log(level, __FILE__, __LINE__, component, boost::this_thread::get_id())
180 
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
226 // 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) \
231  uhd::_log::log_fastpath(message);
232 #else
233 #define UHD_LOG_FASTPATH(message)
234 #endif
235 
236 // iostream-style logging
237 #define UHD_LOGGER_TRACE(component) _UHD_LOG_INTERNAL(component, uhd::log::trace)
238 #define UHD_LOGGER_DEBUG(component) _UHD_LOG_INTERNAL(component, uhd::log::debug)
239 #define UHD_LOGGER_INFO(component) _UHD_LOG_INTERNAL(component, uhd::log::info)
240 #define UHD_LOGGER_WARNING(component) _UHD_LOG_INTERNAL(component, uhd::log::warning)
241 #define UHD_LOGGER_ERROR(component) _UHD_LOG_INTERNAL(component, uhd::log::error)
242 #define UHD_LOGGER_FATAL(component) _UHD_LOG_INTERNAL(component, uhd::log::fatal)
243 
244 
245 #if defined(__GNUG__)
246 #define UHD_HERE() \
248  UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__ << " (" << __PRETTY_FUNCTION__ << ")";
249 #else
250 #define UHD_HERE() \
252  UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__;
253 #endif
254 
256 #define UHD_VAR(var) \
257  UHD_LOGGER_DEBUG("DEBUG") << #var << " = " << var;
258 
260 #define UHD_HEX(var) \
261  UHD_LOGGER_DEBUG("DEBUG") << #var << " = 0x" << std::hex << std::setfill('0') << std::setw(8) << var << std::dec;
262 
264 namespace uhd{ namespace _log {
265 
267  void UHD_API log_fastpath(const std::string &msg);
268 
270  class UHD_API log {
271  public:
272  log(
273  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 
280  ~log(void);
281 
282  // Macro for overloading insertion operators to avoid costly
283  // conversion of types if not logging.
284  #define INSERTION_OVERLOAD(x) 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:
302  uhd::log::logging_info _log_info;
303  std::ostringstream _ss;
304  const bool _log_it;
305  };
306 
307 } //namespace uhd::_log
309 } /* namespace uhd */
310 
311 #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:119
severity_level
Definition: log.hpp:103
Definition: log.hpp:104
Definition: log.hpp:110
logging_info(const boost::posix_time::ptime &time_, const uhd::log::severity_level &verbosity_, const std::string &file_, const size_t &line_, const std::string &component_, const boost::thread::id &thread_id_)
Definition: log.hpp:121
Definition: build_info.hpp:14
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:62
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
Definition: log.hpp:109