~ubuntu-branches/ubuntu/utopic/mir/utopic-proposed

« back to all changes in this revision

Viewing changes to src/server/report/logging/message_processor_report.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-03-10 19:28:46 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: package-import@ubuntu.com-20140310192846-rq9qm3ec26yrelo2
Tags: upstream-0.1.6+14.04.20140310
Import upstream version 0.1.6+14.04.20140310

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Alan Griffiths <alan@octopull.co.uk>
 
17
 */
 
18
 
 
19
#include "message_processor_report.h"
 
20
#include "mir/logging/logger.h"
 
21
 
 
22
#include <boost/exception/diagnostic_information.hpp>
 
23
#include <sstream>
 
24
 
 
25
namespace ml = mir::logging;
 
26
namespace mrl = mir::report::logging;
 
27
 
 
28
namespace
 
29
{
 
30
char const* const component = "frontend::MessageProcessor";
 
31
}
 
32
 
 
33
 
 
34
mrl::MessageProcessorReport::MessageProcessorReport(
 
35
    std::shared_ptr<ml::Logger> const& log,
 
36
    std::shared_ptr<time::Clock> const& clock) :
 
37
    log(log),
 
38
    clock(clock)
 
39
{
 
40
}
 
41
 
 
42
mrl::MessageProcessorReport::~MessageProcessorReport() noexcept(true)
 
43
{
 
44
    if (!mediators.empty())
 
45
    {
 
46
        std::ostringstream out;
 
47
 
 
48
        {
 
49
            out << "Calls outstanding on exit:\n";
 
50
 
 
51
            std::lock_guard<std::mutex> lock(mutex);
 
52
 
 
53
            for (auto const& med : mediators)
 
54
            {
 
55
                for (auto const & invocation: med.second.current_invocations)
 
56
                {
 
57
                    out << "mediator=" << med.first << ": "
 
58
                        << invocation.second.method << "()";
 
59
 
 
60
                    if (!invocation.second.exception.empty())
 
61
                        out << " ERROR=" << invocation.second.exception;
 
62
 
 
63
                    out << '\n';
 
64
                }
 
65
            }
 
66
        }
 
67
 
 
68
        log->log(ml::Logger::informational, out.str(), component);
 
69
    }
 
70
}
 
71
 
 
72
 
 
73
void mrl::MessageProcessorReport::received_invocation(void const* mediator, int id, std::string const& method)
 
74
{
 
75
    std::ostringstream out;
 
76
    out << "mediator=" << mediator << ", method=" << method << "()";
 
77
    log->log(ml::Logger::debug, out.str(), component);
 
78
 
 
79
    std::lock_guard<std::mutex> lock(mutex);
 
80
    auto& invocations = mediators[mediator].current_invocations;
 
81
    auto& invocation = invocations[id];
 
82
 
 
83
    invocation.start = clock->sample();
 
84
    invocation.method = method;
 
85
}
 
86
 
 
87
void mrl::MessageProcessorReport::completed_invocation(void const* mediator, int id, bool result)
 
88
{
 
89
    auto const end = clock->sample();
 
90
    std::ostringstream out;
 
91
 
 
92
    {
 
93
        std::lock_guard<std::mutex> lock(mutex);
 
94
 
 
95
        auto const pm = mediators.find(mediator);
 
96
        if (pm != mediators.end())
 
97
        {
 
98
            auto& invocations = pm->second.current_invocations;
 
99
 
 
100
            auto const pi = invocations.find(id);
 
101
            if (pi != invocations.end())
 
102
            {
 
103
                out << "mediator=" << mediator << ": "
 
104
                    << pi->second.method << "(), elapsed="
 
105
                    << std::chrono::duration_cast<std::chrono::microseconds>(end - pi->second.start).count()
 
106
                    << "µs";
 
107
 
 
108
                if (!pi->second.exception.empty())
 
109
                    out << " ERROR=" << pi->second.exception;
 
110
 
 
111
                if (!result)
 
112
                    out << " (disconnecting)";
 
113
            }
 
114
 
 
115
            invocations.erase(pi);
 
116
 
 
117
            if (invocations.empty())
 
118
                mediators.erase(mediator);
 
119
        }
 
120
    }
 
121
 
 
122
    log->log(ml::Logger::informational, out.str(), component);
 
123
}
 
124
 
 
125
void mrl::MessageProcessorReport::unknown_method(void const* mediator, int id, std::string const& method)
 
126
{
 
127
    std::ostringstream out;
 
128
    out << "mediator=" << mediator << ", id=" << id << ", UNKNOWN method=\"" << method << "\"";
 
129
    log->log(ml::Logger::warning, out.str(), component);
 
130
 
 
131
    std::lock_guard<std::mutex> lock(mutex);
 
132
    auto const pm = mediators.find(mediator);
 
133
    if (pm != mediators.end())
 
134
        mediators.erase(mediator);
 
135
}
 
136
 
 
137
void mrl::MessageProcessorReport::exception_handled(void const* mediator, int id, std::exception const& error)
 
138
{
 
139
    std::lock_guard<std::mutex> lock(mutex);
 
140
 
 
141
    auto const pm = mediators.find(mediator);
 
142
    if (pm != mediators.end())
 
143
    {
 
144
        auto& invocations = pm->second.current_invocations;
 
145
 
 
146
        auto const pi = invocations.find(id);
 
147
        if (pi != invocations.end())
 
148
        {
 
149
            pi->second.exception = boost::diagnostic_information(error);
 
150
        }
 
151
    }
 
152
}
 
153
 
 
154
void mrl::MessageProcessorReport::exception_handled(void const* mediator, std::exception const& error)
 
155
{
 
156
    std::ostringstream out;
 
157
    out << "mediator=" << mediator << ", ERROR: " << boost::diagnostic_information(error);
 
158
    log->log(ml::Logger::informational, out.str(), component);
 
159
 
 
160
    std::lock_guard<std::mutex> lock(mutex);
 
161
    auto const pm = mediators.find(mediator);
 
162
    if (pm != mediators.end())
 
163
        mediators.erase(mediator);
 
164
}
 
165
 
 
166
void mrl::MessageProcessorReport::sent_event(void const* mediator, MirSurfaceEvent const& event)
 
167
{
 
168
    std::ostringstream out;
 
169
    out << "mediator=" << mediator << ", sent event, surface id=" << event.id;
 
170
    log->log(ml::Logger::debug, out.str(), component);
 
171
}