1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
//
// 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;
}
|