| 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_CAPTURING_NET_LOG_H_ |
| 6 #define NET_BASE_NET_LOG_H_ | 6 #define NET_BASE_CAPTURING_NET_LOG_H_ |
| 7 | 7 |
| 8 #include <string> | |
| 9 #include <vector> | 8 #include <vector> |
| 10 | 9 |
| 11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 12 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
| 13 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 14 #include "base/time.h" | |
| 15 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
| 16 | 14 |
| 17 namespace net { | 15 namespace net { |
| 18 | 16 |
| 19 // NetLog is the destination for log messages generated by the network stack. | |
| 20 // Each log message has a "source" field which identifies the specific entity | |
| 21 // that generated the message (for example, which URLRequest or which | |
| 22 // SocketStream). | |
| 23 // | |
| 24 // To avoid needing to pass in the "source id" to the logging functions, NetLog | |
| 25 // is usually accessed through a BoundNetLog, which will always pass in a | |
| 26 // specific source ID. | |
| 27 // | |
| 28 // Note that NetLog is NOT THREADSAFE. | |
| 29 // | |
| 30 // ******** The NetLog (and associated logging) is a work in progress ******** | |
| 31 // | |
| 32 // TODO(eroman): Remove the 'const' qualitifer from the BoundNetLog methods. | |
| 33 // TODO(eroman): Remove the AddString() and AddStringLiteral() methods. | |
| 34 // These are a carry-over from old approach. Really, consumers | |
| 35 // should be calling AddEventWithParameters(), and passing a | |
| 36 // custom EventParameters* object that encapsulates all of the | |
| 37 // interesting state. | |
| 38 // TODO(eroman): Remove NetLogUtil. Pretty printing should only be done from | |
| 39 // javascript, and should be very context-aware. | |
| 40 // TODO(eroman): Move Capturing*NetLog to its own file. (And eventually remove | |
| 41 // all the consumers of it). | |
| 42 // TODO(eroman): Make the DNS jobs emit directly into the NetLog. | |
| 43 // TODO(eroman): Start a new Source each time URLRequest redirects | |
| 44 // (simpler to reason about each as a separate entity). | |
| 45 // TODO(eroman): Add the URLRequest load flags to the start entry. | |
| 46 | |
| 47 class NetLog { | |
| 48 public: | |
| 49 enum EventType { | |
| 50 #define EVENT_TYPE(label) TYPE_ ## label, | |
| 51 #include "net/base/net_log_event_type_list.h" | |
| 52 #undef EVENT_TYPE | |
| 53 }; | |
| 54 | |
| 55 // The 'phase' of an event trace (whether it marks the beginning or end | |
| 56 // of an event.). | |
| 57 enum EventPhase { | |
| 58 PHASE_NONE, | |
| 59 PHASE_BEGIN, | |
| 60 PHASE_END, | |
| 61 }; | |
| 62 | |
| 63 // The "source" identifies the entity that generated the log message. | |
| 64 enum SourceType { | |
| 65 SOURCE_NONE, | |
| 66 SOURCE_URL_REQUEST, | |
| 67 SOURCE_SOCKET_STREAM, | |
| 68 SOURCE_INIT_PROXY_RESOLVER, | |
| 69 SOURCE_CONNECT_JOB, | |
| 70 SOURCE_SOCKET, | |
| 71 }; | |
| 72 | |
| 73 // Identifies the entity that generated this log. The |id| field should | |
| 74 // uniquely identify the source, and is used by log observers to infer | |
| 75 // message groupings. Can use NetLog::NextID() to create unique IDs. | |
| 76 struct Source { | |
| 77 static const uint32 kInvalidId = 0; | |
| 78 | |
| 79 Source() : type(SOURCE_NONE), id(kInvalidId) {} | |
| 80 Source(SourceType type, uint32 id) : type(type), id(id) {} | |
| 81 bool is_valid() { return id != kInvalidId; } | |
| 82 | |
| 83 SourceType type; | |
| 84 uint32 id; | |
| 85 }; | |
| 86 | |
| 87 // Base class for associating additional parameters with an event. Log | |
| 88 // observers need to know what specific derivations of EventParameters a | |
| 89 // particular EventType uses, in order to get at the individual components. | |
| 90 class EventParameters : public base::RefCounted<EventParameters> { | |
| 91 public: | |
| 92 EventParameters() {} | |
| 93 virtual ~EventParameters() {} | |
| 94 | |
| 95 // Serializes the parameters to a string representation (this should be a | |
| 96 // lossless conversion). | |
| 97 virtual std::string ToString() const = 0; | |
| 98 | |
| 99 private: | |
| 100 DISALLOW_COPY_AND_ASSIGN(EventParameters); | |
| 101 }; | |
| 102 | |
| 103 NetLog() {} | |
| 104 virtual ~NetLog() {} | |
| 105 | |
| 106 // Emits an event to the log stream. | |
| 107 // |type| - The type of the event. | |
| 108 // |time| - The time when the event occurred. | |
| 109 // |source| - The source that generated the event. | |
| 110 // |phase| - An optional parameter indicating whether this is the start/end | |
| 111 // of an action. | |
| 112 // |extra_parameters| - Optional (may be NULL) parameters for this event. | |
| 113 // The specific subclass of EventParameters is defined | |
| 114 // by the contract for events of this |type|. | |
| 115 virtual void AddEntry(EventType type, | |
| 116 const base::TimeTicks& time, | |
| 117 const Source& source, | |
| 118 EventPhase phase, | |
| 119 EventParameters* extra_parameters) = 0; | |
| 120 | |
| 121 // Returns a unique ID which can be used as a source ID. | |
| 122 virtual uint32 NextID() = 0; | |
| 123 | |
| 124 // Returns true if more complicated messages should be sent to the log. | |
| 125 // TODO(eroman): This is a carry-over from refactoring; figure out | |
| 126 // something better. | |
| 127 virtual bool HasListener() const = 0; | |
| 128 | |
| 129 // Returns a C-String symbolic name for |event_type|. | |
| 130 static const char* EventTypeToString(EventType event_type); | |
| 131 | |
| 132 // Returns a list of all the available EventTypes. | |
| 133 static std::vector<EventType> GetAllEventTypes(); | |
| 134 | |
| 135 private: | |
| 136 DISALLOW_COPY_AND_ASSIGN(NetLog); | |
| 137 }; | |
| 138 | |
| 139 // Helper that binds a Source to a NetLog, and exposes convenience methods to | |
| 140 // output log messages without needing to pass in the source. | |
| 141 class BoundNetLog { | |
| 142 public: | |
| 143 BoundNetLog() : net_log_(NULL) {} | |
| 144 | |
| 145 // TODO(eroman): This is a complete hack to allow passing in NULL in | |
| 146 // place of a BoundNetLog. I added this while refactoring to simplify the | |
| 147 // task of updating all the callers. | |
| 148 BoundNetLog(uint32) : net_log_(NULL) {} | |
| 149 | |
| 150 BoundNetLog(const NetLog::Source& source, NetLog* net_log) | |
| 151 : source_(source), net_log_(net_log) { | |
| 152 } | |
| 153 | |
| 154 void AddEntry(NetLog::EventType type, | |
| 155 NetLog::EventPhase phase, | |
| 156 NetLog::EventParameters* extra_parameters) const; | |
| 157 | |
| 158 void AddEntryWithTime(NetLog::EventType type, | |
| 159 const base::TimeTicks& time, | |
| 160 NetLog::EventPhase phase, | |
| 161 NetLog::EventParameters* extra_parameters) const; | |
| 162 | |
| 163 // Convenience methods that call through to the NetLog, passing in the | |
| 164 // currently bound source. | |
| 165 void AddEvent(NetLog::EventType event_type) const; | |
| 166 void AddEventWithParameters(NetLog::EventType event_type, | |
| 167 NetLog::EventParameters* params) const; | |
| 168 bool HasListener() const; | |
| 169 void BeginEvent(NetLog::EventType event_type) const; | |
| 170 void BeginEventWithParameters(NetLog::EventType event_type, | |
| 171 NetLog::EventParameters* params) const; | |
| 172 void BeginEventWithString(NetLog::EventType event_type, | |
| 173 const std::string& string) const; | |
| 174 void BeginEventWithInteger(NetLog::EventType event_type, int integer) const; | |
| 175 void AddEventWithInteger(NetLog::EventType event_type, int integer) const; | |
| 176 void EndEvent(NetLog::EventType event_type) const; | |
| 177 void EndEventWithParameters(NetLog::EventType event_type, | |
| 178 NetLog::EventParameters* params) const; | |
| 179 void EndEventWithInteger(NetLog::EventType event_type, int integer) const; | |
| 180 | |
| 181 // Deprecated: Don't add new dependencies that use these methods. Instead, use | |
| 182 // AddEventWithParameters(). | |
| 183 void AddString(const std::string& string) const; | |
| 184 void AddStringLiteral(const char* literal) const; | |
| 185 | |
| 186 // Helper to create a BoundNetLog given a NetLog and a SourceType. Takes care | |
| 187 // of creating a unique source ID, and handles the case of NULL net_log. | |
| 188 static BoundNetLog Make(NetLog* net_log, NetLog::SourceType source_type); | |
| 189 | |
| 190 const NetLog::Source& source() const { return source_; } | |
| 191 NetLog* net_log() const { return net_log_; } | |
| 192 | |
| 193 private: | |
| 194 NetLog::Source source_; | |
| 195 NetLog* net_log_; | |
| 196 }; | |
| 197 | |
| 198 // NetLogStringParameter is a subclass of EventParameters that encapsulates a | |
| 199 // single std::string parameter. | |
| 200 class NetLogStringParameter : public NetLog::EventParameters { | |
| 201 public: | |
| 202 explicit NetLogStringParameter(const std::string& value); | |
| 203 | |
| 204 const std::string& value() const { | |
| 205 return value_; | |
| 206 } | |
| 207 | |
| 208 virtual std::string ToString() const { | |
| 209 return value_; | |
| 210 } | |
| 211 | |
| 212 private: | |
| 213 std::string value_; | |
| 214 }; | |
| 215 | |
| 216 // NetLogIntegerParameter is a subclass of EventParameters that encapsulates a | |
| 217 // single integer parameter. | |
| 218 class NetLogIntegerParameter : public NetLog::EventParameters { | |
| 219 public: | |
| 220 explicit NetLogIntegerParameter(int value) : value_(value) {} | |
| 221 | |
| 222 int value() const { | |
| 223 return value_; | |
| 224 } | |
| 225 | |
| 226 virtual std::string ToString() const; | |
| 227 | |
| 228 private: | |
| 229 const int value_; | |
| 230 }; | |
| 231 | |
| 232 // NetLogStringLiteralParameter is a subclass of EventParameters that | |
| 233 // encapsulates a single string literal parameter. | |
| 234 class NetLogStringLiteralParameter : public NetLog::EventParameters { | |
| 235 public: | |
| 236 explicit NetLogStringLiteralParameter(const char* value) : value_(value) {} | |
| 237 | |
| 238 const char* const value() const { | |
| 239 return value_; | |
| 240 } | |
| 241 | |
| 242 virtual std::string ToString() const; | |
| 243 | |
| 244 private: | |
| 245 const char* const value_; | |
| 246 }; | |
| 247 | |
| 248 | |
| 249 // CapturingNetLog is an implementation of NetLog that saves messages to a | 17 // CapturingNetLog is an implementation of NetLog that saves messages to a |
| 250 // bounded buffer. | 18 // bounded buffer. |
| 251 class CapturingNetLog : public NetLog { | 19 class CapturingNetLog : public NetLog { |
| 252 public: | 20 public: |
| 253 struct Entry { | 21 struct Entry { |
| 254 Entry(EventType type, | 22 Entry(EventType type, |
| 255 const base::TimeTicks& time, | 23 const base::TimeTicks& time, |
| 256 Source source, | 24 Source source, |
| 257 EventPhase phase, | 25 EventPhase phase, |
| 258 EventParameters* extra_parameters) | 26 EventParameters* extra_parameters) |
| 259 : type(type), time(time), source(source), phase(phase), | 27 : type(type), time(time), source(source), phase(phase), |
| 260 extra_parameters(extra_parameters) { | 28 extra_parameters(extra_parameters) { |
| 261 } | 29 } |
| 262 | 30 |
| 263 EventType type; | 31 EventType type; |
| 264 base::TimeTicks time; | 32 base::TimeTicks time; |
| 265 Source source; | 33 Source source; |
| 266 EventPhase phase; | 34 EventPhase phase; |
| 267 scoped_refptr<EventParameters> extra_parameters; | 35 scoped_refptr<EventParameters> extra_parameters; |
| 268 }; | 36 }; |
| 269 | 37 |
| 270 // Ordered set of entries that were logged. | 38 // Ordered set of entries that were logged. |
| 271 typedef std::vector<Entry> EntryList; | 39 typedef std::vector<Entry> EntryList; |
| 272 | 40 |
| 273 enum { kUnbounded = -1 }; | 41 enum { kUnbounded = -1 }; |
| 274 | 42 |
| 275 // Creates a CapturingNetLog that logs a maximum of |max_num_entries| | 43 // Creates a CapturingNetLog that logs a maximum of |max_num_entries| |
| 276 // messages. | 44 // messages. |
| 277 explicit CapturingNetLog(size_t max_num_entries) | 45 explicit CapturingNetLog(size_t max_num_entries); |
| 278 : next_id_(0), max_num_entries_(max_num_entries) {} | |
| 279 | 46 |
| 280 // NetLog implementation: | 47 // NetLog implementation: |
| 281 virtual void AddEntry(EventType type, | 48 virtual void AddEntry(EventType type, |
| 282 const base::TimeTicks& time, | 49 const base::TimeTicks& time, |
| 283 const Source& source, | 50 const Source& source, |
| 284 EventPhase phase, | 51 EventPhase phase, |
| 285 EventParameters* extra_parameters); | 52 EventParameters* extra_parameters); |
| 286 virtual uint32 NextID(); | 53 virtual uint32 NextID(); |
| 287 virtual bool HasListener() const { return true; } | 54 virtual bool HasListener() const { return true; } |
| 288 | 55 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 | 98 |
| 332 private: | 99 private: |
| 333 NetLog::Source source_; | 100 NetLog::Source source_; |
| 334 scoped_ptr<CapturingNetLog> capturing_net_log_; | 101 scoped_ptr<CapturingNetLog> capturing_net_log_; |
| 335 | 102 |
| 336 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); | 103 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); |
| 337 }; | 104 }; |
| 338 | 105 |
| 339 } // namespace net | 106 } // namespace net |
| 340 | 107 |
| 341 #endif // NET_BASE_NET_LOG_H_ | 108 #endif // NET_BASE_CAPTURING_NET_LOG_H_ |
| OLD | NEW |