Chromium Code Reviews| 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 | 15 |
| 15 class Value; | 16 class Value; |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 class TimeTicks; | 19 class TimeTicks; |
| 19 } | 20 } |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 | 23 |
| 23 // NetLog is the destination for log messages generated by the network stack. | 24 // NetLog is the destination for log messages generated by the network stack. |
| 24 // Each log message has a "source" field which identifies the specific entity | 25 // Each log message has a "source" field which identifies the specific entity |
| 25 // that generated the message (for example, which URLRequest or which | 26 // that generated the message (for example, which URLRequest or which |
| 26 // SocketStream). | 27 // SocketStream). |
| 27 // | 28 // |
| 28 // To avoid needing to pass in the "source id" to the logging functions, NetLog | 29 // To avoid needing to pass in the "source id" to the logging functions, NetLog |
| 29 // is usually accessed through a BoundNetLog, which will always pass in a | 30 // is usually accessed through a BoundNetLog, which will always pass in a |
| 30 // specific source ID. | 31 // specific source ID. |
| 31 // | 32 // |
| 32 // ******** The NetLog (and associated logging) is a work in progress ******** | 33 // ******** The NetLog (and associated logging) is a work in progress ******** |
| 33 // | 34 // |
| 34 // TODO(eroman): Remove the 'const' qualitifer from the BoundNetLog methods. | 35 // TODO(eroman): Remove the 'const' qualitifer from the BoundNetLog methods. |
| 35 // TODO(eroman): Start a new Source each time URLRequest redirects | 36 // TODO(eroman): Start a new Source each time URLRequest redirects |
| 36 // (simpler to reason about each as a separate entity). | 37 // (simpler to reason about each as a separate entity). |
| 37 | 38 |
| 38 class NetLog { | 39 class NET_API NetLog { |
| 39 public: | 40 public: |
| 40 enum EventType { | 41 enum EventType { |
| 41 #define EVENT_TYPE(label) TYPE_ ## label, | 42 #define EVENT_TYPE(label) TYPE_ ## label, |
| 42 #include "net/base/net_log_event_type_list.h" | 43 #include "net/base/net_log_event_type_list.h" |
| 43 #undef EVENT_TYPE | 44 #undef EVENT_TYPE |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 // The 'phase' of an event trace (whether it marks the beginning or end | 47 // The 'phase' of an event trace (whether it marks the beginning or end |
| 47 // of an event.). | 48 // of an event.). |
| 48 enum EventPhase { | 49 enum EventPhase { |
| 49 PHASE_NONE, | 50 PHASE_NONE, |
| 50 PHASE_BEGIN, | 51 PHASE_BEGIN, |
| 51 PHASE_END, | 52 PHASE_END, |
| 52 }; | 53 }; |
| 53 | 54 |
| 54 // The "source" identifies the entity that generated the log message. | 55 // The "source" identifies the entity that generated the log message. |
| 55 enum SourceType { | 56 enum SourceType { |
| 56 #define SOURCE_TYPE(label, value) SOURCE_ ## label = value, | 57 #define SOURCE_TYPE(label, value) SOURCE_ ## label = value, |
| 57 #include "net/base/net_log_source_type_list.h" | 58 #include "net/base/net_log_source_type_list.h" |
| 58 #undef SOURCE_TYPE | 59 #undef SOURCE_TYPE |
| 59 }; | 60 }; |
| 60 | 61 |
| 61 // Identifies the entity that generated this log. The |id| field should | 62 // Identifies the entity that generated this log. The |id| field should |
| 62 // uniquely identify the source, and is used by log observers to infer | 63 // uniquely identify the source, and is used by log observers to infer |
| 63 // message groupings. Can use NetLog::NextID() to create unique IDs. | 64 // message groupings. Can use NetLog::NextID() to create unique IDs. |
| 64 struct Source { | 65 struct NET_API Source { |
| 65 static const uint32 kInvalidId = 0; | 66 static const uint32 kInvalidId = 0; |
| 66 | 67 |
| 67 Source() : type(SOURCE_NONE), id(kInvalidId) {} | 68 Source() : type(SOURCE_NONE), id(kInvalidId) {} |
| 68 Source(SourceType type, uint32 id) : type(type), id(id) {} | 69 Source(SourceType type, uint32 id) : type(type), id(id) {} |
| 69 bool is_valid() const { return id != kInvalidId; } | 70 bool is_valid() const { return id != kInvalidId; } |
| 70 | 71 |
| 71 // The caller takes ownership of the returned Value*. | 72 // The caller takes ownership of the returned Value*. |
| 72 Value* ToValue() const; | 73 Value* ToValue() const; |
| 73 | 74 |
| 74 SourceType type; | 75 SourceType type; |
| 75 uint32 id; | 76 uint32 id; |
| 76 }; | 77 }; |
| 77 | 78 |
| 78 // Base class for associating additional parameters with an event. Log | 79 // Base class for associating additional parameters with an event. Log |
| 79 // observers need to know what specific derivations of EventParameters a | 80 // observers need to know what specific derivations of EventParameters a |
| 80 // particular EventType uses, in order to get at the individual components. | 81 // particular EventType uses, in order to get at the individual components. |
| 81 class EventParameters : public base::RefCountedThreadSafe<EventParameters> { | 82 class NET_API EventParameters |
| 83 : public base::RefCountedThreadSafe<EventParameters> { | |
| 82 public: | 84 public: |
| 83 EventParameters() {} | 85 EventParameters() {} |
| 84 virtual ~EventParameters() {} | 86 virtual ~EventParameters() {} |
| 85 | 87 |
| 86 // Serializes the parameters to a Value tree. This is intended to be a | 88 // Serializes the parameters to a Value tree. This is intended to be a |
| 87 // lossless conversion, which is used to serialize the parameters to JSON. | 89 // lossless conversion, which is used to serialize the parameters to JSON. |
| 88 // The caller takes ownership of the returned Value*. | 90 // The caller takes ownership of the returned Value*. |
| 89 virtual Value* ToValue() const = 0; | 91 virtual Value* ToValue() const = 0; |
| 90 | 92 |
| 91 private: | 93 private: |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 NetLog::EventPhase phase, | 158 NetLog::EventPhase phase, |
| 157 NetLog::EventParameters* params, | 159 NetLog::EventParameters* params, |
| 158 bool use_strings); | 160 bool use_strings); |
| 159 | 161 |
| 160 private: | 162 private: |
| 161 DISALLOW_COPY_AND_ASSIGN(NetLog); | 163 DISALLOW_COPY_AND_ASSIGN(NetLog); |
| 162 }; | 164 }; |
| 163 | 165 |
| 164 // Helper that binds a Source to a NetLog, and exposes convenience methods to | 166 // Helper that binds a Source to a NetLog, and exposes convenience methods to |
| 165 // output log messages without needing to pass in the source. | 167 // output log messages without needing to pass in the source. |
| 166 class BoundNetLog { | 168 class NET_API BoundNetLog { |
| 167 public: | 169 public: |
| 168 BoundNetLog() : net_log_(NULL) {} | 170 BoundNetLog() : net_log_(NULL) {} |
| 169 | 171 |
| 170 BoundNetLog(const NetLog::Source& source, NetLog* net_log) | 172 BoundNetLog(const NetLog::Source& source, NetLog* net_log) |
| 171 : source_(source), net_log_(net_log) { | 173 : source_(source), net_log_(net_log) { |
| 172 } | 174 } |
| 173 | 175 |
| 174 // Convenience methods that call through to the NetLog, passing in the | 176 // Convenience methods that call through to the NetLog, passing in the |
| 175 // currently bound source. | 177 // currently bound source. |
| 176 void AddEntry(NetLog::EventType type, | 178 void AddEntry(NetLog::EventType type, |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 | 274 |
| 273 virtual Value* ToValue() const; | 275 virtual Value* ToValue() const; |
| 274 | 276 |
| 275 private: | 277 private: |
| 276 const char* name_; | 278 const char* name_; |
| 277 const NetLog::Source value_; | 279 const NetLog::Source value_; |
| 278 }; | 280 }; |
| 279 | 281 |
| 280 // ScopedNetLogEvent logs a begin event on creation, and the corresponding end | 282 // ScopedNetLogEvent logs a begin event on creation, and the corresponding end |
| 281 // event on destruction. | 283 // event on destruction. |
| 282 class ScopedNetLogEvent { | 284 class NET_TEST ScopedNetLogEvent { |
|
wtc
2011/05/16 20:55:17
This kind of scoped class seems useful, so probabl
rvargas (doing something else)
2011/05/16 22:41:56
The point is that nobody out of net uses this toda
| |
| 283 public: | 285 public: |
| 284 ScopedNetLogEvent(const BoundNetLog& net_log, | 286 ScopedNetLogEvent(const BoundNetLog& net_log, |
| 285 NetLog::EventType event_type, | 287 NetLog::EventType event_type, |
| 286 const scoped_refptr<NetLog::EventParameters>& params); | 288 const scoped_refptr<NetLog::EventParameters>& params); |
| 287 | 289 |
| 288 ~ScopedNetLogEvent(); | 290 ~ScopedNetLogEvent(); |
| 289 | 291 |
| 290 // Sets the parameters that will logged on object destruction. Can be called | 292 // Sets the parameters that will logged on object destruction. Can be called |
| 291 // at most once for a given ScopedNetLogEvent object. If not called, the end | 293 // at most once for a given ScopedNetLogEvent object. If not called, the end |
| 292 // event will have no parameters. | 294 // event will have no parameters. |
| 293 void SetEndEventParameters( | 295 void SetEndEventParameters( |
| 294 const scoped_refptr<NetLog::EventParameters>& end_event_params); | 296 const scoped_refptr<NetLog::EventParameters>& end_event_params); |
| 295 | 297 |
| 296 const BoundNetLog& net_log() const; | 298 const BoundNetLog& net_log() const; |
| 297 | 299 |
| 298 private: | 300 private: |
| 299 BoundNetLog net_log_; | 301 BoundNetLog net_log_; |
| 300 const NetLog::EventType event_type_; | 302 const NetLog::EventType event_type_; |
| 301 scoped_refptr<NetLog::EventParameters> end_event_params_; | 303 scoped_refptr<NetLog::EventParameters> end_event_params_; |
| 302 }; | 304 }; |
| 303 | 305 |
| 304 } // namespace net | 306 } // namespace net |
| 305 | 307 |
| 306 #endif // NET_BASE_NET_LOG_H_ | 308 #endif // NET_BASE_NET_LOG_H_ |
| OLD | NEW |