Statistics
| Branch: | Tag: | Revision:

root / host / lib / utils / msg.cpp @ 9734a743

History | View | Annotate | Download (3.65 KB)

1
//
2
// Copyright 2011 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
#include <uhd/utils/msg.hpp>
19
#include <uhd/utils/log.hpp>
20
#include <uhd/utils/static.hpp>
21
#include <boost/thread/mutex.hpp>
22
#include <boost/foreach.hpp>
23
#include <boost/tokenizer.hpp>
24
#include <sstream>
25
#include <iostream>
26

    
27
/***********************************************************************
28
 * Helper functions
29
 **********************************************************************/
30
#define tokenizer(inp, sep) \
31
    boost::tokenizer<boost::char_separator<char> > \
32
    (inp, boost::char_separator<char>(sep))
33

    
34
static void msg_to_cout(const std::string &msg){
35
    std::stringstream ss;
36

    
37
    static bool just_had_a_newline = true;
38
    BOOST_FOREACH(char ch, msg){
39
        if (just_had_a_newline){
40
            just_had_a_newline = false;
41
            ss << "-- ";
42
        }
43
        if (ch == '\n'){
44
            just_had_a_newline = true;
45
        }
46
        ss << ch;
47
    }
48

    
49
    std::cout << ss.str() << std::flush;
50
}
51

    
52
static void msg_to_cerr(const std::string &title, const std::string &msg){
53
    std::stringstream ss;
54

    
55
    ss << std::endl << title << ":" << std::endl;
56
    BOOST_FOREACH(const std::string &line, tokenizer(msg, "\n")){
57
        ss << "    " << line << std::endl;
58
    }
59

    
60
    std::cerr << ss.str() << std::flush;
61
}
62

    
63
/***********************************************************************
64
 * Global settings for the messenger
65
 **********************************************************************/
66
static boost::mutex msg_mutex;
67
static std::ostringstream msg_ss;
68
static uhd::msg::type_t msg_type;
69
static uhd::msg::handler_t msg_handler;
70

    
71
/***********************************************************************
72
 * Setup the message handlers
73
 **********************************************************************/
74
void uhd::msg::register_handler(const handler_t &handler){
75
    msg_mutex.lock();
76
    msg_handler = handler;
77
    msg_mutex.unlock();
78
}
79

    
80
static void default_msg_handler(uhd::msg::type_t type, const std::string &msg){
81
    switch(type){
82
    case uhd::msg::fastpath:
83
        std::cerr << msg << std::flush;
84
        break;
85

    
86
    case uhd::msg::status:
87
        msg_to_cout(msg);
88
        UHD_LOG << "Status message" << std::endl << msg;
89
        break;
90

    
91
    case uhd::msg::warning:
92
        msg_to_cerr("UHD Warning", msg);
93
        UHD_LOG << "Warning message" << std::endl << msg;
94
        break;
95

    
96
    case uhd::msg::error:
97
        msg_to_cerr("UHD Error", msg);
98
        UHD_LOG << "Error message" << std::endl << msg;
99
        break;
100
    }
101
}
102

    
103
UHD_STATIC_BLOCK(msg_register_default_handler){
104
    uhd::msg::register_handler(&default_msg_handler);
105
}
106

    
107
/***********************************************************************
108
 * The message object implementation
109
 **********************************************************************/
110
uhd::msg::_msg::_msg(const type_t type){
111
    msg_mutex.lock();
112
    msg_type = type;
113
}
114

    
115
uhd::msg::_msg::~_msg(void){
116
    msg_handler(msg_type, msg_ss.str());
117
    msg_ss.str(""); //clear for next call
118
    msg_mutex.unlock();
119
}
120

    
121
std::ostream & uhd::msg::_msg::operator()(void){
122
    return msg_ss;
123
}