OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/api/cast_channel/logger.h" | 5 #include "extensions/browser/api/cast_channel/logger.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "base/time/tick_clock.h" | 8 #include "base/time/tick_clock.h" |
9 #include "extensions/browser/api/cast_channel/cast_auth_util.h" | 9 #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
10 | 10 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 | 57 |
58 } // namespace | 58 } // namespace |
59 | 59 |
60 Logger::AggregatedSocketEventLog::AggregatedSocketEventLog() { | 60 Logger::AggregatedSocketEventLog::AggregatedSocketEventLog() { |
61 } | 61 } |
62 Logger::AggregatedSocketEventLog::~AggregatedSocketEventLog() { | 62 Logger::AggregatedSocketEventLog::~AggregatedSocketEventLog() { |
63 } | 63 } |
64 | 64 |
65 Logger::Logger(scoped_ptr<base::TickClock> clock, | 65 Logger::Logger(scoped_ptr<base::TickClock> clock, |
66 base::TimeTicks unix_epoch_time_ticks) | 66 base::TimeTicks unix_epoch_time_ticks) |
67 : clock_(clock.Pass()), | 67 : clock_(clock.Pass()), unix_epoch_time_ticks_(unix_epoch_time_ticks) { |
68 unix_epoch_time_ticks_(unix_epoch_time_ticks), | |
69 num_evicted_aggregated_socket_events_(0), | |
70 num_evicted_socket_events_(0) { | |
71 DCHECK(clock_); | 68 DCHECK(clock_); |
72 | 69 |
73 // Logger may not be necessarily be created on the IO thread, but logging | 70 // Logger may not be necessarily be created on the IO thread, but logging |
74 // happens exclusively there. | 71 // happens exclusively there. |
75 thread_checker_.DetachFromThread(); | 72 thread_checker_.DetachFromThread(); |
76 } | 73 } |
77 | 74 |
78 Logger::~Logger() { | 75 Logger::~Logger() { |
79 } | 76 } |
80 | 77 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 | 113 |
117 void Logger::LogSocketEventWithRv(int channel_id, | 114 void Logger::LogSocketEventWithRv(int channel_id, |
118 EventType event_type, | 115 EventType event_type, |
119 int rv) { | 116 int rv) { |
120 DCHECK(thread_checker_.CalledOnValidThread()); | 117 DCHECK(thread_checker_.CalledOnValidThread()); |
121 | 118 |
122 SocketEvent event = CreateEvent(event_type); | 119 SocketEvent event = CreateEvent(event_type); |
123 event.set_return_value(rv); | 120 event.set_return_value(rv); |
124 | 121 |
125 LogSocketEvent(channel_id, event); | 122 LogSocketEvent(channel_id, event); |
123 | |
124 if ((event_type == proto::SOCKET_READ || event_type == proto::SOCKET_WRITE) && | |
125 rv > 0) { | |
126 AggregatedSocketEvent& aggregated_socket_event = | |
127 aggregated_socket_events_.find(channel_id) | |
128 ->second->aggregated_socket_event; | |
mark a. foltz
2014/08/13 22:06:31
Is this lookup guaranteed to succeed?
imcheng
2014/08/14 00:24:34
Yes, because this entry was just added in LogSocke
| |
129 if (event_type == proto::SOCKET_READ) { | |
130 aggregated_socket_event.set_bytes_read( | |
131 aggregated_socket_event.bytes_read() + rv); | |
132 log_.set_total_bytes_read(log_.total_bytes_read() + rv); | |
133 } else { | |
134 aggregated_socket_event.set_bytes_written( | |
135 aggregated_socket_event.bytes_written() + rv); | |
136 log_.set_total_bytes_written(log_.total_bytes_written() + rv); | |
137 } | |
138 } | |
126 } | 139 } |
127 | 140 |
128 void Logger::LogSocketReadyState(int channel_id, proto::ReadyState new_state) { | 141 void Logger::LogSocketReadyState(int channel_id, proto::ReadyState new_state) { |
129 DCHECK(thread_checker_.CalledOnValidThread()); | 142 DCHECK(thread_checker_.CalledOnValidThread()); |
130 | 143 |
131 SocketEvent event = CreateEvent(proto::READY_STATE_CHANGED); | 144 SocketEvent event = CreateEvent(proto::READY_STATE_CHANGED); |
132 event.set_ready_state(new_state); | 145 event.set_ready_state(new_state); |
133 | 146 |
134 LogSocketEvent(channel_id, event); | 147 LogSocketEvent(channel_id, event); |
135 } | 148 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 } | 220 } |
208 | 221 |
209 void Logger::LogSocketEvent(int channel_id, const SocketEvent& socket_event) { | 222 void Logger::LogSocketEvent(int channel_id, const SocketEvent& socket_event) { |
210 AggregatedSocketEventLogMap::iterator it = | 223 AggregatedSocketEventLogMap::iterator it = |
211 aggregated_socket_events_.find(channel_id); | 224 aggregated_socket_events_.find(channel_id); |
212 if (it == aggregated_socket_events_.end()) { | 225 if (it == aggregated_socket_events_.end()) { |
213 if (aggregated_socket_events_.size() >= kMaxSocketsToLog) { | 226 if (aggregated_socket_events_.size() >= kMaxSocketsToLog) { |
214 AggregatedSocketEventLogMap::iterator erase_it = | 227 AggregatedSocketEventLogMap::iterator erase_it = |
215 aggregated_socket_events_.begin(); | 228 aggregated_socket_events_.begin(); |
216 | 229 |
217 num_evicted_aggregated_socket_events_++; | 230 log_.set_num_evicted_aggregated_socket_events( |
218 num_evicted_socket_events_ += erase_it->second->socket_events.size(); | 231 log_.num_evicted_aggregated_socket_events() + 1); |
232 log_.set_num_evicted_socket_events( | |
233 log_.num_evicted_socket_events() + | |
234 erase_it->second->socket_events.size()); | |
219 | 235 |
220 aggregated_socket_events_.erase(erase_it); | 236 aggregated_socket_events_.erase(erase_it); |
221 } | 237 } |
222 | 238 |
223 it = aggregated_socket_events_ | 239 it = aggregated_socket_events_ |
224 .insert(std::make_pair( | 240 .insert(std::make_pair( |
225 channel_id, make_linked_ptr(new AggregatedSocketEventLog))) | 241 channel_id, make_linked_ptr(new AggregatedSocketEventLog))) |
226 .first; | 242 .first; |
227 it->second->aggregated_socket_event.set_id(channel_id); | 243 it->second->aggregated_socket_event.set_id(channel_id); |
228 } | 244 } |
229 | 245 |
230 std::deque<proto::SocketEvent>& socket_events = it->second->socket_events; | 246 std::deque<proto::SocketEvent>& socket_events = it->second->socket_events; |
231 if (socket_events.size() >= kMaxEventsPerSocket) { | 247 if (socket_events.size() >= kMaxEventsPerSocket) { |
232 socket_events.pop_front(); | 248 socket_events.pop_front(); |
233 num_evicted_socket_events_++; | 249 log_.set_num_evicted_socket_events(log_.num_evicted_socket_events() + 1); |
234 } | 250 } |
235 | 251 |
236 socket_events.push_back(socket_event); | 252 socket_events.push_back(socket_event); |
237 } | 253 } |
238 | 254 |
239 bool Logger::LogToString(std::string* output) const { | 255 bool Logger::LogToString(std::string* output) const { |
240 output->clear(); | 256 output->clear(); |
241 | 257 |
242 Log log; | 258 Log log; |
243 log.set_num_evicted_aggregated_socket_events( | 259 log.CopyFrom(log_); |
244 num_evicted_aggregated_socket_events_); | |
245 log.set_num_evicted_socket_events(num_evicted_socket_events_); | |
246 | 260 |
247 for (AggregatedSocketEventLogMap::const_iterator it = | 261 for (AggregatedSocketEventLogMap::const_iterator it = |
248 aggregated_socket_events_.begin(); | 262 aggregated_socket_events_.begin(); |
249 it != aggregated_socket_events_.end(); | 263 it != aggregated_socket_events_.end(); |
250 ++it) { | 264 ++it) { |
251 AggregatedSocketEvent* new_aggregated_socket_event = | 265 AggregatedSocketEvent* new_aggregated_socket_event = |
252 log.add_aggregated_socket_event(); | 266 log.add_aggregated_socket_event(); |
253 new_aggregated_socket_event->CopyFrom(it->second->aggregated_socket_event); | 267 new_aggregated_socket_event->CopyFrom(it->second->aggregated_socket_event); |
254 | 268 |
255 const std::deque<SocketEvent>& socket_events = it->second->socket_events; | 269 const std::deque<SocketEvent>& socket_events = it->second->socket_events; |
256 for (std::deque<SocketEvent>::const_iterator socket_event_it = | 270 for (std::deque<SocketEvent>::const_iterator socket_event_it = |
257 socket_events.begin(); | 271 socket_events.begin(); |
258 socket_event_it != socket_events.end(); | 272 socket_event_it != socket_events.end(); |
259 ++socket_event_it) { | 273 ++socket_event_it) { |
260 SocketEvent* socket_event = | 274 SocketEvent* socket_event = |
261 new_aggregated_socket_event->add_socket_event(); | 275 new_aggregated_socket_event->add_socket_event(); |
262 socket_event->CopyFrom(*socket_event_it); | 276 socket_event->CopyFrom(*socket_event_it); |
263 } | 277 } |
264 } | 278 } |
265 | 279 |
266 return log.SerializeToString(output); | 280 return log.SerializeToString(output); |
267 } | 281 } |
268 | 282 |
269 void Logger::Reset() { | 283 void Logger::Reset() { |
270 aggregated_socket_events_.clear(); | 284 aggregated_socket_events_.clear(); |
271 num_evicted_aggregated_socket_events_ = 0; | 285 log_.Clear(); |
272 num_evicted_socket_events_ = 0; | |
273 } | 286 } |
274 | 287 |
275 } // namespace cast_channel | 288 } // namespace cast_channel |
276 } // namespace api | 289 } // namespace api |
277 } // namespace extensions | 290 } // namespace extensions |
OLD | NEW |