diff options
author | Lane Kolbly <lane.kolbly@ni.com> | 2022-02-23 17:04:48 -0600 |
---|---|---|
committer | Aaron Rossetto <aaron.rossetto@ni.com> | 2022-03-11 09:10:04 -0600 |
commit | 7484b4f095e3bf7c16fc2f91dc1a78f04397bf01 (patch) | |
tree | 03954f38370ba6e96acdac6eff6de271c3250195 | |
parent | 6d58b692e4d26f662e6b2317e9c65043d398f00e (diff) | |
download | uhd-7484b4f095e3bf7c16fc2f91dc1a78f04397bf01.tar.gz uhd-7484b4f095e3bf7c16fc2f91dc1a78f04397bf01.tar.bz2 uhd-7484b4f095e3bf7c16fc2f91dc1a78f04397bf01.zip |
host: test: Add UHD_UNITTEST_LOG_LEVEL override
-rw-r--r-- | host/include/uhd/utils/log.hpp | 7 | ||||
-rw-r--r-- | host/lib/utils/log.cpp | 54 | ||||
-rw-r--r-- | host/tests/rfnoc_block_tests/x4xx_radio_mock.hpp | 15 |
3 files changed, 55 insertions, 21 deletions
diff --git a/host/include/uhd/utils/log.hpp b/host/include/uhd/utils/log.hpp index 7b76ec075..2ad7f5528 100644 --- a/host/include/uhd/utils/log.hpp +++ b/host/include/uhd/utils/log.hpp @@ -9,6 +9,7 @@ #include <uhd/config.hpp> #include <boost/date_time/posix_time/posix_time_types.hpp> +#include <boost/optional.hpp> #include <iomanip> #include <iostream> #include <ostream> @@ -122,6 +123,12 @@ enum severity_level { off = 6, /**< logging is turned off */ }; +/*! Parses a `severity_level` from a string. If a value could not be parsed, + * returns none. + */ +boost::optional<uhd::log::severity_level> UHD_API parse_log_level_from_string( + const std::string& log_level_str); + /*! Logging info structure * * Information needed to create a log entry is fully contained in the diff --git a/host/lib/utils/log.cpp b/host/lib/utils/log.cpp index cf18e00c7..031066e65 100644 --- a/host/lib/utils/log.cpp +++ b/host/lib/utils/log.cpp @@ -100,6 +100,35 @@ inline std::string path_to_filename(std::string path) } // namespace +namespace uhd { namespace log { + +boost::optional<uhd::log::severity_level> parse_log_level_from_string( + const std::string& log_level_str) +{ + if (std::isdigit(log_level_str[0])) { + const uhd::log::severity_level log_level_num = + uhd::log::severity_level(std::stoi(log_level_str)); + if (log_level_num >= uhd::log::trace && log_level_num <= uhd::log::fatal) { + return log_level_num; + } else { + std::cerr << "[LOG] Failed to set log level to: " << log_level_str; + return boost::none; + } + } + +#define if_loglevel_equal(name) else if (log_level_str == #name) return uhd::log::name + if_loglevel_equal(trace); + if_loglevel_equal(debug); + if_loglevel_equal(info); + if_loglevel_equal(warning); + if_loglevel_equal(error); + if_loglevel_equal(fatal); + if_loglevel_equal(off); + return boost::none; +} + +}} + /*********************************************************************** * Logger backends **********************************************************************/ @@ -407,26 +436,13 @@ private: uhd::log::severity_level _get_log_level( const std::string& log_level_str, const uhd::log::severity_level& previous_level) { - if (std::isdigit(log_level_str[0])) { - const uhd::log::severity_level log_level_num = - uhd::log::severity_level(std::stoi(log_level_str)); - if (log_level_num >= uhd::log::trace and log_level_num <= uhd::log::fatal) { - return log_level_num; - } else { - std::cerr << "[LOG] Failed to set log level to: " << log_level_str; - return previous_level; - } + boost::optional<uhd::log::severity_level> parsed_level = + uhd::log::parse_log_level_from_string(log_level_str); + if (parsed_level) { + return *parsed_level; + } else { + return previous_level; } - -#define if_loglevel_equal(name) else if (log_level_str == #name) return uhd::log::name - if_loglevel_equal(trace); - if_loglevel_equal(debug); - if_loglevel_equal(info); - if_loglevel_equal(warning); - if_loglevel_equal(error); - if_loglevel_equal(fatal); - if_loglevel_equal(off); - return previous_level; } void _setup_console_logging() diff --git a/host/tests/rfnoc_block_tests/x4xx_radio_mock.hpp b/host/tests/rfnoc_block_tests/x4xx_radio_mock.hpp index 21b8529cc..80492773b 100644 --- a/host/tests/rfnoc_block_tests/x4xx_radio_mock.hpp +++ b/host/tests/rfnoc_block_tests/x4xx_radio_mock.hpp @@ -211,12 +211,23 @@ public: */ constexpr size_t DEFAULT_MTU = 8000; -//! Helper class to make sure we get the most logging regardless of environment -// settings +//! Helper class to make sure we get the most logging. The logging level can be +// overridden using a special test-specific environment variable, +// `UHD_UNITTEST_LOG_LEVEL`. struct uhd_log_enabler { uhd_log_enabler(uhd::log::severity_level level) { + const char* env_p = std::getenv("UHD_UNITTEST_LOG_LEVEL"); + if (env_p) { + auto parsed_level = uhd::log::parse_log_level_from_string(env_p); + if (parsed_level) { + level = *parsed_level; + } else { + std::cout << "Unable to parse UHD_UNITTEST_LOG_LEVEL " << env_p << std::endl; + } + } + std::cout << "Setting log level to " << level << "..." << std::endl; uhd::log::set_log_level(level); uhd::log::set_console_level(level); |