OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "net/base/net_api.h" | 14 #include "net/base/net_export.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class Value; | 17 class Value; |
18 } | 18 } |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 class TimeTicks; | 21 class TimeTicks; |
22 } | 22 } |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
26 // NetLog is the destination for log messages generated by the network stack. | 26 // NetLog is the destination for log messages generated by the network stack. |
27 // Each log message has a "source" field which identifies the specific entity | 27 // Each log message has a "source" field which identifies the specific entity |
28 // that generated the message (for example, which URLRequest or which | 28 // that generated the message (for example, which URLRequest or which |
29 // SocketStream). | 29 // SocketStream). |
30 // | 30 // |
31 // To avoid needing to pass in the "source id" to the logging functions, NetLog | 31 // To avoid needing to pass in the "source id" to the logging functions, NetLog |
32 // is usually accessed through a BoundNetLog, which will always pass in a | 32 // is usually accessed through a BoundNetLog, which will always pass in a |
33 // specific source ID. | 33 // specific source ID. |
34 // | 34 // |
35 class NET_API NetLog { | 35 class NET_EXPORT NetLog { |
36 public: | 36 public: |
37 enum EventType { | 37 enum EventType { |
38 #define EVENT_TYPE(label) TYPE_ ## label, | 38 #define EVENT_TYPE(label) TYPE_ ## label, |
39 #include "net/base/net_log_event_type_list.h" | 39 #include "net/base/net_log_event_type_list.h" |
40 #undef EVENT_TYPE | 40 #undef EVENT_TYPE |
41 }; | 41 }; |
42 | 42 |
43 // The 'phase' of an event trace (whether it marks the beginning or end | 43 // The 'phase' of an event trace (whether it marks the beginning or end |
44 // of an event.). | 44 // of an event.). |
45 enum EventPhase { | 45 enum EventPhase { |
46 PHASE_NONE, | 46 PHASE_NONE, |
47 PHASE_BEGIN, | 47 PHASE_BEGIN, |
48 PHASE_END, | 48 PHASE_END, |
49 }; | 49 }; |
50 | 50 |
51 // The "source" identifies the entity that generated the log message. | 51 // The "source" identifies the entity that generated the log message. |
52 enum SourceType { | 52 enum SourceType { |
53 #define SOURCE_TYPE(label, value) SOURCE_ ## label = value, | 53 #define SOURCE_TYPE(label, value) SOURCE_ ## label = value, |
54 #include "net/base/net_log_source_type_list.h" | 54 #include "net/base/net_log_source_type_list.h" |
55 #undef SOURCE_TYPE | 55 #undef SOURCE_TYPE |
56 }; | 56 }; |
57 | 57 |
58 // Identifies the entity that generated this log. The |id| field should | 58 // Identifies the entity that generated this log. The |id| field should |
59 // uniquely identify the source, and is used by log observers to infer | 59 // uniquely identify the source, and is used by log observers to infer |
60 // message groupings. Can use NetLog::NextID() to create unique IDs. | 60 // message groupings. Can use NetLog::NextID() to create unique IDs. |
61 struct NET_API Source { | 61 struct NET_EXPORT Source { |
62 static const uint32 kInvalidId = 0; | 62 static const uint32 kInvalidId = 0; |
63 | 63 |
64 Source() : type(SOURCE_NONE), id(kInvalidId) {} | 64 Source() : type(SOURCE_NONE), id(kInvalidId) {} |
65 Source(SourceType type, uint32 id) : type(type), id(id) {} | 65 Source(SourceType type, uint32 id) : type(type), id(id) {} |
66 bool is_valid() const { return id != kInvalidId; } | 66 bool is_valid() const { return id != kInvalidId; } |
67 | 67 |
68 // The caller takes ownership of the returned Value*. | 68 // The caller takes ownership of the returned Value*. |
69 base::Value* ToValue() const; | 69 base::Value* ToValue() const; |
70 | 70 |
71 SourceType type; | 71 SourceType type; |
72 uint32 id; | 72 uint32 id; |
73 }; | 73 }; |
74 | 74 |
75 // Base class for associating additional parameters with an event. Log | 75 // Base class for associating additional parameters with an event. Log |
76 // observers need to know what specific derivations of EventParameters a | 76 // observers need to know what specific derivations of EventParameters a |
77 // particular EventType uses, in order to get at the individual components. | 77 // particular EventType uses, in order to get at the individual components. |
78 class NET_API EventParameters | 78 class NET_EXPORT EventParameters |
79 : public base::RefCountedThreadSafe<EventParameters> { | 79 : public base::RefCountedThreadSafe<EventParameters> { |
80 public: | 80 public: |
81 EventParameters() {} | 81 EventParameters() {} |
82 virtual ~EventParameters() {} | 82 virtual ~EventParameters() {} |
83 | 83 |
84 // Serializes the parameters to a Value tree. This is intended to be a | 84 // Serializes the parameters to a Value tree. This is intended to be a |
85 // lossless conversion, which is used to serialize the parameters to JSON. | 85 // lossless conversion, which is used to serialize the parameters to JSON. |
86 // The caller takes ownership of the returned Value*. | 86 // The caller takes ownership of the returned Value*. |
87 virtual base::Value* ToValue() const = 0; | 87 virtual base::Value* ToValue() const = 0; |
88 | 88 |
(...skipping 10 matching lines...) Expand all Loading... |
99 // Log all events, but do not include the actual transferred bytes as | 99 // Log all events, but do not include the actual transferred bytes as |
100 // parameters for bytes sent/received events. | 100 // parameters for bytes sent/received events. |
101 LOG_ALL_BUT_BYTES, | 101 LOG_ALL_BUT_BYTES, |
102 | 102 |
103 // Only log events which are cheap, and don't consume much memory. | 103 // Only log events which are cheap, and don't consume much memory. |
104 LOG_BASIC, | 104 LOG_BASIC, |
105 }; | 105 }; |
106 | 106 |
107 // An observer, that must ensure its own thread safety, for events | 107 // An observer, that must ensure its own thread safety, for events |
108 // being added to a NetLog. | 108 // being added to a NetLog. |
109 class NET_API ThreadSafeObserver { | 109 class NET_EXPORT ThreadSafeObserver { |
110 public: | 110 public: |
111 // Constructs an observer that wants to see network events, with | 111 // Constructs an observer that wants to see network events, with |
112 // the specified minimum event granularity. A ThreadSafeObserver can only | 112 // the specified minimum event granularity. A ThreadSafeObserver can only |
113 // observe a single NetLog at a time. | 113 // observe a single NetLog at a time. |
114 // | 114 // |
115 // Typical observers should specify LOG_BASIC. | 115 // Typical observers should specify LOG_BASIC. |
116 // | 116 // |
117 // Observers that need to see the full granularity of events can | 117 // Observers that need to see the full granularity of events can |
118 // specify LOG_ALL. However doing so will have performance consequences. | 118 // specify LOG_ALL. However doing so will have performance consequences. |
119 // | 119 // |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 NetLog::EventPhase phase, | 206 NetLog::EventPhase phase, |
207 NetLog::EventParameters* params, | 207 NetLog::EventParameters* params, |
208 bool use_strings); | 208 bool use_strings); |
209 | 209 |
210 private: | 210 private: |
211 DISALLOW_COPY_AND_ASSIGN(NetLog); | 211 DISALLOW_COPY_AND_ASSIGN(NetLog); |
212 }; | 212 }; |
213 | 213 |
214 // Helper that binds a Source to a NetLog, and exposes convenience methods to | 214 // Helper that binds a Source to a NetLog, and exposes convenience methods to |
215 // output log messages without needing to pass in the source. | 215 // output log messages without needing to pass in the source. |
216 class NET_API BoundNetLog { | 216 class NET_EXPORT BoundNetLog { |
217 public: | 217 public: |
218 BoundNetLog() : net_log_(NULL) {} | 218 BoundNetLog() : net_log_(NULL) {} |
219 | 219 |
220 BoundNetLog(const NetLog::Source& source, NetLog* net_log) | 220 BoundNetLog(const NetLog::Source& source, NetLog* net_log) |
221 : source_(source), net_log_(net_log) { | 221 : source_(source), net_log_(net_log) { |
222 } | 222 } |
223 | 223 |
224 // Convenience methods that call through to the NetLog, passing in the | 224 // Convenience methods that call through to the NetLog, passing in the |
225 // currently bound source. | 225 // currently bound source. |
226 void AddEntry(NetLog::EventType type, | 226 void AddEntry(NetLog::EventType type, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 | 308 |
309 virtual base::Value* ToValue() const; | 309 virtual base::Value* ToValue() const; |
310 | 310 |
311 private: | 311 private: |
312 const char* name_; | 312 const char* name_; |
313 const int value_; | 313 const int value_; |
314 }; | 314 }; |
315 | 315 |
316 // NetLogSourceParameter is a subclass of EventParameters that encapsulates a | 316 // NetLogSourceParameter is a subclass of EventParameters that encapsulates a |
317 // single NetLog::Source parameter. | 317 // single NetLog::Source parameter. |
318 class NET_API NetLogSourceParameter : public NetLog::EventParameters { | 318 class NET_EXPORT NetLogSourceParameter : public NetLog::EventParameters { |
319 public: | 319 public: |
320 // |name| must be a string literal. | 320 // |name| must be a string literal. |
321 NetLogSourceParameter(const char* name, const NetLog::Source& value) | 321 NetLogSourceParameter(const char* name, const NetLog::Source& value) |
322 : name_(name), value_(value) {} | 322 : name_(name), value_(value) {} |
323 | 323 |
324 const NetLog::Source& value() const { | 324 const NetLog::Source& value() const { |
325 return value_; | 325 return value_; |
326 } | 326 } |
327 | 327 |
328 virtual base::Value* ToValue() const; | 328 virtual base::Value* ToValue() const; |
329 | 329 |
330 private: | 330 private: |
331 const char* name_; | 331 const char* name_; |
332 const NetLog::Source value_; | 332 const NetLog::Source value_; |
333 }; | 333 }; |
334 | 334 |
335 // ScopedNetLogEvent logs a begin event on creation, and the corresponding end | 335 // ScopedNetLogEvent logs a begin event on creation, and the corresponding end |
336 // event on destruction. | 336 // event on destruction. |
337 class NET_TEST ScopedNetLogEvent { | 337 class NET_EXPORT_PRIVATE ScopedNetLogEvent { |
338 public: | 338 public: |
339 ScopedNetLogEvent(const BoundNetLog& net_log, | 339 ScopedNetLogEvent(const BoundNetLog& net_log, |
340 NetLog::EventType event_type, | 340 NetLog::EventType event_type, |
341 const scoped_refptr<NetLog::EventParameters>& params); | 341 const scoped_refptr<NetLog::EventParameters>& params); |
342 | 342 |
343 ~ScopedNetLogEvent(); | 343 ~ScopedNetLogEvent(); |
344 | 344 |
345 // Sets the parameters that will logged on object destruction. Can be called | 345 // Sets the parameters that will logged on object destruction. Can be called |
346 // at most once for a given ScopedNetLogEvent object. If not called, the end | 346 // at most once for a given ScopedNetLogEvent object. If not called, the end |
347 // event will have no parameters. | 347 // event will have no parameters. |
348 void SetEndEventParameters( | 348 void SetEndEventParameters( |
349 const scoped_refptr<NetLog::EventParameters>& end_event_params); | 349 const scoped_refptr<NetLog::EventParameters>& end_event_params); |
350 | 350 |
351 const BoundNetLog& net_log() const; | 351 const BoundNetLog& net_log() const; |
352 | 352 |
353 private: | 353 private: |
354 BoundNetLog net_log_; | 354 BoundNetLog net_log_; |
355 const NetLog::EventType event_type_; | 355 const NetLog::EventType event_type_; |
356 scoped_refptr<NetLog::EventParameters> end_event_params_; | 356 scoped_refptr<NetLog::EventParameters> end_event_params_; |
357 }; | 357 }; |
358 | 358 |
359 } // namespace net | 359 } // namespace net |
360 | 360 |
361 #endif // NET_BASE_NET_LOG_H_ | 361 #endif // NET_BASE_NET_LOG_H_ |
OLD | NEW |