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/protocol/transport.h"
20 #include "remoting_host_messages.h"
26 class HostEventLoggerWin
: public HostEventLogger
, public HostStatusObserver
{
28 HostEventLoggerWin(base::WeakPtr
<HostStatusMonitor
> monitor
,
29 const std::string
& application_name
);
31 virtual ~HostEventLoggerWin();
33 // HostStatusObserver implementation. These methods will be called from the
35 virtual void OnClientAuthenticated(const std::string
& jid
) override
;
36 virtual void OnClientDisconnected(const std::string
& jid
) override
;
37 virtual void OnAccessDenied(const std::string
& jid
) override
;
38 virtual void OnClientRouteChange(
39 const std::string
& jid
,
40 const std::string
& channel_name
,
41 const protocol::TransportRoute
& route
) override
;
42 virtual void OnStart(const std::string
& xmpp_login
) override
;
43 virtual void OnShutdown() override
;
46 void LogString(WORD type
, DWORD event_id
, const std::string
& string
);
47 void Log(WORD type
, DWORD event_id
, const std::vector
<std::string
>& strings
);
49 base::WeakPtr
<HostStatusMonitor
> monitor_
;
51 // The handle of the application event log.
54 DISALLOW_COPY_AND_ASSIGN(HostEventLoggerWin
);
59 HostEventLoggerWin::HostEventLoggerWin(base::WeakPtr
<HostStatusMonitor
> monitor
,
60 const std::string
& application_name
)
63 event_log_
= RegisterEventSourceW(
64 nullptr, base::UTF8ToUTF16(application_name
).c_str());
65 if (event_log_
!= nullptr) {
66 monitor_
->AddStatusObserver(this);
68 PLOG(ERROR
) << "Failed to register the event source: " << application_name
;
72 HostEventLoggerWin::~HostEventLoggerWin() {
73 if (event_log_
!= nullptr) {
75 monitor_
->RemoveStatusObserver(this);
76 DeregisterEventSource(event_log_
);
80 void HostEventLoggerWin::OnClientAuthenticated(const std::string
& jid
) {
81 LogString(EVENTLOG_INFORMATION_TYPE
, MSG_HOST_CLIENT_CONNECTED
, jid
);
84 void HostEventLoggerWin::OnClientDisconnected(const std::string
& jid
) {
85 LogString(EVENTLOG_INFORMATION_TYPE
, MSG_HOST_CLIENT_DISCONNECTED
, jid
);
88 void HostEventLoggerWin::OnAccessDenied(const std::string
& jid
) {
89 LogString(EVENTLOG_ERROR_TYPE
, MSG_HOST_CLIENT_ACCESS_DENIED
, jid
);
92 void HostEventLoggerWin::OnClientRouteChange(
93 const std::string
& jid
,
94 const std::string
& channel_name
,
95 const protocol::TransportRoute
& route
) {
96 std::vector
<std::string
> strings(5);
98 strings
[1] = route
.remote_address
.ToString();
99 strings
[2] = route
.local_address
.ToString();
100 strings
[3] = channel_name
;
101 strings
[4] = protocol::TransportRoute::GetTypeString(route
.type
);
102 Log(EVENTLOG_INFORMATION_TYPE
, MSG_HOST_CLIENT_ROUTING_CHANGED
, strings
);
105 void HostEventLoggerWin::OnShutdown() {
106 // TODO(rmsousa): Fix host shutdown to actually call this, and add a log line.
109 void HostEventLoggerWin::OnStart(const std::string
& xmpp_login
) {
110 LogString(EVENTLOG_INFORMATION_TYPE
, MSG_HOST_STARTED
, xmpp_login
);
113 void HostEventLoggerWin::Log(WORD type
,
115 const std::vector
<std::string
>& strings
) {
116 if (event_log_
== nullptr)
119 // ReportEventW() takes an array of raw string pointers. They should stay
120 // valid for the duration of the call.
121 std::vector
<const WCHAR
*> raw_strings(strings
.size());
122 std::vector
<base::string16
> utf16_strings(strings
.size());
123 for (size_t i
= 0; i
< strings
.size(); ++i
) {
124 utf16_strings
[i
] = base::UTF8ToUTF16(strings
[i
]);
125 raw_strings
[i
] = utf16_strings
[i
].c_str();
128 if (!ReportEventW(event_log_
,
133 static_cast<WORD
>(raw_strings
.size()),
137 PLOG(ERROR
) << "Failed to write an event to the event log";
141 void HostEventLoggerWin::LogString(WORD type
,
143 const std::string
& string
) {
144 std::vector
<std::string
> strings
;
145 strings
.push_back(string
);
146 Log(type
, event_id
, strings
);
150 scoped_ptr
<HostEventLogger
> HostEventLogger::Create(
151 base::WeakPtr
<HostStatusMonitor
> monitor
,
152 const std::string
& application_name
) {
153 return make_scoped_ptr(new HostEventLoggerWin(monitor
, application_name
));
156 } // namespace remoting