diff options
Diffstat (limited to 'host/lib/utils')
| -rw-r--r-- | host/lib/utils/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | host/lib/utils/csv.cpp | 3 | ||||
| -rw-r--r-- | host/lib/utils/gain_group.cpp | 17 | ||||
| -rw-r--r-- | host/lib/utils/load_modules.cpp | 3 | ||||
| -rw-r--r-- | host/lib/utils/log.cpp | 474 | ||||
| -rw-r--r-- | host/lib/utils/msg.cpp | 130 | ||||
| -rw-r--r-- | host/lib/utils/paths.cpp | 9 | ||||
| -rw-r--r-- | host/lib/utils/tasks.cpp | 18 | ||||
| -rw-r--r-- | host/lib/utils/thread_priority.cpp | 6 | ||||
| -rw-r--r-- | host/lib/utils/thread_priority_c.cpp | 2 | 
10 files changed, 398 insertions, 265 deletions
| diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt index 128d7c00a..f76c5763a 100644 --- a/host/lib/utils/CMakeLists.txt +++ b/host/lib/utils/CMakeLists.txt @@ -142,7 +142,6 @@ LIBUHD_APPEND_SOURCES(      ${CMAKE_CURRENT_SOURCE_DIR}/ihex.cpp      ${CMAKE_CURRENT_SOURCE_DIR}/load_modules.cpp      ${CMAKE_CURRENT_SOURCE_DIR}/log.cpp -    ${CMAKE_CURRENT_SOURCE_DIR}/msg.cpp      ${CMAKE_CURRENT_SOURCE_DIR}/paths.cpp      ${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp      ${CMAKE_CURRENT_SOURCE_DIR}/static.cpp diff --git a/host/lib/utils/csv.cpp b/host/lib/utils/csv.cpp index 2ffa70196..e0cadcb96 100644 --- a/host/lib/utils/csv.cpp +++ b/host/lib/utils/csv.cpp @@ -16,7 +16,6 @@  //  #include <uhd/utils/csv.hpp> -#include <boost/foreach.hpp>  using namespace uhd; @@ -29,7 +28,7 @@ csv::rows_type csv::to_rows(std::istream &input){          bool in_quote = false;          char last_ch, next_ch = ' ';          //for each character in the line -        BOOST_FOREACH(char ch, line){ +        for(char ch:  line){              last_ch = next_ch;              next_ch = ch;              //catch a quote character and change the state diff --git a/host/lib/utils/gain_group.cpp b/host/lib/utils/gain_group.cpp index 71caf33be..1a6c0407f 100644 --- a/host/lib/utils/gain_group.cpp +++ b/host/lib/utils/gain_group.cpp @@ -20,7 +20,6 @@  #include <uhd/types/dict.hpp>  #include <uhd/utils/algorithm.hpp>  #include <uhd/exception.hpp> -#include <boost/foreach.hpp>  #include <boost/bind.hpp>  #include <algorithm>  #include <vector> @@ -73,7 +72,7 @@ public:          if (not name.empty()) return _name_to_fcns.get(name).get_range();          double overall_min = 0, overall_max = 0, overall_step = 0; -        BOOST_FOREACH(const gain_fcns_t &fcns, get_all_fcns()){ +        for(const gain_fcns_t &fcns:  get_all_fcns()){              const gain_range_t range = fcns.get_range();              overall_min += range.start();              overall_max += range.stop(); @@ -88,7 +87,7 @@ public:          if (not name.empty()) return _name_to_fcns.get(name).get_value();          double overall_gain = 0; -        BOOST_FOREACH(const gain_fcns_t &fcns, get_all_fcns()){ +        for(const gain_fcns_t &fcns:  get_all_fcns()){              overall_gain += fcns.get_value();          }          return overall_gain; @@ -102,7 +101,7 @@ public:          //get the max step size among the gains          double max_step = 0; -        BOOST_FOREACH(const gain_fcns_t &fcns, all_fcns){ +        for(const gain_fcns_t &fcns:  all_fcns){              max_step = std::max(max_step, fcns.get_range().step());          } @@ -111,7 +110,7 @@ public:          //distribute power according to priority (round to max step)          double gain_left_to_distribute = gain; -        BOOST_FOREACH(const gain_fcns_t &fcns, all_fcns){ +        for(const gain_fcns_t &fcns:  all_fcns){              const gain_range_t range = fcns.get_range();              gain_bucket.push_back(floor_step(uhd::clip(                  gain_left_to_distribute, range.start(), range.stop() @@ -135,7 +134,7 @@ public:          //distribute the remainder (less than max step)          //fill in the largest step sizes first that are less than the remainder -        BOOST_FOREACH(size_t i, indexes_step_size_dec){ +        for(size_t i:  indexes_step_size_dec){              const gain_range_t range = all_fcns.at(i).get_range();              double additional_gain = floor_step(uhd::clip(                  gain_bucket.at(i) + gain_left_to_distribute, range.start(), range.stop() @@ -143,11 +142,11 @@ public:              gain_bucket.at(i) += additional_gain;              gain_left_to_distribute -= additional_gain;          } -        UHD_LOGV(often) << "gain_left_to_distribute " << gain_left_to_distribute << std::endl; +        UHD_LOGGER_DEBUG("UHD") << "gain_left_to_distribute " << gain_left_to_distribute ;          //now write the bucket out to the individual gain values          for (size_t i = 0; i < gain_bucket.size(); i++){ -            UHD_LOGV(often) << i << ": " << gain_bucket.at(i) << std::endl; +            UHD_LOGGER_DEBUG("UHD") << i << ": " << gain_bucket.at(i) ;              all_fcns.at(i).set_value(gain_bucket.at(i));          }      } @@ -173,7 +172,7 @@ private:      //! get the gain function sets in order (highest priority first)      std::vector<gain_fcns_t> get_all_fcns(void){          std::vector<gain_fcns_t> all_fcns; -        BOOST_FOREACH(size_t key, uhd::sorted(_registry.keys())){ +        for(size_t key:  uhd::sorted(_registry.keys())){              const std::vector<gain_fcns_t> &fcns = _registry[key];              all_fcns.insert(all_fcns.begin(), fcns.begin(), fcns.end());          } diff --git a/host/lib/utils/load_modules.cpp b/host/lib/utils/load_modules.cpp index aba3adeed..3ef3c418c 100644 --- a/host/lib/utils/load_modules.cpp +++ b/host/lib/utils/load_modules.cpp @@ -19,7 +19,6 @@  #include <uhd/utils/static.hpp>  #include <uhd/exception.hpp>  #include <boost/format.hpp> -#include <boost/foreach.hpp>  #include <boost/filesystem.hpp>  #include <iostream>  #include <string> @@ -102,7 +101,7 @@ static void load_module_path(const fs::path &path){   * Load all the modules given in the module paths.   */  UHD_STATIC_BLOCK(load_modules){ -    BOOST_FOREACH(const fs::path &path, uhd::get_module_paths()){ +    for(const fs::path &path:  uhd::get_module_paths()){          load_module_path(path);      }  } diff --git a/host/lib/utils/log.cpp b/host/lib/utils/log.cpp index 4e58ce894..bfa012323 100644 --- a/host/lib/utils/log.cpp +++ b/host/lib/utils/log.cpp @@ -16,150 +16,418 @@  //  #include <uhd/utils/log.hpp> -#include <uhd/utils/msg.hpp>  #include <uhd/utils/static.hpp>  #include <uhd/utils/paths.hpp> -#include <boost/filesystem.hpp> -#include <boost/format.hpp> -#include <boost/thread/mutex.hpp> +#include <uhd/transport/bounded_buffer.hpp>  #include <boost/date_time/posix_time/posix_time.hpp> -#include <boost/thread/locks.hpp> -#include <boost/interprocess/sync/file_lock.hpp> +#include <boost/make_shared.hpp>  #include <fstream>  #include <cctype> +#include <atomic> +#include <mutex> +#include <memory> +#include <thread> -namespace fs = boost::filesystem;  namespace pt = boost::posix_time; -namespace ip = boost::interprocess; + +// Don't make these static const std::string -- we need their lifetime guaranteed! +#define PURPLE "\033[35;1m" // purple +#define BLUE "\033[34;1m" // blue +#define GREEN "\033[32;1m" // green +#define YELLOW "\033[33;1m" // yellow +#define RED "\033[31;0m" // red +#define BRED "\033[31;1m" // bright red +#define RESET_COLORS "\033[39;0m" // reset colors  /*********************************************************************** - * Global resources for the logger + * Helpers   **********************************************************************/ -class log_resource_type{ +static std::string verbosity_color(const uhd::log::severity_level &level){ +    switch(level){ +    case (uhd::log::trace): +        return PURPLE; +    case(uhd::log::debug): +        return BLUE; +    case(uhd::log::info): +        return GREEN; +    case(uhd::log::warning): +        return YELLOW; +    case(uhd::log::error): +        return RED; +    case(uhd::log::fatal): +        return BRED; +    default: +        return RESET_COLORS; +    } +} + +static std::string verbosity_name(const uhd::log::severity_level &level){ +    switch(level){ +    case (uhd::log::trace): +        return "TRACE"; +    case(uhd::log::debug): +        return "DEBUG"; +    case(uhd::log::info): +        return "INFO"; +    case(uhd::log::warning): +        return "WARNING"; +    case(uhd::log::error): +        return "ERROR"; +    case(uhd::log::fatal): +        return "FATAL"; +    default: +        return "-"; +    } +    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); +} + +/*********************************************************************** + * Logger backends + **********************************************************************/ +void console_log( +    const uhd::log::logging_info &log_info +) { + +    std::clog +#ifdef UHD_LOG_CONSOLE_COLOR +        << verbosity_color(log_info.verbosity) +#endif +#ifdef UHD_LOG_CONSOLE_TIME +        << "[" << pt::to_simple_string(log_info.time) << "] " +#endif +#ifdef UHD_LOG_CONSOLE_THREAD +        << "[0x" << log_info.thread_id << "] " +#endif +#ifdef UHD_LOG_CONSOLE_SRC +        << "[" << path_to_filename(log_info.file) << ":" << log_info.line << "] " +#endif +        << "[" << verbosity_name(log_info.verbosity) << "] " +        << "[" << log_info.component << "] " +#ifdef UHD_LOG_CONSOLE_COLOR +        << RESET_COLORS +#endif +        << log_info.message +        << std::endl +    ; +} + +/*! Helper class to implement file logging + * + * The class holds references to the file stream object, and handles closing + * and cleanup. + */ +class file_logger_backend +{  public: -    uhd::_log::verbosity_t level; +    file_logger_backend(const std::string &file_path) +    { +        _file_stream.exceptions(std::ofstream::failbit | std::ofstream::badbit); +        if (!file_path.empty()){ +            try { +                _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) +    { +        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; +            ; +        } +    } -    log_resource_type(void){ -        //file lock pointer must be null -        _file_lock = NULL; +    ~file_logger_backend() +    { +        if (_file_stream.is_open()){ +            _file_stream.close(); +        } +    } -        //set the default log level -        level = uhd::_log::never; +private: +    std::ofstream _file_stream; +}; -        //allow override from macro definition -        #ifdef UHD_LOG_LEVEL -        _set_log_level(BOOST_STRINGIZE(UHD_LOG_LEVEL)); -        #endif +/*********************************************************************** + * Global resources for the logger + **********************************************************************/ + +#define UHD_CONSOLE_LOGGER_KEY "console" +#define UHD_FILE_LOGGER_KEY "file" + +class log_resource { +public: +    uhd::log::severity_level global_level; +    std::map<std::string, uhd::log::severity_level> logger_level; -        //allow override from environment variable +    log_resource(void): +        global_level(uhd::log::off), +        _exit(false), +#ifndef UHD_LOG_FASTPATH_DISABLE +        _fastpath_queue(10), +#endif +        _log_queue(10) +    { +        //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); +#endif +       //allow override from environment variables          const char * log_level_env = std::getenv("UHD_LOG_LEVEL"); -        if (log_level_env != NULL) _set_log_level(log_level_env); +        if (log_level_env != NULL && log_level_env[0] != '\0') { +            this->global_level = +                _get_log_level(log_level_env, this->global_level); +        } + + +        /***** Console logging ***********************************************/ +#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"); +        if (log_console_level_env != NULL && log_console_level_env[0] != '\0') { +            console_level = +                _get_log_level(log_console_level_env, console_level); +        } +        logger_level[UHD_CONSOLE_LOGGER_KEY] = console_level; +        _loggers[UHD_CONSOLE_LOGGER_KEY] = &console_log; +#endif + +        /***** File logging **************************************************/ +        uhd::log::severity_level file_level = uhd::log::trace; +        std::string log_file_target; +#if defined(UHD_LOG_FILE_LEVEL) && defined(UHD_LOG_FILE_PATH) +        file_level = _get_log_level(BOOST_STRINGIZE(UHD_LOG_FILE_LEVEL), file_level); +        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'){ +            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()){ +            logger_level[UHD_FILE_LOGGER_KEY] = file_level; +            auto F = boost::make_shared<file_logger_backend>(log_file_target); +            _loggers[UHD_FILE_LOGGER_KEY] = [F](const uhd::log::logging_info& log_info){F->log(log_info);}; +        } + +        // Launch log message consumer +        _pop_task = std::make_shared<std::thread>(std::thread([this](){this->pop_task();})); +        _pop_fastpath_task = std::make_shared<std::thread>(std::thread([this](){this->pop_fastpath_task();}));      } -    ~log_resource_type(void){ -        boost::lock_guard<boost::mutex> lock(_mutex); -        _file_stream.close(); -        if (_file_lock != NULL) delete _file_lock; +    ~log_resource(void){ +        _exit = true; +        _pop_task->join(); +        { +            std::lock_guard<std::mutex> l(_logmap_mutex); +            _loggers.clear(); +        } +        _pop_task.reset(); +#ifndef UHD_LOG_FASTPATH_DISABLE +        _pop_fastpath_task->join(); +        _pop_fastpath_task.reset(); +#endif      } -    void log_to_file(const std::string &log_msg){ -        boost::lock_guard<boost::mutex> lock(_mutex); -        if (_file_lock == NULL){ -            const std::string log_path = (fs::path(uhd::get_tmp_path()) / "uhd.log").string(); -            _file_stream.open(log_path.c_str(), std::fstream::out | std::fstream::app); -            _file_lock = new ip::file_lock(log_path.c_str()); +    void push(const uhd::log::logging_info& log_info) +    { +        static const double PUSH_TIMEOUT = 0.25; // seconds +        _log_queue.push_with_timed_wait(log_info, PUSH_TIMEOUT); +    } + +    void push_fastpath(const std::string &message) +    { +        // Never wait. If the buffer is full, we just don't see the message. +        // Too bad. +#ifndef UHD_LOG_FASTPATH_DISABLE +        _fastpath_queue.push_with_haste(message); +#endif +    } + +    void pop_task() +    { +        uhd::log::logging_info log_info; +        log_info.message = ""; + +        while (!_exit) { +            if (_log_queue.pop_with_timed_wait(log_info, 1)){ +                std::lock_guard<std::mutex> l(_logmap_mutex); +                for (const auto &logger : _loggers) { +                    auto level = logger_level.find(logger.first); +                    if(level != logger_level.end() && log_info.verbosity < level->second){ +                        continue; +                    } +                    logger.second(log_info); +                } +            } +        } + +        // Exit procedure: Clear the queue +        while (_log_queue.pop_with_haste(log_info)) { +            std::lock_guard<std::mutex> l(_logmap_mutex); +            for (const auto &logger : _loggers) { +                auto level = logger_level.find(logger.first); +                if (level != logger_level.end() && log_info.verbosity < level->second){ +                    continue; +                } +                logger.second(log_info); +            }          } -        _file_lock->lock(); -        _file_stream << log_msg << std::flush; -        _file_lock->unlock(); +    } + +    void pop_fastpath_task() +    { +#ifndef UHD_LOG_FASTPATH_DISABLE +        while (!_exit) { +            std::string msg; +            if (_fastpath_queue.pop_with_timed_wait(msg, 1)){ +                std::cerr << msg << std::flush; +            } +        } + +        // Exit procedure: Clear the queue +        std::string msg; +        while (_fastpath_queue.pop_with_haste(msg)) { +            std::cerr << msg << std::flush; +        } +#endif +    } + + +    void add_logger(const std::string &key, uhd::log::log_fn_t logger_fn) +    { +        std::lock_guard<std::mutex> l(_logmap_mutex); +        _loggers[key] = logger_fn;      }  private: -    //! set the log level from a string that is either a digit or an enum name -    void _set_log_level(const std::string &log_level_str){ -        const uhd::_log::verbosity_t log_level_num = uhd::_log::verbosity_t(log_level_str[0]-'0'); -        if (std::isdigit(log_level_str[0]) and log_level_num >= uhd::_log::always and log_level_num <= uhd::_log::never){ -            this->level = log_level_num; -            return; +    std::shared_ptr<std::thread> _pop_task; +#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){ +        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{ +                UHD_LOGGER_ERROR("LOG") << "Failed to set log level to: " << log_level_str; +                return previous_level; +            }          } -        #define if_lls_equal(name) else if(log_level_str == #name) this->level = uhd::_log::name -        if_lls_equal(always); -        if_lls_equal(often); -        if_lls_equal(regularly); -        if_lls_equal(rarely); -        if_lls_equal(very_rarely); -        if_lls_equal(never); + +#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;      } -    //file stream and lock: -    std::ofstream _file_stream; -    ip::file_lock *_file_lock; -    boost::mutex _mutex; +    std::mutex _logmap_mutex; +    std::atomic<bool> _exit; +    std::map<std::string, uhd::log::log_fn_t> _loggers; +#ifndef UHD_LOG_FASTPATH_DISABLE +    uhd::transport::bounded_buffer<std::string> _fastpath_queue; +#endif +    uhd::transport::bounded_buffer<uhd::log::logging_info> _log_queue;  }; -UHD_SINGLETON_FCN(log_resource_type, log_rs); +UHD_SINGLETON_FCN(log_resource, log_rs);  /***********************************************************************   * The logger object implementation   **********************************************************************/ -//! get the relative file path from the host directory -static std::string get_rel_file_path(const fs::path &file){ -    fs::path abs_path = file.parent_path(); -    fs::path rel_path = file.leaf(); -    while (not abs_path.empty() and abs_path.leaf() != "host"){ -        rel_path = abs_path.leaf() / rel_path; -        abs_path = abs_path.parent_path(); -    } -    return rel_path.string(); -} - -  uhd::_log::log::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 thread_id +    ) : +    _log_it(verbosity >= log_rs().global_level)  { -    _log_it = (verbosity >= log_rs().level); -    if (_log_it) -    { -        const std::string time = pt::to_simple_string(pt::microsec_clock::local_time()); -        const std::string header1 = str(boost::format("-- %s - level %d") % time % int(verbosity)); -        const std::string header2 = str(boost::format("-- %s") % function).substr(0, 80); -        const std::string header3 = str(boost::format("-- %s:%u") % get_rel_file_path(file) % line); -        const std::string border = std::string(std::max(std::max(header1.size(), header2.size()), header3.size()), '-'); -        _ss << std::endl -            << border << std::endl -            << header1 << std::endl -            << header2 << std::endl -            << header3 << std::endl -            << border << std::endl -        ; -    } +    if (_log_it){ +        this->_log_info = uhd::log::logging_info( +            pt::microsec_clock::local_time(), +            verbosity, +            file, +            line, +            component, +            thread_id); +        }  }  uhd::_log::log::~log(void)  { -    if (not _log_it) -        return; - -    _ss << std::endl; -    try{ -        log_rs().log_to_file(_ss.str()); -    } -    catch(const std::exception &e){ -        /*! -         * Critical behavior below. -         * The following steps must happen in order to avoid a lock-up condition. -         * This is because the message facility will call into the logging facility. -         * Therefore we must disable the logger (level = never) before messaging. -         */ -        log_rs().level = never; -        UHD_MSG(error) -            << "Logging failed: " << e.what() << std::endl -            << "Logging has been disabled for this process" << std::endl -        ; +    if (_log_it) { +        this->_log_info.message = _ss.str(); +        log_rs().push(this->_log_info);      }  } + +void uhd::_log::log_fastpath(const std::string &msg) +{ +#ifndef UHD_LOG_FASTPATH_DISABLE +    log_rs().push_fastpath(msg); +#endif +} + +/*********************************************************************** + * Public API calls + **********************************************************************/ +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){ +    log_rs().global_level = level; +} + +void +uhd::log::set_logger_level(const std::string &key, uhd::log::severity_level level){ +    log_rs().logger_level[key] = 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){ +    set_logger_level(UHD_FILE_LOGGER_KEY, level); +} + diff --git a/host/lib/utils/msg.cpp b/host/lib/utils/msg.cpp deleted file mode 100644 index 95879a116..000000000 --- a/host/lib/utils/msg.cpp +++ /dev/null @@ -1,130 +0,0 @@ -// -// Copyright 2011 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 <http://www.gnu.org/licenses/>. -// - -#include <uhd/utils/msg.hpp> -#include <uhd/utils/log.hpp> -#include <uhd/utils/static.hpp> -#include <boost/thread/mutex.hpp> -#include <boost/foreach.hpp> -#include <boost/tokenizer.hpp> -#include <sstream> -#include <iostream> - -/*********************************************************************** - * Helper functions - **********************************************************************/ -#define tokenizer(inp, sep) \ -    boost::tokenizer<boost::char_separator<char> > \ -    (inp, boost::char_separator<char>(sep)) - -static void msg_to_cout(const std::string &msg){ -    std::stringstream ss; - -    static bool just_had_a_newline = true; -    BOOST_FOREACH(char ch, msg){ -        if (just_had_a_newline){ -            just_had_a_newline = false; -            ss << "-- "; -        } -        if (ch == '\n'){ -            just_had_a_newline = true; -        } -        ss << ch; -    } - -    std::cout << ss.str() << std::flush; -} - -static void msg_to_cerr(const std::string &title, const std::string &msg){ -    std::stringstream ss; - -    ss << std::endl << title << ":" << std::endl; -    BOOST_FOREACH(const std::string &line, tokenizer(msg, "\n")){ -        ss << "    " << line << std::endl; -    } - -    std::cerr << ss.str() << std::flush; -} - -/*********************************************************************** - * Global resources for the messenger - **********************************************************************/ -struct msg_resource_type{ -    boost::mutex mutex; -    uhd::msg::handler_t handler; -}; - -UHD_SINGLETON_FCN(msg_resource_type, msg_rs); - -/*********************************************************************** - * Setup the message handlers - **********************************************************************/ -void uhd::msg::register_handler(const handler_t &handler){ -    boost::mutex::scoped_lock lock(msg_rs().mutex); -    msg_rs().handler = handler; -} - -static void default_msg_handler(uhd::msg::type_t type, const std::string &msg){ -    static boost::mutex msg_mutex; -    boost::mutex::scoped_lock lock(msg_mutex); -    switch(type){ -    case uhd::msg::fastpath: -        std::cerr << msg << std::flush; -        break; - -    case uhd::msg::status: -        msg_to_cout(msg); -        UHD_LOG << "Status message" << std::endl << msg; -        break; - -    case uhd::msg::warning: -        msg_to_cerr("UHD Warning", msg); -        UHD_LOG << "Warning message" << std::endl << msg; -        break; - -    case uhd::msg::error: -        msg_to_cerr("UHD Error", msg); -        UHD_LOG << "Error message" << std::endl << msg; -        break; -    } -} - -UHD_STATIC_BLOCK(msg_register_default_handler){ -    uhd::msg::register_handler(&default_msg_handler); -} - -/*********************************************************************** - * The message object implementation - **********************************************************************/ -struct uhd::msg::_msg::impl{ -    std::ostringstream ss; -    type_t type; -}; - -uhd::msg::_msg::_msg(const type_t type){ -    _impl = UHD_PIMPL_MAKE(impl, ()); -    _impl->type = type; -} - -uhd::msg::_msg::~_msg(void){ -    boost::mutex::scoped_lock lock(msg_rs().mutex); -    msg_rs().handler(_impl->type, _impl->ss.str()); -} - -std::ostream & uhd::msg::_msg::operator()(void){ -    return _impl->ss; -} diff --git a/host/lib/utils/paths.cpp b/host/lib/utils/paths.cpp index 38839c8d4..5d53f95bd 100644 --- a/host/lib/utils/paths.cpp +++ b/host/lib/utils/paths.cpp @@ -22,7 +22,6 @@  #include <boost/algorithm/string.hpp>  #include <boost/bind.hpp>  #include <boost/filesystem.hpp> -#include <boost/foreach.hpp>  #include <boost/format.hpp>  #include <boost/regex.hpp>  #include <boost/tokenizer.hpp> @@ -98,7 +97,7 @@ static std::vector<std::string> get_env_paths(const std::string &var_name){      //convert to full filesystem path, filter blank paths      if (var_value.empty()) return paths; -    BOOST_FOREACH(const std::string &path_string, path_tokenizer(var_value)){ +    for(const std::string &path_string:  path_tokenizer(var_value)){          if (path_string.empty()) continue;          paths.push_back(fs::system_complete(path_string).string());      } @@ -190,7 +189,7 @@ std::vector<fs::path> uhd::get_module_paths(void){      std::vector<fs::path> paths;      std::vector<std::string> env_paths = get_env_paths("UHD_MODULE_PATH"); -    BOOST_FOREACH(std::string &str_path, env_paths) { +    for(std::string &str_path:  env_paths) {          paths.push_back(str_path);      } @@ -272,7 +271,7 @@ std::string uhd::get_images_dir(const std::string &search_paths) {      /* We will start by looking for a path indicated by the `UHD_IMAGES_DIR`       * environment variable. */      std::vector<std::string> env_paths = get_env_paths("UHD_IMAGES_DIR"); -    BOOST_FOREACH(possible_dir, env_paths) { +    for(auto possible_dir:  env_paths) {          if (fs::is_directory(fs::path(possible_dir))) {                  return possible_dir;          } @@ -293,7 +292,7 @@ std::string uhd::get_images_dir(const std::string &search_paths) {          std::vector<std::string> search_paths_vector;          boost::split(search_paths_vector, _search_paths, boost::is_any_of(",;")); -        BOOST_FOREACH(std::string& search_path, search_paths_vector) { +        for(std::string& search_path:  search_paths_vector) {              boost::algorithm::trim(search_path);              if (search_path.empty()) continue; diff --git a/host/lib/utils/tasks.cpp b/host/lib/utils/tasks.cpp index 661315ae8..5dac729c8 100644 --- a/host/lib/utils/tasks.cpp +++ b/host/lib/utils/tasks.cpp @@ -17,7 +17,7 @@  #include <uhd/utils/tasks.hpp>  #include <uhd/utils/msg_task.hpp> -#include <uhd/utils/msg.hpp> +#include <uhd/utils/log.hpp>  #include <boost/thread/thread.hpp>  #include <boost/thread/barrier.hpp>  #include <exception> @@ -67,10 +67,10 @@ private:      }      void do_error_msg(const std::string &msg){ -        UHD_MSG(error) -            << "An unexpected exception was caught in a task loop." << std::endl -            << "The task loop will now exit, things may not work." << std::endl -            << msg << std::endl +        UHD_LOGGER_ERROR("UHD") +            << "An unexpected exception was caught in a task loop."  +            << "The task loop will now exit, things may not work."  +            << msg           ;      } @@ -162,10 +162,10 @@ private:      }      void do_error_msg(const std::string &msg){ -        UHD_MSG(error) -            << "An unexpected exception was caught in a task loop." << std::endl -            << "The task loop will now exit, things may not work." << std::endl -            << msg << std::endl +        UHD_LOGGER_ERROR("UHD") +            << "An unexpected exception was caught in a task loop."  +            << "The task loop will now exit, things may not work."  +            << msg           ;      } diff --git a/host/lib/utils/thread_priority.cpp b/host/lib/utils/thread_priority.cpp index 98023c5aa..729edcf0a 100644 --- a/host/lib/utils/thread_priority.cpp +++ b/host/lib/utils/thread_priority.cpp @@ -16,7 +16,7 @@  //  #include <uhd/utils/thread_priority.hpp> -#include <uhd/utils/msg.hpp> +#include <uhd/utils/log.hpp>  #include <uhd/exception.hpp>  #include <boost/format.hpp>  #include <iostream> @@ -26,10 +26,10 @@ bool uhd::set_thread_priority_safe(float priority, bool realtime){          set_thread_priority(priority, realtime);          return true;      }catch(const std::exception &e){ -        UHD_MSG(warning) << boost::format( +        UHD_LOGGER_WARNING("UHD") << boost::format(              "Unable to set the thread priority. Performance may be negatively affected.\n"              "Please see the general application notes in the manual for instructions.\n" -            "%s\n" +            "%s"          ) % e.what();          return false;      } diff --git a/host/lib/utils/thread_priority_c.cpp b/host/lib/utils/thread_priority_c.cpp index fe019e51d..b2de9970d 100644 --- a/host/lib/utils/thread_priority_c.cpp +++ b/host/lib/utils/thread_priority_c.cpp @@ -18,7 +18,7 @@  #include <uhd/error.h>  #include <uhd/utils/thread_priority.h>  #include <uhd/utils/thread_priority.hpp> -#include <uhd/utils/msg.hpp> +#include <uhd/utils/log.hpp>  #include <uhd/exception.hpp>  #include <boost/format.hpp>  #include <iostream> | 
