From 21aad77c9ca07f4271136b9241f5adb00a6bb908 Mon Sep 17 00:00:00 2001 From: Andrej Rode Date: Tue, 7 Feb 2017 16:37:25 -0800 Subject: utils: introduce new logging API and remove msg API --- host/include/uhd/rfnoc/node_ctrl_base.hpp | 2 +- host/include/uhd/rfnoc/node_ctrl_base.ipp | 2 +- host/include/uhd/transport/nirio/nirio_quirks.h | 4 +- .../include/uhd/transport/nirio/rpc/rpc_client.hpp | 4 +- host/include/uhd/types/filters.hpp | 4 +- host/include/uhd/utils/CMakeLists.txt | 1 - host/include/uhd/utils/fp_compare_delta.ipp | 2 +- host/include/uhd/utils/fp_compare_epsilon.ipp | 2 +- host/include/uhd/utils/log.hpp | 239 +++++++++++++++++---- host/include/uhd/utils/msg.hpp | 92 -------- host/include/uhd/utils/safe_call.hpp | 2 +- 11 files changed, 210 insertions(+), 144 deletions(-) delete mode 100644 host/include/uhd/utils/msg.hpp (limited to 'host/include') diff --git a/host/include/uhd/rfnoc/node_ctrl_base.hpp b/host/include/uhd/rfnoc/node_ctrl_base.hpp index 6fd6fd288..0505556ec 100644 --- a/host/include/uhd/rfnoc/node_ctrl_base.hpp +++ b/host/include/uhd/rfnoc/node_ctrl_base.hpp @@ -32,7 +32,7 @@ namespace uhd { namespace rfnoc { -#define UHD_RFNOC_BLOCK_TRACE() UHD_LOGV(never) << "[" << unique_id() << "] " +#define UHD_RFNOC_BLOCK_TRACE() UHD_LOGGER_TRACE("RFNOC") /*! \brief Abstract base class for streaming nodes. * diff --git a/host/include/uhd/rfnoc/node_ctrl_base.ipp b/host/include/uhd/rfnoc/node_ctrl_base.ipp index d300f72a7..df92e2e8b 100644 --- a/host/include/uhd/rfnoc/node_ctrl_base.ipp +++ b/host/include/uhd/rfnoc/node_ctrl_base.ipp @@ -21,7 +21,7 @@ #define INCLUDED_LIBUHD_NODE_CTRL_BASE_IPP #include -#include + #include #include diff --git a/host/include/uhd/transport/nirio/nirio_quirks.h b/host/include/uhd/transport/nirio/nirio_quirks.h index 45ef40394..f98432d6b 100644 --- a/host/include/uhd/transport/nirio/nirio_quirks.h +++ b/host/include/uhd/transport/nirio/nirio_quirks.h @@ -44,7 +44,7 @@ public: UHD_INLINE void add_tx_fifo(uint32_t index) { if (_tx_stream_fifo_indices.find(index) != _tx_stream_fifo_indices.end()) { if (_tx_stream_count == 0) { - UHD_LOG << "NI-RIO RX FIFO Transfer Check Quirk Enabled."; + UHD_LOGGER_DEBUG("NIRIO") << "NI-RIO RX FIFO Transfer Check Quirk Enabled."; } _tx_stream_count++; } @@ -54,7 +54,7 @@ public: if (_tx_stream_fifo_indices.find(index) != _tx_stream_fifo_indices.end()) { _tx_stream_count--; if (_tx_stream_count == 0) { - UHD_LOG << "NI-RIO RX FIFO Transfer Check Quirk Disabled."; + UHD_LOGGER_DEBUG("NIRIO") << "NI-RIO RX FIFO Transfer Check Quirk Disabled."; } } } diff --git a/host/include/uhd/transport/nirio/rpc/rpc_client.hpp b/host/include/uhd/transport/nirio/rpc/rpc_client.hpp index 78761c1fc..422f57e8a 100644 --- a/host/include/uhd/transport/nirio/rpc/rpc_client.hpp +++ b/host/include/uhd/transport/nirio/rpc/rpc_client.hpp @@ -65,11 +65,11 @@ private: inline void _stop_io_service() { if (_io_service_thread.get()) { - UHD_LOG << "rpc_client stopping..." << std::endl; + UHD_LOGGER_INFO("NIRIO") << "rpc_client stopping..."; _io_service.stop(); _io_service_thread->join(); _io_service_thread.reset(); - UHD_LOG << "rpc_client stopped." << std::endl; + UHD_LOGGER_INFO("NIRIO") << "rpc_client stopped."; } } diff --git a/host/include/uhd/types/filters.hpp b/host/include/uhd/types/filters.hpp index e3756b7a5..f91f35030 100644 --- a/host/include/uhd/types/filters.hpp +++ b/host/include/uhd/types/filters.hpp @@ -20,7 +20,7 @@ #include #include -#include + #include #include #include @@ -263,7 +263,7 @@ namespace uhd{ { std::size_t num_taps = taps.size(); if(num_taps < this->_max_num_taps){ - UHD_MSG(warning) << "digital_filter_fir::set_taps not enough coefficients. Appending zeros"; + UHD_LOGGER_WARNING("FILTERS") << "digital_filter_fir::set_taps not enough coefficients. Appending zeros"; std::vector coeffs; for (size_t i = 0; i < this->_max_num_taps; i++) { diff --git a/host/include/uhd/utils/CMakeLists.txt b/host/include/uhd/utils/CMakeLists.txt index af6d3ee47..5a6e9a48f 100644 --- a/host/include/uhd/utils/CMakeLists.txt +++ b/host/include/uhd/utils/CMakeLists.txt @@ -29,7 +29,6 @@ UHD_INSTALL(FILES gain_group.hpp log.hpp math.hpp - msg.hpp msg_task.hpp paths.hpp pimpl.hpp diff --git a/host/include/uhd/utils/fp_compare_delta.ipp b/host/include/uhd/utils/fp_compare_delta.ipp index 292ef4bf6..9a03bd95b 100644 --- a/host/include/uhd/utils/fp_compare_delta.ipp +++ b/host/include/uhd/utils/fp_compare_delta.ipp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include + #include #include diff --git a/host/include/uhd/utils/fp_compare_epsilon.ipp b/host/include/uhd/utils/fp_compare_epsilon.ipp index ff2d585db..af71046ed 100644 --- a/host/include/uhd/utils/fp_compare_epsilon.ipp +++ b/host/include/uhd/utils/fp_compare_epsilon.ipp @@ -15,7 +15,7 @@ // along with this program. If not, see . // -#include + #include #include diff --git a/host/include/uhd/utils/log.hpp b/host/include/uhd/utils/log.hpp index 106c0d9d5..d90fb4667 100644 --- a/host/include/uhd/utils/log.hpp +++ b/host/include/uhd/utils/log.hpp @@ -21,81 +21,219 @@ #include #include #include -#include +#include #include #include #include +#include /*! \file log.hpp - * The UHD logging facility. + * The UHD logging facility + * ========================= * - * The logger enables UHD library code to easily log events into a file. + * The logger enables UHD library code to easily log events into a file and display + * messages above a certain level in the terminal. * Log entries are time-stamped and stored with file, line, and function. - * Each call to the UHD_LOG macros is synchronous and thread-safe. + * Each call to the UHD_LOG macros is thread-safe. Each thread will aquire the lock + * for the logger. * - * The log file can be found in the path /uhd.log, - * where is the user or system's temporary directory. - * To override , set the UHD_TEMP_PATH environment variable. + * To disable console logging completely at compile time specify + * `-DUHD_LOG_CONSOLE_DISABLE` during configuration with CMake. + * + * By default no file logging will occur. Set a log file path: + * - at compile time by specifying `-DUHD_LOG_FILE=$file_path` + * - and/or override at runtime by setting the environment variable `UHD_LOG_FILE` + * + * Log levels + * ---------- * * All log messages with verbosity greater than or equal to the log level * (in other words, as often or less often than the current log level) - * are recorded into the log file. All other messages are sent to null. + * are recorded to std::clog and/or the log file. + * Log levels can be specified using string or numeric values of uhd::log::severity_level. * - * The default log level is "never", but can be overridden: - * - at compile time by setting the pre-processor define UHD_LOG_LEVEL. - * - at runtime by setting the environment variable UHD_LOG_LEVEL. + * The default log level is "info", but can be overridden: + * - at compile time by setting the pre-processor define `-DUHD_LOG_MIN_LEVEL`. + * - at runtime by setting the environment variable `UHD_LOG_LEVEL`. + * - for console logging by setting `(-D)UHD_LOG_CONSOLE_LEVEL` at run-/compiletime + * - for file logging by setting `(-D)UHD_LOG_FILE_LEVEL` at run-/compiletime * * UHD_LOG_LEVEL can be the name of a verbosity enum or integer value: - * - Example pre-processor define: -DUHD_LOG_LEVEL=3 - * - Example pre-processor define: -DUHD_LOG_LEVEL=regularly - * - Example environment variable: export UHD_LOG_LEVEL=3 - * - Example environment variable: export UHD_LOG_LEVEL=regularly + * - Example pre-processor define: `-DUHD_LOG_MIN_LEVEL=3` + * - Example pre-processor define: `-DUHD_LOG_MIN_LEVEL=info` + * - Example environment variable: `export UHD_LOG_LEVEL=3` + * - Example environment variable: `export UHD_LOG_LEVEL=info` + * + * Log formatting + * -------------- + * + * The log format for messages going into a log file is CSV. + * All log messages going into a logfile will contain following fields: + * - timestamp + * - thread-id + * - source-file + line information + * - severity level + * - component/channel information which logged the information + * - the actual log message + * + * The log format of log messages displayed on the terminal is plain text with space separated tags prepended. + * For example: + * - `[INFO] [x300] This is a informational log message` + * + * The log format for log output on the console by using these preprocessor defines in CMake: + * - `-DUHD_LOG_CONSOLE_TIME` adds a timestamp [2017-01-01 00:00:00.000000] + * - `-DUHD_LOG_CONSOLE_THREAD` adds a thread-id `[0x001234]` + * - `-DUHD_LOG_CONSOLE_SRC` adds a sourcefile and line tag `[src_file:line]` */ -/*! - * A UHD logger macro with configurable verbosity. - * Usage: UHD_LOGV(very_rarely) << "the log message" << std::endl; +/* + * Advanced logging macros + * UHD_LOG_MIN_LEVEL definitions + * trace: 0 + * debug: 1 + * info: 2 + * warning: 3 + * error: 4 + * fatal: 5 */ -#define UHD_LOGV(verbosity) \ - uhd::_log::log(uhd::_log::verbosity, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) -/*! - * A UHD logger macro with default verbosity. - * Usage: UHD_LOG << "the log message" << std::endl; - */ -#define UHD_LOG \ - UHD_LOGV(regularly) +namespace uhd { + namespace log { + /** logging severity levels + * Either numeric value or string can be used to define loglevel in CMake and environment variables + */ + enum severity_level { + trace = 0, /**< displays every available log message */ + debug = 1, /**< displays most log messages necessary for debugging internals */ + info = 2, /**< informational messages about setup and what is going on*/ + warning = 3, /**< something is not right but operation can continue */ + error = 4, /**< something has gone wrong */ + fatal = 5, /**< something has gone horribly wrong */ + off = 6, /**< logging is turned off */ + }; + // The operator is used when putting the severity level to log + template + inline std::basic_ostream& + operator<<(std::basic_ostream& strm, severity_level level) + { + static const char* strings[] = {"TRACE", "DEBUG", "INFO", + "WARNING", "ERROR", "FATAL" + }; -namespace uhd{ namespace _log{ + if (static_cast(level) < sizeof(strings) / sizeof(*strings)) { + strm << strings[level]; + } else { + strm << static_cast(level); + } - //! Verbosity levels for the logger - enum verbosity_t{ - always = 1, - often = 2, - regularly = 3, - rarely = 4, - very_rarely = 5, - never = 6 - }; + return strm; + } + + + } +} + + +// internal logging macro to be used in other macros +#define _UHD_LOG_INTERNAL(component, level) \ + uhd::_log::log(level, __FILE__, __LINE__, component, boost::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 + +#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 UHD_LOG_FASTPATH(message) \ + std::cerr << message << std::flush; + +// 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) + + +//! Helpful debug tool to print site info +#define UHD_HERE() \ + UHD_LOGGER_DEBUG("DEBUG") << __FILE__ << ":" << __LINE__; + +//! Helpful debug tool to print a variable +#define UHD_VAR(var) \ + UHD_LOGGER_DEBUG("DEBUG") << #var << " = " << var; + +//! Helpful debug tool to print a variable in hex +#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{ //! Internal logging object (called by UHD_LOG macros) class UHD_API log { public: log( - const verbosity_t verbosity, + const uhd::log::severity_level verbosity, const std::string &file, const unsigned int line, - const std::string &function + const std::string &component, + const boost::thread::id id ); ~log(void); + static void set_log_level(uhd::log::severity_level level); + static void set_console_level(uhd::log::severity_level level); + static void set_file_level(uhd::log::severity_level level); + // 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; \ + if(_log_it) { \ + _ss << val ; \ + } \ return *this; \ } @@ -110,9 +248,30 @@ namespace uhd{ namespace _log{ private: std::ostringstream _ss; + std::ostringstream _file; + std::ostringstream _console; bool _log_it; + bool _log_file; + bool _log_console; }; -}} //namespace uhd::_log + } //namespace uhd::_log + namespace log{ + inline void + set_console_level(severity_level level){ + ::uhd::_log::log::set_console_level(level); + } + + inline void + set_log_level(severity_level level){ + ::uhd::_log::log::set_log_level(level); + } + + inline void + set_file_level(severity_level level){ + ::uhd::_log::log::set_file_level(level); + } + } +} #endif /* INCLUDED_UHD_UTILS_LOG_HPP */ diff --git a/host/include/uhd/utils/msg.hpp b/host/include/uhd/utils/msg.hpp deleted file mode 100644 index 48ffb28a1..000000000 --- a/host/include/uhd/utils/msg.hpp +++ /dev/null @@ -1,92 +0,0 @@ -// -// Copyright 2011-2013 Ettus Research LLC -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -// - -#ifndef INCLUDED_UHD_UTILS_MSG_HPP -#define INCLUDED_UHD_UTILS_MSG_HPP - -#include -#include -#include -#include -#include - -/*! - * A UHD message macro with configurable type. - * Usage: UHD_MSG(warning) << "some warning message" << std::endl; - */ -#define UHD_MSG(type) \ - uhd::msg::_msg(uhd::msg::type)() - -//! Helpful debug tool to print site info -#define UHD_HERE() \ - UHD_MSG(status) << __FILE__ << ":" << __LINE__ << std::endl - -//! Helpful debug tool to print a variable -#define UHD_VAR(var) \ - UHD_MSG(status) << #var << " = " << var << std::endl; - -//! Helpful debug tool to print a variable in hex -#define UHD_HEX(var) \ - UHD_MSG(status) << #var << " = 0x" << std::hex << std::setfill('0') << std::setw(8) << var << std::dec << std::endl; - -namespace uhd{ namespace msg{ - - //! Possible message types - enum type_t{ - status = 's', - warning = 'w', - error = 'e', - fastpath= 'f' - }; - - //! Typedef for a user-registered message handler - typedef void (*handler_t)(type_t, const std::string &); - - /*! - * Default message handler for printing uhd system messages - * \param type message type, such as status, warning, or error - * \param msg contents of the system message as a string - */ - void default_msg_handler(type_t type, const std::string &msg); - - /*! - * Register the handler for uhd system messages. - * Only one handler can be registered at once. - * This replaces the default std::cout/cerr handler. - * \param handler a new handler callback function - */ - UHD_API void register_handler(const handler_t &handler); - - /*! - * Returns the current message handler for uhd system messages - * \returns the current message handler - */ - UHD_API const handler_t& get_handler(); - - //! Internal message object (called by UHD_MSG macro) - class UHD_API _msg{ - public: - _msg(const type_t type); - ~_msg(void); - std::ostream &operator()(void); - private: - UHD_PIMPL_DECL(impl) _impl; - }; - -}} //namespace uhd::msg - -#endif /* INCLUDED_UHD_UTILS_MSG_HPP */ diff --git a/host/include/uhd/utils/safe_call.hpp b/host/include/uhd/utils/safe_call.hpp index ab287cc66..d04d89436 100644 --- a/host/include/uhd/utils/safe_call.hpp +++ b/host/include/uhd/utils/safe_call.hpp @@ -23,7 +23,7 @@ #include //! helper macro for safe call to produce warnings -#define _UHD_SAFE_CALL_WARNING(code, what) UHD_LOGV(rarely) << \ +#define _UHD_SAFE_CALL_WARNING(code, what) UHD_LOGGER_ERROR("UHD") << \ UHD_THROW_SITE_INFO("Exception caught in safe-call.") + #code + " -> " + what \ ; -- cgit v1.2.3