OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_LOG_NET_LOG_H_ | 5 #ifndef NET_LOG_NET_LOG_H_ |
6 #define NET_LOG_NET_LOG_H_ | 6 #define NET_LOG_NET_LOG_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 | 11 |
12 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "net/base/net_export.h" | 20 #include "net/base/net_export.h" |
21 #include "net/log/net_log_capture_mode.h" | |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 class DictionaryValue; | 24 class DictionaryValue; |
24 class Value; | 25 class Value; |
25 } | 26 } |
26 | 27 |
27 namespace net { | 28 namespace net { |
28 | 29 |
29 // NetLog is the destination for log messages generated by the network stack. | 30 // NetLog is the destination for log messages generated by the network stack. |
30 // Each log message has a "source" field which identifies the specific entity | 31 // Each log message has a "source" field which identifies the specific entity |
(...skipping 28 matching lines...) Expand all Loading... | |
59 }; | 60 }; |
60 | 61 |
61 // The "source" identifies the entity that generated the log message. | 62 // The "source" identifies the entity that generated the log message. |
62 enum SourceType { | 63 enum SourceType { |
63 #define SOURCE_TYPE(label) SOURCE_##label, | 64 #define SOURCE_TYPE(label) SOURCE_##label, |
64 #include "net/log/net_log_source_type_list.h" | 65 #include "net/log/net_log_source_type_list.h" |
65 #undef SOURCE_TYPE | 66 #undef SOURCE_TYPE |
66 SOURCE_COUNT | 67 SOURCE_COUNT |
67 }; | 68 }; |
68 | 69 |
69 // Specifies the granularity of events that should be emitted to the log. | |
eroman
2015/04/09 00:08:19
changes in this file
| |
70 // | |
71 // Since the LogLevel may be read and set on any thread without locking, it | |
72 // may be possible for an Observer to receive an event or parameters that | |
73 // normally wouldn't be logged at the currently active log level. | |
74 enum LogLevel { | |
75 // Log everything possible, even if it is slow and memory expensive. | |
76 // Includes logging of transferred bytes. | |
77 LOG_ALL, | |
78 | |
79 // Log all events, but do not include the actual transferred bytes as | |
80 // parameters for bytes sent/received events. | |
81 LOG_ALL_BUT_BYTES, | |
82 | |
83 // Log all events, but do not include the actual transferred bytes and | |
84 // remove cookies and HTTP credentials. | |
85 LOG_STRIP_PRIVATE_DATA, | |
86 | |
87 // Don't log any events. | |
88 LOG_NONE, | |
89 }; | |
90 | |
91 // A callback function that return a Value representation of the parameters | 70 // A callback function that return a Value representation of the parameters |
92 // associated with an event. If called, it will be called synchonously, | 71 // associated with an event. If called, it will be called synchonously, |
93 // so it need not have owning references. May be called more than once, or | 72 // so it need not have owning references. May be called more than once, or |
94 // not at all. May return NULL. | 73 // not at all. May return NULL. |
95 typedef base::Callback<base::Value*(LogLevel)> ParametersCallback; | 74 typedef base::Callback<base::Value*(NetLogCaptureMode)> ParametersCallback; |
96 | 75 |
97 // Identifies the entity that generated this log. The |id| field should | 76 // Identifies the entity that generated this log. The |id| field should |
98 // uniquely identify the source, and is used by log observers to infer | 77 // uniquely identify the source, and is used by log observers to infer |
99 // message groupings. Can use NetLog::NextID() to create unique IDs. | 78 // message groupings. Can use NetLog::NextID() to create unique IDs. |
100 struct NET_EXPORT Source { | 79 struct NET_EXPORT Source { |
101 static const uint32 kInvalidId; | 80 static const uint32 kInvalidId; |
102 | 81 |
103 Source(); | 82 Source(); |
104 Source(SourceType type, uint32 id); | 83 Source(SourceType type, uint32 id); |
105 bool IsValid() const; | 84 bool IsValid() const; |
(...skipping 24 matching lines...) Expand all Loading... | |
130 const ParametersCallback* parameters_callback); | 109 const ParametersCallback* parameters_callback); |
131 ~EntryData(); | 110 ~EntryData(); |
132 | 111 |
133 const EventType type; | 112 const EventType type; |
134 const Source source; | 113 const Source source; |
135 const EventPhase phase; | 114 const EventPhase phase; |
136 const base::TimeTicks time; | 115 const base::TimeTicks time; |
137 const ParametersCallback* const parameters_callback; | 116 const ParametersCallback* const parameters_callback; |
138 }; | 117 }; |
139 | 118 |
140 // An Entry pre-binds EntryData to a LogLevel, so observers will observe the | 119 // An Entry pre-binds EntryData to a capture mode, so observers will observe |
141 // output of ToValue() and ParametersToValue() at their log level rather than | 120 // the output of ToValue() and ParametersToValue() at their log capture mode |
142 // current maximum. | 121 // rather than the current maximum. |
143 class NET_EXPORT Entry { | 122 class NET_EXPORT Entry { |
144 public: | 123 public: |
145 Entry(const EntryData* data, LogLevel log_level); | 124 Entry(const EntryData* data, NetLogCaptureMode capture_mode); |
146 ~Entry(); | 125 ~Entry(); |
147 | 126 |
148 EventType type() const { return data_->type; } | 127 EventType type() const { return data_->type; } |
149 Source source() const { return data_->source; } | 128 Source source() const { return data_->source; } |
150 EventPhase phase() const { return data_->phase; } | 129 EventPhase phase() const { return data_->phase; } |
151 | 130 |
152 // Serializes the specified event to a Value. The Value also includes the | 131 // Serializes the specified event to a Value. The Value also includes the |
153 // current time. Caller takes ownership of returned Value. Takes in a time | 132 // current time. Caller takes ownership of returned Value. Takes in a time |
154 // to allow back-dating entries. | 133 // to allow back-dating entries. |
155 base::Value* ToValue() const; | 134 base::Value* ToValue() const; |
156 | 135 |
157 // Returns the parameters as a Value. Returns NULL if there are no | 136 // Returns the parameters as a Value. Returns NULL if there are no |
158 // parameters. Caller takes ownership of returned Value. | 137 // parameters. Caller takes ownership of returned Value. |
159 base::Value* ParametersToValue() const; | 138 base::Value* ParametersToValue() const; |
160 | 139 |
161 private: | 140 private: |
162 const EntryData* const data_; | 141 const EntryData* const data_; |
163 | 142 |
164 // Log level when the event occurred. | 143 // Log capture mode when the event occurred. |
165 const LogLevel log_level_; | 144 const NetLogCaptureMode capture_mode_; |
166 | 145 |
167 // It is not safe to copy this class, since |parameters_callback_| may | 146 // It is not safe to copy this class, since |parameters_callback_| may |
168 // include pointers that become stale immediately after the event is added, | 147 // include pointers that become stale immediately after the event is added, |
169 // even if the code were modified to keep its own copy of the callback. | 148 // even if the code were modified to keep its own copy of the callback. |
170 DISALLOW_COPY_AND_ASSIGN(Entry); | 149 DISALLOW_COPY_AND_ASSIGN(Entry); |
171 }; | 150 }; |
172 | 151 |
173 // An observer, that must ensure its own thread safety, for events | 152 // An observer, that must ensure its own thread safety, for events |
174 // being added to a NetLog. | 153 // being added to a NetLog. |
175 class NET_EXPORT ThreadSafeObserver { | 154 class NET_EXPORT ThreadSafeObserver { |
176 public: | 155 public: |
177 // Constructs an observer that wants to see network events, with | 156 // Constructs an observer that wants to see network events, with |
178 // the specified minimum event granularity. A ThreadSafeObserver can only | 157 // the specified minimum event granularity. A ThreadSafeObserver can only |
179 // observe a single NetLog at a time. | 158 // observe a single NetLog at a time. |
180 // | 159 // |
181 // Observers will be called on the same thread an entry is added on, | 160 // Observers will be called on the same thread an entry is added on, |
182 // and are responsible for ensuring their own thread safety. | 161 // and are responsible for ensuring their own thread safety. |
183 // | 162 // |
184 // Observers must stop watching a NetLog before either the Observer or the | 163 // Observers must stop watching a NetLog before either the Observer or the |
185 // NetLog is destroyed. | 164 // NetLog is destroyed. |
186 ThreadSafeObserver(); | 165 ThreadSafeObserver(); |
187 | 166 |
188 // Returns the minimum log level for events this observer wants to | 167 // Returns the capture mode for events this observer wants to |
189 // receive. Must not be called when not watching a NetLog. | 168 // receive. Must not be called when not watching a NetLog. |
190 LogLevel log_level() const; | 169 NetLogCaptureMode capture_mode() const; |
191 | 170 |
192 // Returns the NetLog we are currently watching, if any. Returns NULL | 171 // Returns the NetLog we are currently watching, if any. Returns NULL |
193 // otherwise. | 172 // otherwise. |
194 NetLog* net_log() const; | 173 NetLog* net_log() const; |
195 | 174 |
196 // This method will be called on the thread that the event occurs on. It | 175 // This method will be called on the thread that the event occurs on. It |
197 // is the responsibility of the observer to handle it in a thread safe | 176 // is the responsibility of the observer to handle it in a thread safe |
198 // manner. | 177 // manner. |
199 // | 178 // |
200 // It is illegal for an Observer to call any NetLog or | 179 // It is illegal for an Observer to call any NetLog or |
201 // NetLog::Observer functions in response to a call to OnAddEntry. | 180 // NetLog::Observer functions in response to a call to OnAddEntry. |
202 virtual void OnAddEntry(const Entry& entry) = 0; | 181 virtual void OnAddEntry(const Entry& entry) = 0; |
203 | 182 |
204 protected: | 183 protected: |
205 virtual ~ThreadSafeObserver(); | 184 virtual ~ThreadSafeObserver(); |
206 | 185 |
207 private: | 186 private: |
208 friend class NetLog; | 187 friend class NetLog; |
209 | 188 |
210 void OnAddEntryData(const EntryData& entry_data); | 189 void OnAddEntryData(const EntryData& entry_data); |
211 | 190 |
212 // Both of these values are only modified by the NetLog. | 191 // Both of these values are only modified by the NetLog. |
213 LogLevel log_level_; | 192 NetLogCaptureMode capture_mode_; |
214 NetLog* net_log_; | 193 NetLog* net_log_; |
215 | 194 |
216 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); | 195 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); |
217 }; | 196 }; |
218 | 197 |
219 NetLog(); | 198 NetLog(); |
220 virtual ~NetLog(); | 199 virtual ~NetLog(); |
221 | 200 |
222 // Emits a global event to the log stream, with its own unique source ID. | 201 // Emits a global event to the log stream, with its own unique source ID. |
223 void AddGlobalEntry(EventType type); | 202 void AddGlobalEntry(EventType type); |
224 void AddGlobalEntry(EventType type, | 203 void AddGlobalEntry(EventType type, |
225 const NetLog::ParametersCallback& parameters_callback); | 204 const NetLog::ParametersCallback& parameters_callback); |
226 | 205 |
227 // Returns a unique ID which can be used as a source ID. All returned IDs | 206 // Returns a unique ID which can be used as a source ID. All returned IDs |
228 // will be unique and greater than 0. | 207 // will be unique and greater than 0. |
229 uint32 NextID(); | 208 uint32 NextID(); |
230 | 209 |
231 // Returns the logging level for this NetLog. This is used to avoid computing | 210 // Returns the capture mode for this NetLog. This is used to avoid computing |
232 // and saving expensive log entries. | 211 // and saving expensive log entries. |
233 LogLevel GetLogLevel() const; | 212 NetLogCaptureMode GetCaptureMode() const; |
234 | 213 |
235 // Adds an observer and sets its log level. The observer must not be | 214 // Adds an observer and sets its log capture mode. The observer must not be |
236 // watching any NetLog, including this one, when this is called. | 215 // watching any NetLog, including this one, when this is called. |
237 // | 216 // |
238 // NetLog implementations must call NetLog::OnAddObserver to update the | 217 // NetLog implementations must call NetLog::OnAddObserver to update the |
239 // observer's internal state. | 218 // observer's internal state. |
240 // | 219 // |
241 // DEPRECATED: The ability to watch the netlog stream is being phased out | 220 // DEPRECATED: The ability to watch the netlog stream is being phased out |
242 // (crbug.com/472693) as it can be misused in production code. Please consult | 221 // (crbug.com/472693) as it can be misused in production code. Please consult |
243 // with a net/log OWNER before introducing a new dependency on this. | 222 // with a net/log OWNER before introducing a new dependency on this. |
244 void DeprecatedAddObserver(ThreadSafeObserver* observer, LogLevel log_level); | 223 void DeprecatedAddObserver(ThreadSafeObserver* observer, |
224 NetLogCaptureMode capture_mode); | |
245 | 225 |
246 // Sets the log level of |observer| to |log_level|. |observer| must be | 226 // Sets the log capture mode of |observer| to |capture_mode|. |observer| must |
247 // watching |this|. NetLog implementations must call | 227 // be watching |this|. NetLog implementations must call |
248 // NetLog::OnSetObserverLogLevel to update the observer's internal state. | 228 // NetLog::OnSetObserverCaptureMode to update the observer's internal state. |
249 void SetObserverLogLevel(ThreadSafeObserver* observer, LogLevel log_level); | 229 void SetObserverCaptureMode(ThreadSafeObserver* observer, |
230 NetLogCaptureMode capture_mode); | |
250 | 231 |
251 // Removes an observer. NetLog implementations must call | 232 // Removes an observer. NetLog implementations must call |
252 // NetLog::OnAddObserver to update the observer's internal state. | 233 // NetLog::OnAddObserver to update the observer's internal state. |
253 // | 234 // |
254 // For thread safety reasons, it is recommended that this not be called in | 235 // For thread safety reasons, it is recommended that this not be called in |
255 // an object's destructor. | 236 // an object's destructor. |
256 // | 237 // |
257 // DEPRECATED: The ability to watch the netlog stream is being phased out | 238 // DEPRECATED: The ability to watch the netlog stream is being phased out |
258 // (crbug.com/472693) as it can be misused in production code. Please consult | 239 // (crbug.com/472693) as it can be misused in production code. Please consult |
259 // with a net/log OWNER before introducing a new dependency on this. | 240 // with a net/log OWNER before introducing a new dependency on this. |
(...skipping 13 matching lines...) Expand all Loading... | |
273 // Returns a C-String symbolic name for |source_type|. | 254 // Returns a C-String symbolic name for |source_type|. |
274 static const char* SourceTypeToString(SourceType source_type); | 255 static const char* SourceTypeToString(SourceType source_type); |
275 | 256 |
276 // Returns a dictionary that maps source type symbolic names to their enum | 257 // Returns a dictionary that maps source type symbolic names to their enum |
277 // values. Caller takes ownership of the returned Value. | 258 // values. Caller takes ownership of the returned Value. |
278 static base::Value* GetSourceTypesAsValue(); | 259 static base::Value* GetSourceTypesAsValue(); |
279 | 260 |
280 // Returns a C-String symbolic name for |event_phase|. | 261 // Returns a C-String symbolic name for |event_phase|. |
281 static const char* EventPhaseToString(EventPhase event_phase); | 262 static const char* EventPhaseToString(EventPhase event_phase); |
282 | 263 |
283 // Returns true if |log_level| indicates the actual bytes transferred should | |
284 // be logged. This is only the case when |log_level| is LOG_ALL. | |
285 static bool IsLoggingBytes(LogLevel log_level); | |
286 | |
287 // Returns true if |log_level| indicates that events should be logged. This is | |
288 // the case when |log_level| is anything other than LOG_NONE. | |
289 static bool IsLogging(LogLevel log_level); | |
290 | |
291 // Creates a ParametersCallback that encapsulates a single integer. | 264 // Creates a ParametersCallback that encapsulates a single integer. |
292 // Warning: |name| must remain valid for the life of the callback. | 265 // Warning: |name| must remain valid for the life of the callback. |
293 // TODO(mmenke): Rename this to be consistent with Int64Callback. | 266 // TODO(mmenke): Rename this to be consistent with Int64Callback. |
294 static ParametersCallback IntegerCallback(const char* name, int value); | 267 static ParametersCallback IntegerCallback(const char* name, int value); |
295 | 268 |
296 // Creates a ParametersCallback that encapsulates a single int64. The | 269 // Creates a ParametersCallback that encapsulates a single int64. The |
297 // callback will return the value as a StringValue, since IntegerValues | 270 // callback will return the value as a StringValue, since IntegerValues |
298 // only support 32-bit values. | 271 // only support 32-bit values. |
299 // Warning: |name| must remain valid for the life of the callback. | 272 // Warning: |name| must remain valid for the life of the callback. |
300 static ParametersCallback Int64Callback(const char* name, int64 value); | 273 static ParametersCallback Int64Callback(const char* name, int64 value); |
(...skipping 10 matching lines...) Expand all Loading... | |
311 const base::string16* value); | 284 const base::string16* value); |
312 | 285 |
313 private: | 286 private: |
314 friend class BoundNetLog; | 287 friend class BoundNetLog; |
315 | 288 |
316 void AddEntry(EventType type, | 289 void AddEntry(EventType type, |
317 const Source& source, | 290 const Source& source, |
318 EventPhase phase, | 291 EventPhase phase, |
319 const NetLog::ParametersCallback* parameters_callback); | 292 const NetLog::ParametersCallback* parameters_callback); |
320 | 293 |
321 // Called whenever an observer is added or removed, or has its log level | 294 // Called whenever an observer is added or removed, or has its log |
322 // changed. Must have acquired |lock_| prior to calling. | 295 // capture mode changed. Must have acquired |lock_| prior to calling. |
323 void UpdateLogLevel(); | 296 void UpdateCaptureMode(); |
324 | 297 |
325 // |lock_| protects access to |observers_|. | 298 // |lock_| protects access to |observers_|. |
326 base::Lock lock_; | 299 base::Lock lock_; |
327 | 300 |
328 // Last assigned source ID. Incremented to get the next one. | 301 // Last assigned source ID. Incremented to get the next one. |
329 base::subtle::Atomic32 last_id_; | 302 base::subtle::Atomic32 last_id_; |
330 | 303 |
331 // The current log level. | 304 // The current capture mode level. |
332 base::subtle::Atomic32 effective_log_level_; | 305 base::subtle::Atomic32 effective_capture_mode_level_; |
333 | 306 |
334 // |lock_| must be acquired whenever reading or writing to this. | 307 // |lock_| must be acquired whenever reading or writing to this. |
335 ObserverList<ThreadSafeObserver, true> observers_; | 308 ObserverList<ThreadSafeObserver, true> observers_; |
336 | 309 |
337 DISALLOW_COPY_AND_ASSIGN(NetLog); | 310 DISALLOW_COPY_AND_ASSIGN(NetLog); |
338 }; | 311 }; |
339 | 312 |
340 // Helper that binds a Source to a NetLog, and exposes convenience methods to | 313 // Helper that binds a Source to a NetLog, and exposes convenience methods to |
341 // output log messages without needing to pass in the source. | 314 // output log messages without needing to pass in the source. |
342 class NET_EXPORT BoundNetLog { | 315 class NET_EXPORT BoundNetLog { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
377 // |net_error| must not be ERR_IO_PENDING, as it's not a true error. | 350 // |net_error| must not be ERR_IO_PENDING, as it's not a true error. |
378 void EndEventWithNetErrorCode(NetLog::EventType event_type, | 351 void EndEventWithNetErrorCode(NetLog::EventType event_type, |
379 int net_error) const; | 352 int net_error) const; |
380 | 353 |
381 // Logs a byte transfer event to the NetLog. Determines whether to log the | 354 // Logs a byte transfer event to the NetLog. Determines whether to log the |
382 // received bytes or not based on the current logging level. | 355 // received bytes or not based on the current logging level. |
383 void AddByteTransferEvent(NetLog::EventType event_type, | 356 void AddByteTransferEvent(NetLog::EventType event_type, |
384 int byte_count, | 357 int byte_count, |
385 const char* bytes) const; | 358 const char* bytes) const; |
386 | 359 |
387 NetLog::LogLevel GetLogLevel() const; | 360 NetLogCaptureMode GetCaptureMode() const; |
388 | |
389 // Shortcut for NetLog::IsLoggingBytes(this->GetLogLevel()). | |
390 bool IsLoggingBytes() const; | |
391 | |
392 // Shortcut for NetLog::IsLogging(this->GetLogLevel()). | |
393 bool IsLogging() const; | |
394 | 361 |
395 // Helper to create a BoundNetLog given a NetLog and a SourceType. Takes care | 362 // Helper to create a BoundNetLog given a NetLog and a SourceType. Takes care |
396 // of creating a unique source ID, and handles the case of NULL net_log. | 363 // of creating a unique source ID, and handles the case of NULL net_log. |
397 static BoundNetLog Make(NetLog* net_log, NetLog::SourceType source_type); | 364 static BoundNetLog Make(NetLog* net_log, NetLog::SourceType source_type); |
398 | 365 |
399 const NetLog::Source& source() const { return source_; } | 366 const NetLog::Source& source() const { return source_; } |
400 NetLog* net_log() const { return net_log_; } | 367 NetLog* net_log() const { return net_log_; } |
401 | 368 |
402 private: | 369 private: |
403 // TODO(eroman): Temporary until crbug.com/467797 is solved. | 370 // TODO(eroman): Temporary until crbug.com/467797 is solved. |
(...skipping 11 matching lines...) Expand all Loading... | |
415 NetLog::Source source_; | 382 NetLog::Source source_; |
416 NetLog* net_log_; | 383 NetLog* net_log_; |
417 | 384 |
418 // TODO(eroman): Temporary until crbug.com/467797 is solved. | 385 // TODO(eroman): Temporary until crbug.com/467797 is solved. |
419 Liveness liveness_ = ALIVE; | 386 Liveness liveness_ = ALIVE; |
420 }; | 387 }; |
421 | 388 |
422 } // namespace net | 389 } // namespace net |
423 | 390 |
424 #endif // NET_LOG_NET_LOG_H_ | 391 #endif // NET_LOG_NET_LOG_H_ |
OLD | NEW |