diff options
author | Brent Stapleton <brent.stapleton@ettus.com> | 2019-04-04 10:24:48 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2019-04-11 16:46:05 -0700 |
commit | 497513c74d79d75cf9a1bbe4eb0f441ef99f339e (patch) | |
tree | 853b733704d80cb00d8751b7d93ec5cb01f07a47 | |
parent | c1e0e7329615806516d9ec3391a3fbf9e3a08852 (diff) | |
download | uhd-497513c74d79d75cf9a1bbe4eb0f441ef99f339e.tar.gz uhd-497513c74d79d75cf9a1bbe4eb0f441ef99f339e.tar.bz2 uhd-497513c74d79d75cf9a1bbe4eb0f441ef99f339e.zip |
log: formatting log.cpp
`clang-format -i --style=file host/lib/utils/log.cpp`
-rw-r--r-- | host/lib/utils/log.cpp | 301 |
1 files changed, 133 insertions, 168 deletions
diff --git a/host/lib/utils/log.cpp b/host/lib/utils/log.cpp index 8126d46b7..f2f0e24ff 100644 --- a/host/lib/utils/log.cpp +++ b/host/lib/utils/log.cpp @@ -5,19 +5,19 @@ // SPDX-License-Identifier: GPL-3.0-or-later // +#include <uhd/transport/bounded_buffer.hpp> #include <uhd/utils/log.hpp> #include <uhd/utils/log_add.hpp> -#include <uhd/utils/static.hpp> #include <uhd/utils/paths.hpp> -#include <uhd/transport/bounded_buffer.hpp> +#include <uhd/utils/static.hpp> #include <uhd/version.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/make_shared.hpp> -#include <fstream> -#include <cctype> #include <atomic> -#include <mutex> +#include <cctype> +#include <fstream> #include <memory> +#include <mutex> #include <thread> namespace pt = boost::posix_time; @@ -35,59 +35,59 @@ namespace pt = boost::posix_time; * Helpers **********************************************************************/ namespace { - std::string verbosity_color(const uhd::log::severity_level &level){ - switch(level){ +std::string verbosity_color(const uhd::log::severity_level& level) +{ + switch (level) { case (uhd::log::trace): return PURPLE; - case(uhd::log::debug): + case (uhd::log::debug): return BLUE; - case(uhd::log::info): + case (uhd::log::info): return GREEN; - case(uhd::log::warning): + case (uhd::log::warning): return YELLOW; - case(uhd::log::error): + case (uhd::log::error): return RED; - case(uhd::log::fatal): + case (uhd::log::fatal): return BRED; default: return RESET_COLORS; - } } +} - std::string verbosity_name(const uhd::log::severity_level &level){ - switch(level){ +std::string verbosity_name(const uhd::log::severity_level& level) +{ + switch (level) { case (uhd::log::trace): return "TRACE"; - case(uhd::log::debug): + case (uhd::log::debug): return "DEBUG"; - case(uhd::log::info): + case (uhd::log::info): return "INFO"; - case(uhd::log::warning): + case (uhd::log::warning): return "WARNING"; - case(uhd::log::error): + case (uhd::log::error): return "ERROR"; - case(uhd::log::fatal): + case (uhd::log::fatal): return "FATAL"; default: return "-"; - } - } - - //! get the relative file path from the host directory - inline std::string path_to_filename(std::string path) - { - return path.substr(path.find_last_of("/\\") + 1); } +} +//! get the relative file path from the host directory +inline std::string path_to_filename(std::string path) +{ + return path.substr(path.find_last_of("/\\") + 1); } +} // namespace + /*********************************************************************** * Logger backends **********************************************************************/ -void console_log( - const uhd::log::logging_info &log_info -) { - +void console_log(const uhd::log::logging_info& log_info) +{ std::clog #ifdef UHD_LOG_CONSOLE_COLOR << verbosity_color(log_info.verbosity) @@ -106,9 +106,7 @@ void console_log( #ifdef UHD_LOG_CONSOLE_COLOR << RESET_COLORS #endif - << log_info.message - << std::endl - ; + << log_info.message << std::endl; } /*! Helper class to implement file logging @@ -119,29 +117,27 @@ void console_log( class file_logger_backend { public: - file_logger_backend(const std::string &file_path) + file_logger_backend(const std::string& file_path) { _file_stream.exceptions(std::ofstream::failbit | std::ofstream::badbit); - if (!file_path.empty()){ + if (!file_path.empty()) { try { - _file_stream.open(file_path.c_str(), std::fstream::out | std::fstream::app); - } catch (const std::ofstream::failure& fail){ + _file_stream.open( + file_path.c_str(), std::fstream::out | std::fstream::app); + } catch (const std::ofstream::failure& fail) { std::cerr << "Error opening log file: " << fail.what() << std::endl; } } } - void log(const uhd::log::logging_info &log_info) + void log(const uhd::log::logging_info& log_info) { - if (_file_stream.is_open()){ - _file_stream - << pt::to_simple_string(log_info.time) << "," - << "0x" << log_info.thread_id << "," - << path_to_filename(log_info.file) << ":" << log_info.line << "," - << log_info.verbosity << "," - << log_info.component << "," - << log_info.message - << std::endl; + if (_file_stream.is_open()) { + _file_stream << pt::to_simple_string(log_info.time) << "," + << "0x" << log_info.thread_id << "," + << path_to_filename(log_info.file) << ":" << log_info.line << "," + << log_info.verbosity << "," << log_info.component << "," + << log_info.message << std::endl; ; } } @@ -149,7 +145,7 @@ public: ~file_logger_backend() { - if (_file_stream.is_open()){ + if (_file_stream.is_open()) { _file_stream.close(); } } @@ -165,30 +161,30 @@ private: #define UHD_CONSOLE_LOGGER_KEY "console" #define UHD_FILE_LOGGER_KEY "file" -class log_resource { +class log_resource +{ public: uhd::log::severity_level global_level; - log_resource(void): - global_level(uhd::log::off), - _exit(false), + log_resource(void) + : global_level(uhd::log::off) + , _exit(false) + , #ifndef UHD_LOG_FASTPATH_DISABLE - _fastpath_queue(10), + _fastpath_queue(10) + , #endif _log_queue(10) { - //allow override from macro definition + // allow override from macro definition #ifdef UHD_LOG_MIN_LEVEL - this->global_level = _get_log_level( - BOOST_STRINGIZE(UHD_LOG_MIN_LEVEL), - this->global_level - ); + this->global_level = + _get_log_level(BOOST_STRINGIZE(UHD_LOG_MIN_LEVEL), this->global_level); #endif - //allow override from environment variables - const char * log_level_env = std::getenv("UHD_LOG_LEVEL"); + // allow override from environment variables + const char* log_level_env = std::getenv("UHD_LOG_LEVEL"); if (log_level_env != NULL && log_level_env[0] != '\0') { - this->global_level = - _get_log_level(log_level_env, this->global_level); + this->global_level = _get_log_level(log_level_env, this->global_level); } // Setup default loggers (console and file) @@ -198,28 +194,22 @@ public: // On boot, we print the current UHD version info: { std::ostringstream sys_info; - sys_info \ - << BOOST_PLATFORM << "; " - << BOOST_COMPILER << "; " - << "Boost_" - << BOOST_VERSION << "; " - << "UHD_" << uhd::get_version_string(); + sys_info << BOOST_PLATFORM << "; " << BOOST_COMPILER << "; " + << "Boost_" << BOOST_VERSION << "; " + << "UHD_" << uhd::get_version_string(); _publish_log_msg(sys_info.str(), uhd::log::info, "UHD"); } // Launch log message consumer - _pop_task = std::make_shared<std::thread>( - std::thread([this](){this->pop_task();}) - ); + _pop_task = + std::make_shared<std::thread>(std::thread([this]() { this->pop_task(); })); // Fastpath message consumer #ifndef UHD_LOG_FASTPATH_DISABLE - //allow override from environment variables - const bool enable_fastpath = [](){ - const char* disable_fastpath_env = - std::getenv("UHD_LOG_FASTPATH_DISABLE"); - if (disable_fastpath_env != NULL - && disable_fastpath_env[0] != '\0') { + // allow override from environment variables + const bool enable_fastpath = []() { + const char* disable_fastpath_env = std::getenv("UHD_LOG_FASTPATH_DISABLE"); + if (disable_fastpath_env != NULL && disable_fastpath_env[0] != '\0') { return false; } return true; @@ -227,12 +217,10 @@ public: if (enable_fastpath) { _pop_fastpath_task = std::make_shared<std::thread>( - std::thread([this](){this->pop_fastpath_task();}) - ); + std::thread([this]() { this->pop_fastpath_task(); })); } else { _pop_fastpath_task = std::make_shared<std::thread>( - std::thread([this](){this->pop_fastpath_dummy_task();}) - ); + std::thread([this]() { this->pop_fastpath_dummy_task(); })); _publish_log_msg("Fastpath logging disabled at runtime."); } #else @@ -242,19 +230,18 @@ public: #endif } - ~log_resource(void){ + ~log_resource(void) + { _exit = true; // We push a final message to kick the pop task out of it's wait state. // This wouldn't be necessary if pop_with_wait() could fail. Should // that ever get fixed, we can remove this. - auto final_message = uhd::log::logging_info( - pt::microsec_clock::local_time(), - uhd::log::trace, - __FILE__, - __LINE__, - "LOGGING", - boost::this_thread::get_id() - ); + auto final_message = uhd::log::logging_info(pt::microsec_clock::local_time(), + uhd::log::trace, + __FILE__, + __LINE__, + "LOGGING", + boost::this_thread::get_id()); final_message.message = ""; push(final_message); #ifndef UHD_LOG_FASTPATH_DISABLE @@ -279,7 +266,7 @@ public: } #ifndef UHD_LOG_FASTPATH_DISABLE - void push_fastpath(const std::string &message) + void push_fastpath(const std::string& message) { // Never wait. If the buffer is full, we just don't see the message. // Too bad. @@ -295,7 +282,7 @@ public: std::lock_guard<std::mutex> l(_logmap_mutex); for (const auto& logger_pair : _loggers) { const auto& logger = logger_pair.second; - if (log_info.verbosity < logger.first){ + if (log_info.verbosity < logger.first) { continue; } logger.second(log_info); @@ -346,22 +333,19 @@ public: } // Exit procedure: Clear the queue - while (_fastpath_queue.pop_with_haste(msg)); + while (_fastpath_queue.pop_with_haste(msg)) + ; #endif } - void add_logger( - const std::string &key, - uhd::log::log_fn_t logger_fn - ) { + void add_logger(const std::string& key, uhd::log::log_fn_t logger_fn) + { std::lock_guard<std::mutex> l(_logmap_mutex); _loggers[key] = level_logfn_pair{global_level, logger_fn}; } - void set_log_level( - const std::string &key, - const uhd::log::severity_level level - ) { + void set_log_level(const std::string& key, const uhd::log::severity_level level) + { std::lock_guard<std::mutex> l(_logmap_mutex); _loggers[key].first = level; } @@ -371,22 +355,22 @@ private: #ifndef UHD_LOG_FASTPATH_DISABLE std::shared_ptr<std::thread> _pop_fastpath_task; #endif - uhd::log::severity_level _get_log_level(const std::string &log_level_str, - const uhd::log::severity_level &previous_level){ + 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) { + if (log_level_num >= uhd::log::trace and log_level_num <= uhd::log::fatal) { return log_level_num; - }else{ - UHD_LOGGER_ERROR("LOG") << "Failed to set log level to: " << log_level_str; + } else { + UHD_LOGGER_ERROR("LOG") + << "Failed to set log level to: " << log_level_str; return previous_level; } } -#define if_loglevel_equal(name) \ - else if (log_level_str == #name) return uhd::log::name +#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); @@ -401,20 +385,15 @@ private: { #ifndef UHD_LOG_CONSOLE_DISABLE uhd::log::severity_level console_level = uhd::log::trace; -#ifdef UHD_LOG_CONSOLE_LEVEL - console_level = _get_log_level( - BOOST_STRINGIZE(UHD_LOG_CONSOLE_LEVEL), - console_level - ); -#endif - const char* log_console_level_env = - std::getenv("UHD_LOG_CONSOLE_LEVEL"); +# ifdef UHD_LOG_CONSOLE_LEVEL + console_level = + _get_log_level(BOOST_STRINGIZE(UHD_LOG_CONSOLE_LEVEL), console_level); +# endif + const char* log_console_level_env = std::getenv("UHD_LOG_CONSOLE_LEVEL"); if (log_console_level_env != NULL && log_console_level_env[0] != '\0') { - console_level = - _get_log_level(log_console_level_env, console_level); + console_level = _get_log_level(log_console_level_env, console_level); } - _loggers[UHD_CONSOLE_LOGGER_KEY] = - level_logfn_pair{console_level, &console_log}; + _loggers[UHD_CONSOLE_LOGGER_KEY] = level_logfn_pair{console_level, &console_log}; #endif } @@ -423,52 +402,43 @@ private: uhd::log::severity_level file_level = uhd::log::trace; std::string log_file_target; #if defined(UHD_LOG_FILE_LEVEL) - file_level = _get_log_level( - BOOST_STRINGIZE(UHD_LOG_FILE_LEVEL), - file_level - ); + file_level = _get_log_level(BOOST_STRINGIZE(UHD_LOG_FILE_LEVEL), file_level); #endif #if defined(UHD_LOG_FILE) log_file_target = BOOST_STRINGIZE(UHD_LOG_FILE); #endif const char* log_file_level_env = std::getenv("UHD_LOG_FILE_LEVEL"); - if (log_file_level_env != NULL && log_file_level_env[0] != '\0'){ + if (log_file_level_env != NULL && log_file_level_env[0] != '\0') { file_level = _get_log_level(log_file_level_env, file_level); } const char* log_file_env = std::getenv("UHD_LOG_FILE"); if ((log_file_env != NULL) && (log_file_env[0] != '\0')) { log_file_target = std::string(log_file_env); } - if (!log_file_target.empty()){ + if (!log_file_target.empty()) { auto F = std::make_shared<file_logger_backend>(log_file_target); - _loggers[UHD_FILE_LOGGER_KEY] = level_logfn_pair{ - file_level, - [F](const uhd::log::logging_info& log_info){F->log(log_info);} - }; + _loggers[UHD_FILE_LOGGER_KEY] = level_logfn_pair{file_level, + [F](const uhd::log::logging_info& log_info) { F->log(log_info); }}; } } - void _publish_log_msg( - const std::string& msg, - const uhd::log::severity_level level=uhd::log::info, - const std::string& component="LOGGING" - ) { - auto log_msg = uhd::log::logging_info( - pt::microsec_clock::local_time(), + void _publish_log_msg(const std::string& msg, + const uhd::log::severity_level level = uhd::log::info, + const std::string& component = "LOGGING") + { + auto log_msg = uhd::log::logging_info(pt::microsec_clock::local_time(), level, __FILE__, __LINE__, component, - boost::this_thread::get_id() - ); + boost::this_thread::get_id()); log_msg.message = msg; _log_queue.push_with_timed_wait(log_msg, 0.25); } std::mutex _logmap_mutex; std::atomic<bool> _exit; - using level_logfn_pair = - std::pair<uhd::log::severity_level, uhd::log::log_fn_t>; + using level_logfn_pair = std::pair<uhd::log::severity_level, uhd::log::log_fn_t>; std::map<std::string, level_logfn_pair> _loggers; #ifndef UHD_LOG_FASTPATH_DISABLE uhd::transport::bounded_buffer<std::string> _fastpath_queue; @@ -481,18 +451,15 @@ UHD_SINGLETON_FCN(log_resource, log_rs); /*********************************************************************** * The logger object implementation **********************************************************************/ -uhd::_log::log::log( - const uhd::log::severity_level verbosity, - const std::string &file, +uhd::_log::log::log(const uhd::log::severity_level verbosity, + const std::string& file, const unsigned int line, - const std::string &component, - const boost::thread::id thread_id - ) : - _log_it(verbosity >= log_rs().global_level) + const std::string& component, + const boost::thread::id thread_id) + : _log_it(verbosity >= log_rs().global_level) { - if (_log_it){ - this->_log_info = uhd::log::logging_info( - pt::microsec_clock::local_time(), + if (_log_it) { + this->_log_info = uhd::log::logging_info(pt::microsec_clock::local_time(), verbosity, file, line, @@ -505,20 +472,20 @@ uhd::_log::log::~log(void) { if (_log_it) { this->_log_info.message = _ss.str(); - try{ + try { log_rs().push(this->_log_info); + } catch (...) { } - catch (...){} } } #ifndef UHD_LOG_FASTPATH_DISABLE -void uhd::_log::log_fastpath(const std::string &msg) +void uhd::_log::log_fastpath(const std::string& msg) { log_rs().push_fastpath(msg); } #else -void uhd::_log::log_fastpath(const std::string &) +void uhd::_log::log_fastpath(const std::string&) { // nop } @@ -527,29 +494,27 @@ void uhd::_log::log_fastpath(const std::string &) /*********************************************************************** * Public API calls **********************************************************************/ -void -uhd::log::add_logger(const std::string &key, log_fn_t logger_fn) +void uhd::log::add_logger(const std::string& key, log_fn_t logger_fn) { log_rs().add_logger(key, logger_fn); } -void -uhd::log::set_log_level(uhd::log::severity_level level){ +void uhd::log::set_log_level(uhd::log::severity_level level) +{ log_rs().global_level = level; } -void -uhd::log::set_logger_level(const std::string &key, uhd::log::severity_level level){ +void uhd::log::set_logger_level(const std::string& key, uhd::log::severity_level level) +{ log_rs().set_log_level(key, level); } -void -uhd::log::set_console_level(uhd::log::severity_level level){ +void uhd::log::set_console_level(uhd::log::severity_level level) +{ set_logger_level(UHD_CONSOLE_LOGGER_KEY, level); } -void -uhd::log::set_file_level(uhd::log::severity_level level){ +void uhd::log::set_file_level(uhd::log::severity_level level) +{ set_logger_level(UHD_FILE_LOGGER_KEY, level); } - |