Loading...
Searching...
No Matches
log.hpp
Go to the documentation of this file.
//
// Copyright 2011 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#pragma once
#include <uhd/config.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/optional.hpp>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <sstream>
#include <string>
#include <thread>
/*
* Advanced logging macros
* UHD_LOG_MIN_LEVEL definitions
* trace: 0
* debug: 1
* info: 2
* warning: 3
* error: 4
* fatal: 5
*/
namespace uhd { namespace log {
enum severity_level {
trace = 0,
debug = 1,
info = 2,
warning = 3,
error = 4,
fatal = 5,
off = 6,
};
boost::optional<uhd::log::severity_level> UHD_API parse_log_level_from_string(
const std::string& log_level_str);
struct UHD_API logging_info
{
logging_info() : verbosity(uhd::log::off) {}
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 std::thread::id& thread_id_)
: time(time_)
, verbosity(verbosity_)
, file(file_)
, line(line_)
, component(component_)
, thread_id(thread_id_)
{ /* nop */
}
boost::posix_time::ptime time;
uhd::log::severity_level verbosity;
std::string file;
unsigned int line;
std::string component;
std::thread::id thread_id;
std::string message;
};
UHD_API void set_log_level(uhd::log::severity_level level);
UHD_API void set_console_level(uhd::log::severity_level level);
UHD_API void set_file_level(uhd::log::severity_level level);
UHD_API void set_logger_level(const std::string& logger, uhd::log::severity_level level);
}} // namespace uhd::log
#define _UHD_LOG_INTERNAL(component, level) \
uhd::_log::log(level, __FILE__, __LINE__, component, std::this_thread::get_id())
// macro-style logging (compile-time determined)
#if UHD_LOG_MIN_LEVEL < 1
# define UHD_LOG_TRACE(component, message) \
_UHD_LOG_INTERNAL(component, uhd::log::trace) << message;
#else
# define UHD_LOG_TRACE(component, message)
#endif
#if UHD_LOG_MIN_LEVEL < 2
# define UHD_LOG_DEBUG(component, message) \
_UHD_LOG_INTERNAL(component, uhd::log::debug) << message;
#else
# define UHD_LOG_DEBUG(component, message)
#endif
#if UHD_LOG_MIN_LEVEL < 3
# define UHD_LOG_INFO(component, message) \
_UHD_LOG_INTERNAL(component, uhd::log::info) << message;
#else
# define UHD_LOG_INFO(component, message)
#endif
#if UHD_LOG_MIN_LEVEL < 4
# define UHD_LOG_WARNING(component, message) \
_UHD_LOG_INTERNAL(component, uhd::log::warning) << message;
#else
# define UHD_LOG_WARNING(component, message)
#endif
#if UHD_LOG_MIN_LEVEL < 5
# define UHD_LOG_ERROR(component, message) \
_UHD_LOG_INTERNAL(component, uhd::log::error) << message;
#else
# define UHD_LOG_ERROR(component, message)
#endif
#define UHD_LOG_THROW(exception_type, component, message) \
{ \
std::ostringstream __ss; \
__ss << message; \
UHD_LOG_ERROR(component, __ss.str()); \
throw exception_type(__ss.str()); \
}
#if UHD_LOG_MIN_LEVEL < 6
# define UHD_LOG_FATAL(component, message) \
_UHD_LOG_INTERNAL(component, uhd::log::fatal) << message;
#else
# define UHD_LOG_FATAL(component, message)
#endif
#define RFNOC_LOG_TRACE(message) UHD_LOG_TRACE(this->get_unique_id(), message)
#define RFNOC_LOG_DEBUG(message) UHD_LOG_DEBUG(this->get_unique_id(), message)
#define RFNOC_LOG_INFO(message) UHD_LOG_INFO(this->get_unique_id(), message)
#define RFNOC_LOG_WARNING(message) UHD_LOG_WARNING(this->get_unique_id(), message)
#define RFNOC_LOG_ERROR(message) UHD_LOG_ERROR(this->get_unique_id(), message)
#define RFNOC_LOG_FATAL(message) UHD_LOG_FATAL(this->get_unique_id(), message)
#ifndef UHD_LOG_FASTPATH_DISABLE
// No metadata is tracked. Only the message is displayed. This does not go
// through the regular backends. Mostly used for printing the UOSDL characters
// during streaming.
# define UHD_LOG_FASTPATH(message) uhd::_log::log_fastpath(message);
#else
# define UHD_LOG_FASTPATH(message)
#endif
// iostream-style logging
#define UHD_LOGGER_TRACE(component) _UHD_LOG_INTERNAL(component, uhd::log::trace)
#define UHD_LOGGER_DEBUG(component) _UHD_LOG_INTERNAL(component, uhd::log::debug)
#define UHD_LOGGER_INFO(component) _UHD_LOG_INTERNAL(component, uhd::log::info)
#define UHD_LOGGER_WARNING(component) _UHD_LOG_INTERNAL(component, uhd::log::warning)
#define UHD_LOGGER_ERROR(component) _UHD_LOG_INTERNAL(component, uhd::log::error)
#define UHD_LOGGER_FATAL(component) _UHD_LOG_INTERNAL(component, uhd::log::fatal)
#if defined(__GNUG__)
# define UHD_HERE() \
UHD_LOGGER_DEBUG("DEBUG") \
<< __FILE__ << ":" << __LINE__ << " (" << UHD_PRETTY_FUNCTION << ")";
#else
# define UHD_HERE() UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__;
#endif
#define UHD_VAR(var) UHD_LOGGER_DEBUG("DEBUG") << #var << " = " << var;
#define UHD_HEX(var) \
UHD_LOGGER_DEBUG("DEBUG") << #var << " = 0x" << std::hex << std::setfill('0') \
<< std::setw(8) << var << std::dec;
namespace uhd {
namespace _log {
void UHD_API log_fastpath(const std::string&);
class UHD_API log
{
public:
log(const uhd::log::severity_level verbosity,
const std::string& file,
const unsigned int line,
const std::string& component,
const std::thread::id thread_id);
~log(void);
// Macro for overloading insertion operators to avoid costly
// conversion of types if not logging.
#define INSERTION_OVERLOAD(x) \
log& operator<<(x) \
{ \
if (_log_it) { \
_ss << val; \
} \
return *this; \
}
// General insertion overload
template <typename T>
INSERTION_OVERLOAD(T val)
// Insertion overloads for std::ostream manipulators
INSERTION_OVERLOAD(std::ostream& (*val)(std::ostream&))
INSERTION_OVERLOAD(std::ios& (*val)(std::ios&))
INSERTION_OVERLOAD(std::ios_base& (*val)(std::ios_base&))
private : uhd::log::logging_info _log_info;
std::ostringstream _ss;
const bool _log_it;
};
} // namespace _log
} /* namespace uhd */
UHD_API
#define UHD_API
Definition config.h:87
config.hpp
uhd::log
Definition log.hpp:110
uhd::log::set_logger_level
UHD_API void set_logger_level(const std::string &logger, uhd::log::severity_level level)
uhd::log::set_console_level
UHD_API void set_console_level(uhd::log::severity_level level)
uhd::log::set_log_level
UHD_API void set_log_level(uhd::log::severity_level level)
uhd::log::parse_log_level_from_string
boost::optional< uhd::log::severity_level > UHD_API parse_log_level_from_string(const std::string &log_level_str)
uhd::log::set_file_level
UHD_API void set_file_level(uhd::log::severity_level level)
uhd::log::severity_level
severity_level
Definition log.hpp:116
uhd::log::warning
@ warning
Definition log.hpp:120
uhd::log::fatal
@ fatal
Definition log.hpp:122
uhd::log::error
@ error
Definition log.hpp:121
uhd::log::trace
@ trace
Definition log.hpp:117
uhd::log::off
@ off
Definition log.hpp:123
uhd::log::info
@ info
Definition log.hpp:119
uhd::log::debug
@ debug
Definition log.hpp:118
uhd
Definition build_info.hpp:12
uhd::log::logging_info
Definition log.hpp:138
uhd::log::logging_info::file
std::string file
Definition log.hpp:157
uhd::log::logging_info::thread_id
std::thread::id thread_id
Definition log.hpp:160
uhd::log::logging_info::logging_info
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 std::thread::id &thread_id_)
Definition log.hpp:140
uhd::log::logging_info::logging_info
logging_info()
Definition log.hpp:139
uhd::log::logging_info::component
std::string component
Definition log.hpp:159
uhd::log::logging_info::time
boost::posix_time::ptime time
Definition log.hpp:155
uhd::log::logging_info::line
unsigned int line
Definition log.hpp:158
uhd::log::logging_info::verbosity
uhd::log::severity_level verbosity
Definition log.hpp:156
uhd::log::logging_info::message
std::string message
Definition log.hpp:161