From 7f01386f63850d9e13afb4033d1fae39f6a03764 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Wed, 4 May 2011 18:36:10 -0700 Subject: 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. --- host/lib/utils/msg.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 host/lib/utils/msg.cpp (limited to 'host/lib/utils/msg.cpp') 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 . +// + +#include +#include +#include +#include +#include +#include +#include +#include + +/*********************************************************************** + * Helper functions + **********************************************************************/ +#define tokenizer(inp, sep) \ + boost::tokenizer > \ + (inp, boost::char_separator(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; +} -- cgit v1.2.3