OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef NET_BASE_NET_LOG_H_ | 5 #ifndef NET_BASE_NET_LOG_H_ |
6 #define NET_BASE_NET_LOG_H_ | 6 #define NET_BASE_NET_LOG_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
13 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "net/base/net_log.h" | |
16 | 15 |
17 class Value; | 16 class Value; |
18 | 17 |
19 namespace net { | 18 namespace net { |
20 | 19 |
21 // NetLog is the destination for log messages generated by the network stack. | 20 // NetLog is the destination for log messages generated by the network stack. |
22 // Each log message has a "source" field which identifies the specific entity | 21 // Each log message has a "source" field which identifies the specific entity |
23 // that generated the message (for example, which URLRequest or which | 22 // that generated the message (for example, which URLRequest or which |
24 // SocketStream). | 23 // SocketStream). |
25 // | 24 // |
26 // To avoid needing to pass in the "source id" to the logging functions, NetLog | 25 // To avoid needing to pass in the "source id" to the logging functions, NetLog |
27 // is usually accessed through a BoundNetLog, which will always pass in a | 26 // is usually accessed through a BoundNetLog, which will always pass in a |
28 // specific source ID. | 27 // specific source ID. |
29 // | 28 // |
30 // Note that NetLog is NOT THREADSAFE. | 29 // Note that NetLog is NOT THREADSAFE. |
31 // | 30 // |
32 // ******** The NetLog (and associated logging) is a work in progress ******** | 31 // ******** The NetLog (and associated logging) is a work in progress ******** |
33 // | 32 // |
34 // TODO(eroman): Remove the 'const' qualitifer from the BoundNetLog methods. | 33 // TODO(eroman): Remove the 'const' qualitifer from the BoundNetLog methods. |
35 // TODO(eroman): Remove NetLogUtil. Pretty printing should only be done from | 34 // TODO(eroman): Remove NetLogUtil. Pretty printing should only be done from |
36 // javascript, and should be very context-aware. | 35 // javascript, and should be very context-aware. |
37 // TODO(eroman): Move Capturing*NetLog to its own file. (And eventually remove | |
38 // all the consumers of it). | |
39 // TODO(eroman): Make the DNS jobs emit directly into the NetLog. | 36 // TODO(eroman): Make the DNS jobs emit directly into the NetLog. |
40 // TODO(eroman): Start a new Source each time URLRequest redirects | 37 // TODO(eroman): Start a new Source each time URLRequest redirects |
41 // (simpler to reason about each as a separate entity). | 38 // (simpler to reason about each as a separate entity). |
42 // TODO(eroman): Add the URLRequest load flags to the start entry. | 39 // TODO(eroman): Add the URLRequest load flags to the start entry. |
43 | 40 |
44 class NetLog { | 41 class NetLog { |
45 public: | 42 public: |
46 enum EventType { | 43 enum EventType { |
47 #define EVENT_TYPE(label) TYPE_ ## label, | 44 #define EVENT_TYPE(label) TYPE_ ## label, |
48 #include "net/base/net_log_event_type_list.h" | 45 #include "net/base/net_log_event_type_list.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 | 139 |
143 // TODO(eroman): This is a complete hack to allow passing in NULL in | 140 // TODO(eroman): This is a complete hack to allow passing in NULL in |
144 // place of a BoundNetLog. I added this while refactoring to simplify the | 141 // place of a BoundNetLog. I added this while refactoring to simplify the |
145 // task of updating all the callers. | 142 // task of updating all the callers. |
146 BoundNetLog(uint32) : net_log_(NULL) {} | 143 BoundNetLog(uint32) : net_log_(NULL) {} |
147 | 144 |
148 BoundNetLog(const NetLog::Source& source, NetLog* net_log) | 145 BoundNetLog(const NetLog::Source& source, NetLog* net_log) |
149 : source_(source), net_log_(net_log) { | 146 : source_(source), net_log_(net_log) { |
150 } | 147 } |
151 | 148 |
| 149 // Convenience methods that call through to the NetLog, passing in the |
| 150 // currently bound source. |
152 void AddEntry(NetLog::EventType type, | 151 void AddEntry(NetLog::EventType type, |
153 NetLog::EventPhase phase, | 152 NetLog::EventPhase phase, |
154 NetLog::EventParameters* extra_parameters) const; | 153 const scoped_refptr<NetLog::EventParameters>& params) const; |
155 | 154 |
156 void AddEntryWithTime(NetLog::EventType type, | 155 void AddEntryWithTime( |
157 const base::TimeTicks& time, | 156 NetLog::EventType type, |
158 NetLog::EventPhase phase, | 157 const base::TimeTicks& time, |
159 NetLog::EventParameters* extra_parameters) const; | 158 NetLog::EventPhase phase, |
| 159 const scoped_refptr<NetLog::EventParameters>& params) const; |
160 | 160 |
161 // Convenience methods that call through to the NetLog, passing in the | 161 // Convenience methods that call through to the NetLog, passing in the |
162 // currently bound source. | 162 // currently bound source, current time, and a fixed "capture phase" |
163 void AddEvent(NetLog::EventType event_type) const; | 163 // (begin, end, or none). |
164 void AddEventWithParameters(NetLog::EventType event_type, | 164 void AddEvent(NetLog::EventType event_type, |
165 NetLog::EventParameters* params) const; | 165 const scoped_refptr<NetLog::EventParameters>& params) const; |
| 166 void BeginEvent(NetLog::EventType event_type, |
| 167 const scoped_refptr<NetLog::EventParameters>& params) const; |
| 168 void EndEvent(NetLog::EventType event_type, |
| 169 const scoped_refptr<NetLog::EventParameters>& params) const; |
| 170 |
166 bool HasListener() const; | 171 bool HasListener() const; |
167 void BeginEvent(NetLog::EventType event_type) const; | |
168 void BeginEventWithParameters(NetLog::EventType event_type, | |
169 NetLog::EventParameters* params) const; | |
170 void BeginEventWithString(NetLog::EventType event_type, | |
171 const char* name, const std::string& value) const; | |
172 void BeginEventWithInteger(NetLog::EventType event_type, | |
173 const char* name, int value) const; | |
174 void AddEventWithInteger(NetLog::EventType event_type, | |
175 const char* name, int value) const; | |
176 void AddEventWithString(NetLog::EventType event_type, | |
177 const char* name, const std::string& value) const; | |
178 void EndEvent(NetLog::EventType event_type) const; | |
179 void EndEventWithParameters(NetLog::EventType event_type, | |
180 NetLog::EventParameters* params) const; | |
181 void EndEventWithInteger(NetLog::EventType event_type, | |
182 const char* name, int value) const; | |
183 | 172 |
184 // Helper to create a BoundNetLog given a NetLog and a SourceType. Takes care | 173 // Helper to create a BoundNetLog given a NetLog and a SourceType. Takes care |
185 // of creating a unique source ID, and handles the case of NULL net_log. | 174 // of creating a unique source ID, and handles the case of NULL net_log. |
186 static BoundNetLog Make(NetLog* net_log, NetLog::SourceType source_type); | 175 static BoundNetLog Make(NetLog* net_log, NetLog::SourceType source_type); |
187 | 176 |
188 const NetLog::Source& source() const { return source_; } | 177 const NetLog::Source& source() const { return source_; } |
189 NetLog* net_log() const { return net_log_; } | 178 NetLog* net_log() const { return net_log_; } |
190 | 179 |
191 private: | 180 private: |
192 NetLog::Source source_; | 181 NetLog::Source source_; |
(...skipping 30 matching lines...) Expand all Loading... |
223 return value_; | 212 return value_; |
224 } | 213 } |
225 | 214 |
226 virtual Value* ToValue() const; | 215 virtual Value* ToValue() const; |
227 | 216 |
228 private: | 217 private: |
229 const char* name_; | 218 const char* name_; |
230 const int value_; | 219 const int value_; |
231 }; | 220 }; |
232 | 221 |
233 // CapturingNetLog is an implementation of NetLog that saves messages to a | |
234 // bounded buffer. | |
235 class CapturingNetLog : public NetLog { | |
236 public: | |
237 struct Entry { | |
238 Entry(EventType type, | |
239 const base::TimeTicks& time, | |
240 Source source, | |
241 EventPhase phase, | |
242 EventParameters* extra_parameters) | |
243 : type(type), time(time), source(source), phase(phase), | |
244 extra_parameters(extra_parameters) { | |
245 } | |
246 | |
247 EventType type; | |
248 base::TimeTicks time; | |
249 Source source; | |
250 EventPhase phase; | |
251 scoped_refptr<EventParameters> extra_parameters; | |
252 }; | |
253 | |
254 // Ordered set of entries that were logged. | |
255 typedef std::vector<Entry> EntryList; | |
256 | |
257 enum { kUnbounded = -1 }; | |
258 | |
259 // Creates a CapturingNetLog that logs a maximum of |max_num_entries| | |
260 // messages. | |
261 explicit CapturingNetLog(size_t max_num_entries) | |
262 : next_id_(0), max_num_entries_(max_num_entries) {} | |
263 | |
264 // NetLog implementation: | |
265 virtual void AddEntry(EventType type, | |
266 const base::TimeTicks& time, | |
267 const Source& source, | |
268 EventPhase phase, | |
269 EventParameters* extra_parameters); | |
270 virtual uint32 NextID(); | |
271 virtual bool HasListener() const { return true; } | |
272 | |
273 // Returns the list of all entries in the log. | |
274 const EntryList& entries() const { return entries_; } | |
275 | |
276 void Clear(); | |
277 | |
278 private: | |
279 uint32 next_id_; | |
280 size_t max_num_entries_; | |
281 EntryList entries_; | |
282 | |
283 DISALLOW_COPY_AND_ASSIGN(CapturingNetLog); | |
284 }; | |
285 | |
286 // Helper class that exposes a similar API as BoundNetLog, but uses a | |
287 // CapturingNetLog rather than the more generic NetLog. | |
288 // | |
289 // CapturingBoundNetLog can easily be converted to a BoundNetLog using the | |
290 // bound() method. | |
291 class CapturingBoundNetLog { | |
292 public: | |
293 CapturingBoundNetLog(const NetLog::Source& source, CapturingNetLog* net_log) | |
294 : source_(source), capturing_net_log_(net_log) { | |
295 } | |
296 | |
297 explicit CapturingBoundNetLog(size_t max_num_entries) | |
298 : capturing_net_log_(new CapturingNetLog(max_num_entries)) {} | |
299 | |
300 // The returned BoundNetLog is only valid while |this| is alive. | |
301 BoundNetLog bound() const { | |
302 return BoundNetLog(source_, capturing_net_log_.get()); | |
303 } | |
304 | |
305 // Returns the list of all entries in the log. | |
306 const CapturingNetLog::EntryList& entries() const { | |
307 return capturing_net_log_->entries(); | |
308 } | |
309 | |
310 void Clear(); | |
311 | |
312 // Sends all of captured messages to |net_log|, using the same source ID | |
313 // as |net_log|. | |
314 void AppendTo(const BoundNetLog& net_log) const; | |
315 | |
316 private: | |
317 NetLog::Source source_; | |
318 scoped_ptr<CapturingNetLog> capturing_net_log_; | |
319 | |
320 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); | |
321 }; | |
322 | |
323 } // namespace net | 222 } // namespace net |
324 | 223 |
325 #endif // NET_BASE_NET_LOG_H_ | 224 #endif // NET_BASE_NET_LOG_H_ |
OLD | NEW |