OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "remoting/host/host_event_logger.h" | |
6 | |
7 #include <windows.h> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/string16.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "net/base/ip_endpoint.h" | |
16 #include "remoting/host/chromoting_host.h" | |
17 #include "remoting/host/host_status_observer.h" | |
18 | |
19 #include "remoting_host.h" | |
20 | |
21 namespace remoting { | |
22 | |
23 namespace { | |
24 | |
25 class HostEventLoggerWin : public HostEventLogger, public HostStatusObserver { | |
26 public: | |
27 HostEventLoggerWin(ChromotingHost* host, | |
28 const std::string& application_name); | |
29 | |
30 virtual ~HostEventLoggerWin(); | |
31 | |
32 // HostStatusObserver implementation. These methods will be called from the | |
33 // network thread. | |
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 net::IPEndPoint& remote_end_point, | |
41 const net::IPEndPoint& local_end_point) OVERRIDE; | |
42 virtual void OnShutdown() OVERRIDE; | |
43 | |
44 private: | |
45 void Log(DWORD event_id, const std::string& string); | |
46 void Log(DWORD event_id, const std::vector<string16>& strings); | |
Lambros
2012/03/01 21:15:41
Style guide generally prohibits overloading. Bett
alexeypa (please no reviews)
2012/03/01 22:32:41
Done.
| |
47 | |
48 scoped_refptr<ChromotingHost> host_; | |
49 | |
50 // The handle of the application event log. | |
51 HANDLE event_log_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(HostEventLoggerWin); | |
54 }; | |
55 | |
56 } //namespace | |
57 | |
58 HostEventLoggerWin::HostEventLoggerWin(ChromotingHost* host, | |
59 const std::string& application_name) | |
60 : host_(host), | |
61 event_log_(NULL) { | |
62 event_log_ = RegisterEventSourceW(NULL, | |
63 ASCIIToUTF16(application_name).c_str()); | |
64 if (event_log_ != NULL) { | |
65 host_->AddStatusObserver(this); | |
66 } else { | |
67 LOG_GETLASTERROR(ERROR) << "Failed to register the event source: " | |
68 << application_name; | |
69 } | |
70 } | |
71 | |
72 HostEventLoggerWin::~HostEventLoggerWin() { | |
73 if (event_log_ != NULL) { | |
74 host_->RemoveStatusObserver(this); | |
75 DeregisterEventSource(event_log_); | |
76 } | |
77 } | |
78 | |
79 void HostEventLoggerWin::OnClientAuthenticated(const std::string& jid) { | |
80 Log(MSG_HOST_CLIENT_CONNECTED, jid); | |
81 } | |
82 | |
83 void HostEventLoggerWin::OnClientDisconnected(const std::string& jid) { | |
84 Log(MSG_HOST_CLIENT_DISCONNECTED, jid); | |
85 } | |
86 | |
87 void HostEventLoggerWin::OnAccessDenied(const std::string& jid) { | |
88 Log(MSG_HOST_CLIENT_ACCESS_DENIED, jid); | |
89 } | |
90 | |
91 void HostEventLoggerWin::OnClientRouteChange( | |
92 const std::string& jid, | |
93 const std::string& channel_name, | |
94 const net::IPEndPoint& remote_end_point, | |
95 const net::IPEndPoint& local_end_point) { | |
96 std::vector<string16> strings(4); | |
97 strings[0] = ASCIIToUTF16(jid); | |
98 strings[1] = ASCIIToUTF16(remote_end_point.ToString()); | |
99 strings[2] = ASCIIToUTF16(local_end_point.ToString()); | |
100 strings[3] = ASCIIToUTF16(channel_name); | |
101 Log(MSG_HOST_CLIENT_ROUTING_CHANGED, strings); | |
102 } | |
103 | |
104 void HostEventLoggerWin::OnShutdown() { | |
105 } | |
106 | |
107 void HostEventLoggerWin::Log(DWORD event_id, | |
108 const std::vector<string16>& strings) { | |
109 if (event_log_ == NULL) | |
110 return; | |
111 | |
112 std::vector<const WCHAR*> raw_strings(strings.size()); | |
113 for (size_t i = 0; i < strings.size(); ++i) { | |
114 raw_strings[i] = strings[i].c_str(); | |
Wez
2012/03/01 18:03:06
nit: Add comment to clarify that each string16 ret
alexeypa (please no reviews)
2012/03/01 20:33:58
Done.
| |
115 } | |
116 | |
117 if (!ReportEventW(event_log_, | |
118 EVENTLOG_INFORMATION_TYPE, | |
Wez
2012/03/01 18:03:06
nit: This should arguably be EVENTLOG_ERROR_TYPE f
alexeypa (please no reviews)
2012/03/01 20:33:58
Done.
| |
119 HOST_CATEGORY, | |
120 event_id, | |
121 NULL, | |
122 static_cast<WORD>(raw_strings.size()), | |
123 0, | |
124 &raw_strings[0], | |
125 NULL)) { | |
126 LOG_GETLASTERROR(ERROR) << "Failed to write an event to the event log"; | |
127 } | |
128 } | |
129 | |
130 void HostEventLoggerWin::Log(DWORD event_id, const std::string& string) { | |
131 std::vector<string16> strings; | |
132 strings.push_back(ASCIIToUTF16(string)); | |
133 Log(event_id, strings); | |
134 } | |
135 | |
136 // static | |
137 scoped_ptr<HostEventLogger> HostEventLogger::Create( | |
138 ChromotingHost* host, const std::string& application_name) { | |
139 return scoped_ptr<HostEventLogger>( | |
140 new HostEventLoggerWin(host, application_name)); | |
141 } | |
142 | |
143 } // namespace remoting | |
OLD | NEW |