diff options
author | Josh Blum <josh@joshknows.com> | 2010-10-27 08:07:45 -0700 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2010-10-27 08:07:45 -0700 |
commit | 4fb937339bbe95091b5a22276d9618dc8e178f68 (patch) | |
tree | 61a604de6ab352bfe3d90fdc77e0d92ea8501bfc /host/lib/utils/warning.cpp | |
parent | db0e3e574e9058ad51cacea91ccc42f0baed95fa (diff) | |
parent | a7840ebb3f7d7f807d05059e2809eb45cff04899 (diff) | |
download | uhd-4fb937339bbe95091b5a22276d9618dc8e178f68.tar.gz uhd-4fb937339bbe95091b5a22276d9618dc8e178f68.tar.bz2 uhd-4fb937339bbe95091b5a22276d9618dc8e178f68.zip |
Merge branch 'next' into usrp_e
Diffstat (limited to 'host/lib/utils/warning.cpp')
-rw-r--r-- | host/lib/utils/warning.cpp | 59 |
1 files changed, 55 insertions, 4 deletions
diff --git a/host/lib/utils/warning.cpp b/host/lib/utils/warning.cpp index 8a7d35a23..05be7ae4d 100644 --- a/host/lib/utils/warning.cpp +++ b/host/lib/utils/warning.cpp @@ -17,16 +17,67 @@ #include <uhd/utils/warning.hpp> #include <uhd/utils/algorithm.hpp> +#include <uhd/utils/static.hpp> +#include <uhd/types/dict.hpp> #include <boost/foreach.hpp> +#include <sstream> +#include <stdexcept> #include <iostream> #include <vector> using namespace uhd; -void uhd::print_warning(const std::string &msg){ - //print the warning message - std::cerr << std::endl << "Warning:" << std::endl; +/*********************************************************************** + * Registry implementation + **********************************************************************/ +//create the registry for the handlers +typedef uhd::dict<std::string, warning::handler_t> registry_t; +UHD_SINGLETON_FCN(registry_t, get_registry) + +//the default warning handler +static void stderr_warning(const std::string &msg){ + std::cerr << msg; +} + +//register a default handler +UHD_STATIC_BLOCK(warning_register_default){ + warning::register_handler("default", &stderr_warning); +} + +/*********************************************************************** + * Post + format + **********************************************************************/ +void warning::post(const std::string &msg){ + std::stringstream ss; + + //format the warning message + ss << std::endl << "Warning:" << std::endl; BOOST_FOREACH(const std::string &line, std::split_string(msg, "\n")){ - std::cerr << " " << line << std::endl; + ss << " " << line << std::endl; + } + + //post the formatted message + BOOST_FOREACH(const std::string &name, get_registry().keys()){ + get_registry()[name](ss.str()); } } + +/*********************************************************************** + * Registry accessor functions + **********************************************************************/ +void warning::register_handler( + const std::string &name, const handler_t &handler +){ + get_registry()[name] = handler; +} + +warning::handler_t warning::unregister_handler(const std::string &name){ + if (not get_registry().has_key(name)) throw std::runtime_error( + "The warning registry does not have a handler registered to " + name + ); + return get_registry().pop(name); +} + +const std::vector<std::string> warning::registry_names(void){ + return get_registry().keys(); +} |