OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/client/server_log_entry.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/macros.h" | |
9 #include "base/rand_util.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/stringize_macros.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "base/sys_info.h" | |
14 #include "remoting/base/constants.h" | |
15 #include "remoting/client/chromoting_stats.h" | |
16 #include "remoting/protocol/connection_to_host.h" | |
17 #include "remoting/protocol/errors.h" | |
18 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | |
19 | |
20 using base::StringPrintf; | |
21 using base::SysInfo; | |
22 using buzz::QName; | |
23 using buzz::XmlElement; | |
24 using remoting::protocol::ConnectionToHost; | |
25 | |
26 namespace remoting { | |
27 | |
28 namespace client { | |
29 | |
30 namespace { | |
31 const char kLogCommand[] = "log"; | |
32 | |
33 const char kLogEntry[] = "entry"; | |
34 | |
35 const char kKeyEventName[] = "event-name"; | |
36 const char kValueEventNameSessionState[] = "session-state"; | |
37 const char kValueEventNameStatistics[] = "connection-statistics"; | |
38 const char kValueEventNameSessionIdOld[] = "session-id-old"; | |
39 const char kValueEventNameSessionIdNew[] = "session-id-new"; | |
40 | |
41 const char kKeyRole[] = "role"; | |
42 const char kValueRoleClient[] = "client"; | |
43 | |
44 const char kKeyMode[] = "mode"; | |
45 const char kValueModeIt2Me[] = "it2me"; | |
46 const char kValueModeMe2Me[] = "me2me"; | |
47 | |
48 const char kKeySessionState[] = "session-state"; | |
49 const char kValueSessionStateConnected[] = "connected"; | |
50 const char kValueSessionStateClosed[] = "closed"; | |
51 | |
52 const char kKeyOsName[] = "os-name"; | |
53 const char kKeyOsVersion[] = "os-version"; | |
54 const char kKeyAppVersion[] = "app-version"; | |
55 | |
56 const char kKeyCpu[] = "cpu"; | |
57 | |
58 } // namespace | |
59 | |
60 ServerLogEntry::ServerLogEntry() { | |
61 } | |
62 | |
63 ServerLogEntry::~ServerLogEntry() { | |
64 } | |
65 | |
66 // static | |
67 scoped_ptr<buzz::XmlElement> ServerLogEntry::MakeStanza() { | |
68 return scoped_ptr<buzz::XmlElement>( | |
69 new XmlElement(QName(kChromotingXmlNamespace, kLogCommand))); | |
70 } | |
71 | |
72 // static | |
73 scoped_ptr<ServerLogEntry> ServerLogEntry::MakeForSessionStateChange( | |
74 protocol::ConnectionToHost::State state, | |
75 protocol::ErrorCode error) { | |
76 scoped_ptr<ServerLogEntry> entry(new ServerLogEntry()); | |
77 entry->Set(kKeyRole, kValueRoleClient); | |
78 entry->Set(kKeyEventName, kValueEventNameSessionState); | |
79 | |
80 entry->Set(kKeySessionState, GetValueSessionState(state)); | |
81 if (error != protocol::OK) { | |
82 entry->Set("connection-error", GetValueError(error)); | |
83 } | |
84 | |
85 return entry.Pass(); | |
86 } | |
87 | |
88 // static | |
89 scoped_ptr<ServerLogEntry> ServerLogEntry::MakeForStatistics( | |
90 ChromotingStats* statistics) { | |
91 scoped_ptr<ServerLogEntry> entry(new ServerLogEntry()); | |
92 entry->Set(kKeyRole, kValueRoleClient); | |
93 entry->Set(kKeyEventName, kValueEventNameStatistics); | |
94 | |
95 entry->Set("video-bandwidth", | |
96 StringPrintf("%.2f", statistics->video_bandwidth()->Rate())); | |
97 entry->Set("capture-latency", | |
98 StringPrintf("%.2f", statistics->video_capture_ms()->Average())); | |
99 entry->Set("encode-latency", | |
100 StringPrintf("%.2f", statistics->video_encode_ms()->Average())); | |
101 entry->Set("decode-latency", | |
102 StringPrintf("%.2f", statistics->video_decode_ms()->Average())); | |
103 entry->Set("render-latency", | |
104 StringPrintf("%.2f", statistics->video_frame_rate()->Rate())); | |
105 entry->Set("roundtrip-latency", | |
106 StringPrintf("%.2f", statistics->round_trip_ms()->Average())); | |
107 | |
108 return entry.Pass(); | |
109 } | |
110 | |
111 // static | |
112 scoped_ptr<ServerLogEntry> ServerLogEntry::MakeForSessionIdOld( | |
113 const std::string& session_id) { | |
114 scoped_ptr<ServerLogEntry> entry(new ServerLogEntry()); | |
115 entry->Set(kKeyRole, kValueRoleClient); | |
116 entry->Set(kKeyEventName, kValueEventNameSessionIdOld); | |
117 entry->AddSessionId(session_id); | |
118 return entry.Pass(); | |
119 } | |
120 | |
121 // static | |
122 scoped_ptr<ServerLogEntry> ServerLogEntry::MakeForSessionIdNew( | |
123 const std::string& session_id) { | |
124 scoped_ptr<ServerLogEntry> entry(new ServerLogEntry()); | |
125 entry->Set(kKeyRole, kValueRoleClient); | |
126 entry->Set(kKeyEventName, kValueEventNameSessionIdNew); | |
127 entry->AddSessionId(session_id); | |
128 return entry.Pass(); | |
129 } | |
130 | |
131 void ServerLogEntry::AddClientFields() { | |
132 Set(kKeyOsName, SysInfo::OperatingSystemName()); | |
133 Set(kKeyOsVersion, SysInfo::OperatingSystemVersion()); | |
134 Set(kKeyAppVersion, STRINGIZE(VERSION)); | |
135 Set(kKeyCpu, SysInfo::OperatingSystemArchitecture()); | |
136 }; | |
137 | |
138 void ServerLogEntry::AddModeField(ServerLogEntry::Mode mode) { | |
139 Set(kKeyMode, GetValueMode(mode)); | |
140 } | |
141 | |
142 void ServerLogEntry::AddSessionId(const std::string& session_id) { | |
143 Set("session-id", session_id); | |
144 } | |
145 | |
146 void ServerLogEntry::AddSessionDuration(base::TimeDelta duration) { | |
147 Set("session-duration", base::Int64ToString(duration.InSeconds())); | |
148 } | |
149 | |
150 // static | |
151 const char* ServerLogEntry::GetValueMode(ServerLogEntry::Mode mode) { | |
152 switch (mode) { | |
153 case IT2ME: | |
154 return kValueModeIt2Me; | |
155 case ME2ME: | |
156 return kValueModeMe2Me; | |
157 default: | |
158 NOTREACHED(); | |
159 return NULL; | |
160 } | |
161 } | |
162 | |
163 scoped_ptr<XmlElement> ServerLogEntry::ToStanza() const { | |
164 scoped_ptr<XmlElement> stanza(new XmlElement(QName( | |
165 kChromotingXmlNamespace, kLogEntry))); | |
166 ValuesMap::const_iterator iter; | |
167 for (iter = values_map_.begin(); iter != values_map_.end(); ++iter) { | |
168 stanza->AddAttr(QName(std::string(), iter->first), iter->second); | |
169 } | |
170 return stanza.Pass(); | |
171 } | |
172 | |
173 // static | |
174 const char* ServerLogEntry::GetValueSessionState( | |
175 ConnectionToHost::State state) { | |
176 switch (state) { | |
177 // Where possible, these are the same strings that the webapp sends for the | |
178 // corresponding state - see remoting/webapp/server_log_entry.js. | |
179 case ConnectionToHost::INITIALIZING: | |
180 return "initializing"; | |
181 case ConnectionToHost::CONNECTING: | |
182 return "connecting"; | |
183 case ConnectionToHost::AUTHENTICATED: | |
184 return "authenticated"; | |
185 case ConnectionToHost::CONNECTED: | |
186 return kValueSessionStateConnected; | |
187 case ConnectionToHost::FAILED: | |
188 return "connection-failed"; | |
189 case ConnectionToHost::CLOSED: | |
190 return kValueSessionStateClosed; | |
191 default: | |
192 NOTREACHED(); | |
193 return NULL; | |
194 } | |
195 } | |
196 | |
197 // static | |
198 const char* ServerLogEntry::GetValueError(protocol::ErrorCode error) { | |
199 switch (error) { | |
200 // Where possible, these are the same strings that the webapp sends for the | |
201 // corresponding error - see remoting/webapp/server_log_entry.js. | |
202 case protocol::OK: | |
203 return "none"; | |
204 case protocol::PEER_IS_OFFLINE: | |
205 return "host-is-offline"; | |
206 case protocol::SESSION_REJECTED: | |
207 return "session-rejected"; | |
208 case protocol::INCOMPATIBLE_PROTOCOL: | |
209 return "incompatible-protocol"; | |
210 case protocol::AUTHENTICATION_FAILED: | |
211 return "authentication-failed"; | |
212 case protocol::CHANNEL_CONNECTION_ERROR: | |
213 return "channel-connection-error"; | |
214 case protocol::SIGNALING_ERROR: | |
215 return "signaling-error"; | |
216 case protocol::SIGNALING_TIMEOUT: | |
217 return "signaling-timeout"; | |
218 case protocol::HOST_OVERLOAD: | |
219 return "host-overload"; | |
220 case protocol::UNKNOWN_ERROR: | |
221 return "unknown-error"; | |
222 default: | |
223 NOTREACHED(); | |
224 return NULL; | |
225 } | |
226 } | |
227 | |
228 void ServerLogEntry::AddEventName(const std::string& event_name) { | |
229 Set("event-name", event_name); | |
230 } | |
231 | |
232 void ServerLogEntry::Set(const std::string& key, const std::string& value) { | |
233 values_map_[key] = value; | |
234 } | |
235 | |
236 } // namespace client | |
237 | |
238 } // namespace remoting | |
OLD | NEW |