summaryrefslogtreecommitdiffstats
path: root/host/lib/utils
diff options
context:
space:
mode:
authorJosh Blum <josh@joshknows.com>2011-05-04 18:36:10 -0700
committerJosh Blum <josh@joshknows.com>2011-05-04 18:36:10 -0700
commit7f01386f63850d9e13afb4033d1fae39f6a03764 (patch)
treefb68a49ac4d5a41c0af5e4135786c5f169211673 /host/lib/utils
parente71d2833fbc2d9b87a8367b6ddc4388c3f182d93 (diff)
downloaduhd-7f01386f63850d9e13afb4033d1fae39f6a03764.tar.gz
uhd-7f01386f63850d9e13afb4033d1fae39f6a03764.tar.bz2
uhd-7f01386f63850d9e13afb4033d1fae39f6a03764.zip
uhd: replaced warning post with calls to UHD_MSG(warning)
The message api can support warnings, error, and status messages. The default handler is to stdio, but the user can change this.
Diffstat (limited to 'host/lib/utils')
-rw-r--r--host/lib/utils/CMakeLists.txt1
-rw-r--r--host/lib/utils/log.cpp2
-rw-r--r--host/lib/utils/msg.cpp109
-rw-r--r--host/lib/utils/thread_priority.cpp6
-rw-r--r--host/lib/utils/warning.cpp8
5 files changed, 118 insertions, 8 deletions
diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt
index ae18bde9d..0e0d51c78 100644
--- a/host/lib/utils/CMakeLists.txt
+++ b/host/lib/utils/CMakeLists.txt
@@ -132,6 +132,7 @@ LIBUHD_APPEND_SOURCES(
${CMAKE_CURRENT_SOURCE_DIR}/images.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}/props.cpp
${CMAKE_CURRENT_SOURCE_DIR}/static.cpp
diff --git a/host/lib/utils/log.cpp b/host/lib/utils/log.cpp
index 0a2861cbd..d99ce01c4 100644
--- a/host/lib/utils/log.cpp
+++ b/host/lib/utils/log.cpp
@@ -167,7 +167,7 @@ private:
UHD_SINGLETON_FCN(uhd_logger_stream_resource_class, uhd_logger_stream_resource);
/***********************************************************************
- * The logger function implementation
+ * The logger object implementation
**********************************************************************/
//! get the relative file path from the host directory
static std::string get_rel_file_path(const fs::path &file){
diff --git a/host/lib/utils/msg.cpp b/host/lib/utils/msg.cpp
new file mode 100644
index 000000000..de6d4c8fa
--- /dev/null
+++ b/host/lib/utils/msg.cpp
@@ -0,0 +1,109 @@
+//
+// 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;
+
+ BOOST_FOREACH(const std::string &line, tokenizer(msg, "\n")){
+ ss << "-- " << line << std::endl;
+ }
+
+ std::cout << ss.str();
+}
+
+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();
+}
+
+/***********************************************************************
+ * Global settings for the messenger
+ **********************************************************************/
+static boost::mutex msg_mutex;
+static std::ostringstream msg_ss;
+static uhd::msg::type_t msg_type;
+static uhd::msg::handler_t msg_handler;
+
+/***********************************************************************
+ * Setup the message handlers
+ **********************************************************************/
+void uhd::msg::register_handler(const handler_t &handler){
+ msg_mutex.lock();
+ msg_handler = handler;
+ msg_mutex.unlock();
+}
+
+static void default_msg_handler(uhd::msg::type_t type, const std::string &msg){
+ switch(type){
+ case uhd::msg::status:
+ msg_to_cout(msg);
+ break;
+
+ case uhd::msg::warning:
+ msg_to_cerr("UHD Warning", msg);
+ break;
+
+ case uhd::msg::error:
+ msg_to_cerr("UHD Error", msg);
+ break;
+ }
+}
+
+UHD_STATIC_BLOCK(msg_register_default_handler){
+ uhd::msg::register_handler(&default_msg_handler);
+}
+
+/***********************************************************************
+ * The message object implementation
+ **********************************************************************/
+uhd::msg::_msg::_msg(const type_t type){
+ msg_mutex.lock();
+ msg_type = type;
+}
+
+uhd::msg::_msg::~_msg(void){
+ msg_handler(msg_type, msg_ss.str());
+ UHD_LOG << "Message " << char(msg_type) << std::endl << msg_ss.str();
+ msg_ss.str(""); //clear for next call
+ msg_mutex.unlock();
+}
+
+std::ostream & uhd::msg::_msg::get(void){
+ return msg_ss;
+}
diff --git a/host/lib/utils/thread_priority.cpp b/host/lib/utils/thread_priority.cpp
index a63bdf5ce..6d6ca5630 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/warning.hpp>
+#include <uhd/utils/msg.hpp>
#include <uhd/exception.hpp>
#include <boost/format.hpp>
#include <iostream>
@@ -26,11 +26,11 @@ bool uhd::set_thread_priority_safe(float priority, bool realtime){
set_thread_priority(priority, realtime);
return true;
}catch(const std::exception &e){
- uhd::warning::post(str(boost::format(
+ UHD_MSG(warning) << 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"
- ) % e.what()));
+ ) % e.what();
return false;
}
}
diff --git a/host/lib/utils/warning.cpp b/host/lib/utils/warning.cpp
index 6a94a0a2c..87b6b24f5 100644
--- a/host/lib/utils/warning.cpp
+++ b/host/lib/utils/warning.cpp
@@ -39,13 +39,13 @@ 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;
-}
+//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);
+ //warning::register_handler("default", &stderr_warning);
}
/***********************************************************************