1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/host/host_event_logger.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "net/base/ip_endpoint.h"
16 #include "remoting/host/host_status_monitor.h"
17 #include "remoting/host/host_status_observer.h"
18 #include "remoting/host/remoting_host_messages.h"
19 #include "remoting/protocol/transport.h"
25 class HostEventLoggerWin
: public HostEventLogger
, public HostStatusObserver
{
27 HostEventLoggerWin(base::WeakPtr
<HostStatusMonitor
> monitor
,
28 const std::string
& application_name
);
30 virtual ~HostEventLoggerWin();
32 // HostStatusObserver implementation. These methods will be called from the
34 virtual void OnClientAuthenticated(const std::string
& jid
) override
;
35 virtual void OnClientDisconnected(const std::string
& jid
) override
;
36 virtual void OnAccessDenied(const std::string
& jid
) override
;
37 virtual void OnClientRouteChange(
38 const std::string
& jid
,
39 const std::string
& channel_name
,
40 const protocol::TransportRoute
& route
) override
;
41 virtual void OnStart(const std::string
& xmpp_login
) override
;
42 virtual void OnShutdown() override
;
45 void LogString(WORD type
, DWORD event_id
, const std::string
& string
);
46 void Log(WORD type
, DWORD event_id
, const std::vector
<std::string
>& strings
);
48 base::WeakPtr
<HostStatusMonitor
> monitor_
;
50 // The handle of the application event log.
53 DISALLOW_COPY_AND_ASSIGN(HostEventLoggerWin
);
58 HostEventLoggerWin::HostEventLoggerWin(base::WeakPtr
<HostStatusMonitor
> monitor
,
59 const std::string
& application_name
)
62 event_log_
= RegisterEventSourceW(
63 nullptr, base::UTF8ToUTF16(application_name
).c_str());
64 if (event_log_
!= nullptr) {
65 monitor_
->AddStatusObserver(this);
67 PLOG(ERROR
) << "Failed to register the event source: " << application_name
;
71 HostEventLoggerWin::~HostEventLoggerWin() {
72 if (event_log_
!= nullptr) {
74 monitor_
->RemoveStatusObserver(this);
75 DeregisterEventSource(event_log_
);
79 void HostEventLoggerWin::OnClientAuthenticated(const std::string
& jid
) {
80 LogString(EVENTLOG_INFORMATION_TYPE
, MSG_HOST_CLIENT_CONNECTED
, jid
);
83 void HostEventLoggerWin::OnClientDisconnected(const std::string
& jid
) {
84 LogString(EVENTLOG_INFORMATION_TYPE
, MSG_HOST_CLIENT_DISCONNECTED
, jid
);
87 void HostEventLoggerWin::OnAccessDenied(const std::string
& jid
) {
88 LogString(EVENTLOG_ERROR_TYPE
, MSG_HOST_CLIENT_ACCESS_DENIED
, jid
);
91 void HostEventLoggerWin::OnClientRouteChange(
92 const std::string
& jid
,
93 const std::string
& channel_name
,
94 const protocol::TransportRoute
& route
) {
95 std::vector
<std::string
> strings(5);
97 strings
[1] = route
.remote_address
.ToString();
98 strings
[2] = route
.local_address
.ToString();
99 strings
[3] = channel_name
;
100 strings
[4] = protocol::TransportRoute::GetTypeString(route
.type
);
101 Log(EVENTLOG_INFORMATION_TYPE
, MSG_HOST_CLIENT_ROUTING_CHANGED
, strings
);
104 void HostEventLoggerWin::OnShutdown() {
105 // TODO(rmsousa): Fix host shutdown to actually call this, and add a log line.
108 void HostEventLoggerWin::OnStart(const std::string
& xmpp_login
) {
109 LogString(EVENTLOG_INFORMATION_TYPE
, MSG_HOST_STARTED
, xmpp_login
);
112 void HostEventLoggerWin::Log(WORD type
,
114 const std::vector
<std::string
>& strings
) {
115 if (event_log_
== nullptr)
118 // ReportEventW() takes an array of raw string pointers. They should stay
119 // valid for the duration of the call.
120 std::vector
<const WCHAR
*> raw_strings(strings
.size());
121 std::vector
<base::string16
> utf16_strings(strings
.size());
122 for (size_t i
= 0; i
< strings
.size(); ++i
) {
123 utf16_strings
[i
] = base::UTF8ToUTF16(strings
[i
]);
124 raw_strings
[i
] = utf16_strings
[i
].c_str();
127 if (!ReportEventW(event_log_
,
132 static_cast<WORD
>(raw_strings
.size()),
136 PLOG(ERROR
) << "Failed to write an event to the event log";
140 void HostEventLoggerWin::LogString(WORD type
,
142 const std::string
& string
) {
143 std::vector
<std::string
> strings
;
144 strings
.push_back(string
);
145 Log(type
, event_id
, strings
);
149 scoped_ptr
<HostEventLogger
> HostEventLogger::Create(
150 base::WeakPtr
<HostStatusMonitor
> monitor
,
151 const std::string
& application_name
) {
152 return make_scoped_ptr(new HostEventLoggerWin(monitor
, application_name
));
155 } // namespace remoting