Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: net/log/net_log.h

Issue 2315613002: Extracted NetLog class's inner enum types into their own enum classes and (Closed)
Patch Set: Ran "git cl format" on code. Much formatting ensued. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/log/bounded_file_net_log_observer_unittest.cc ('k') | net/log/net_log.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <string> 11 #include <string>
12 12
13 #include "base/atomicops.h" 13 #include "base/atomicops.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/macros.h" 16 #include "base/macros.h"
17 #include "base/observer_list.h" 17 #include "base/observer_list.h"
18 #include "base/strings/string16.h" 18 #include "base/strings/string16.h"
19 #include "base/synchronization/lock.h" 19 #include "base/synchronization/lock.h"
20 #include "base/time/time.h" 20 #include "base/time/time.h"
21 #include "build/build_config.h" 21 #include "build/build_config.h"
22 #include "net/base/net_export.h" 22 #include "net/base/net_export.h"
23 #include "net/log/net_log_capture_mode.h" 23 #include "net/log/net_log_capture_mode.h"
24 #include "net/log/net_log_event_type.h"
25 #include "net/log/net_log_source_type.h"
24 26
25 namespace base { 27 namespace base {
26 class DictionaryValue; 28 class DictionaryValue;
27 class Value; 29 class Value;
28 } 30 }
29 31
30 namespace net { 32 namespace net {
31 33
32 // NetLog is the destination for log messages generated by the network stack. 34 // NetLog is the destination for log messages generated by the network stack.
33 // Each log message has a "source" field which identifies the specific entity 35 // Each log message has a "source" field which identifies the specific entity
34 // that generated the message (for example, which URLRequest or which 36 // that generated the message (for example, which URLRequest or which
35 // SpdySession). 37 // SpdySession).
36 // 38 //
37 // To avoid needing to pass in the "source ID" to the logging functions, NetLog 39 // To avoid needing to pass in the "source ID" to the logging functions, NetLog
38 // is usually accessed through a BoundNetLog, which will always pass in a 40 // is usually accessed through a BoundNetLog, which will always pass in a
39 // specific source ID. 41 // specific source ID.
40 // 42 //
41 // All methods are thread safe, with the exception that no NetLog or 43 // All methods are thread safe, with the exception that no NetLog or
42 // NetLog::ThreadSafeObserver functions may be called by an observer's 44 // NetLog::ThreadSafeObserver functions may be called by an observer's
43 // OnAddEntry() method. Doing so will result in a deadlock. 45 // OnAddEntry() method. Doing so will result in a deadlock.
44 // 46 //
45 // For a broader introduction see the design document: 47 // For a broader introduction see the design document:
46 // https://sites.google.com/a/chromium.org/dev/developers/design-documents/netwo rk-stack/netlog 48 // https://sites.google.com/a/chromium.org/dev/developers/design-documents/netwo rk-stack/netlog
47 class NET_EXPORT NetLog { 49 class NET_EXPORT NetLog {
48 public: 50 public:
49 enum EventType {
50 #define EVENT_TYPE(label) TYPE_##label,
51 #include "net/log/net_log_event_type_list.h"
52 #undef EVENT_TYPE
53 EVENT_COUNT
54 };
55
56 // The 'phase' of an event trace (whether it marks the beginning or end
57 // of an event.).
58 enum EventPhase {
59 PHASE_NONE,
60 PHASE_BEGIN,
61 PHASE_END,
62 };
63
64 // The "source" identifies the entity that generated the log message.
65 enum SourceType {
66 #define SOURCE_TYPE(label) SOURCE_##label,
67 #include "net/log/net_log_source_type_list.h"
68 #undef SOURCE_TYPE
69 SOURCE_COUNT
70 };
71 51
72 // A callback that returns a Value representation of the parameters 52 // A callback that returns a Value representation of the parameters
73 // associated with an event. If called, it will be called synchronously, 53 // associated with an event. If called, it will be called synchronously,
74 // so it need not have owning references. May be called more than once, or 54 // so it need not have owning references. May be called more than once, or
75 // not at all. May return NULL. 55 // not at all. May return NULL.
76 typedef base::Callback<std::unique_ptr<base::Value>(NetLogCaptureMode)> 56 typedef base::Callback<std::unique_ptr<base::Value>(NetLogCaptureMode)>
77 ParametersCallback; 57 ParametersCallback;
78 58
79 // Identifies the entity that generated this log. The |id| field should 59 // Identifies the entity that generated this log. The |id| field should
80 // uniquely identify the source, and is used by log observers to infer 60 // uniquely identify the source, and is used by log observers to infer
81 // message groupings. Can use NetLog::NextID() to create unique IDs. 61 // message groupings. Can use NetLog::NextID() to create unique IDs.
82 struct NET_EXPORT Source { 62 struct NET_EXPORT Source {
83 static const uint32_t kInvalidId; 63 static const uint32_t kInvalidId;
84 64
85 Source(); 65 Source();
86 Source(SourceType type, uint32_t id); 66 Source(NetLogSourceType type, uint32_t id);
87 bool IsValid() const; 67 bool IsValid() const;
88 68
89 // Adds the source to a DictionaryValue containing event parameters, 69 // Adds the source to a DictionaryValue containing event parameters,
90 // using the name "source_dependency". 70 // using the name "source_dependency".
91 void AddToEventParameters(base::DictionaryValue* event_params) const; 71 void AddToEventParameters(base::DictionaryValue* event_params) const;
92 72
93 // Returns a callback that returns a dictionary with a single entry 73 // Returns a callback that returns a dictionary with a single entry
94 // named "source_dependency" that describes |this|. 74 // named "source_dependency" that describes |this|.
95 ParametersCallback ToEventParametersCallback() const; 75 ParametersCallback ToEventParametersCallback() const;
96 76
97 // Attempts to extract a Source from a set of event parameters. Returns 77 // Attempts to extract a Source from a set of event parameters. Returns
98 // true and writes the result to |source| on success. Returns false and 78 // true and writes the result to |source| on success. Returns false and
99 // makes |source| an invalid source on failure. 79 // makes |source| an invalid source on failure.
100 // TODO(mmenke): Long term, we want to remove this. 80 // TODO(mmenke): Long term, we want to remove this.
101 static bool FromEventParameters(base::Value* event_params, Source* source); 81 static bool FromEventParameters(base::Value* event_params, Source* source);
102 82
103 SourceType type; 83 NetLogSourceType type;
104 uint32_t id; 84 uint32_t id;
105 }; 85 };
106 86
107 struct NET_EXPORT EntryData { 87 struct NET_EXPORT EntryData {
108 EntryData(EventType type, 88 EntryData(NetLogEventType type,
109 Source source, 89 Source source,
110 EventPhase phase, 90 NetLogEventPhase phase,
111 base::TimeTicks time, 91 base::TimeTicks time,
112 const ParametersCallback* parameters_callback); 92 const ParametersCallback* parameters_callback);
113 ~EntryData(); 93 ~EntryData();
114 94
115 const EventType type; 95 const NetLogEventType type;
116 const Source source; 96 const Source source;
117 const EventPhase phase; 97 const NetLogEventPhase phase;
118 const base::TimeTicks time; 98 const base::TimeTicks time;
119 const ParametersCallback* const parameters_callback; 99 const ParametersCallback* const parameters_callback;
120 }; 100 };
121 101
122 // An Entry pre-binds EntryData to a capture mode, so observers will observe 102 // An Entry pre-binds EntryData to a capture mode, so observers will observe
123 // the output of ToValue() and ParametersToValue() at their log capture mode 103 // the output of ToValue() and ParametersToValue() at their log capture mode
124 // rather than the current maximum. 104 // rather than the current maximum.
125 class NET_EXPORT Entry { 105 class NET_EXPORT Entry {
126 public: 106 public:
127 Entry(const EntryData* data, NetLogCaptureMode capture_mode); 107 Entry(const EntryData* data, NetLogCaptureMode capture_mode);
128 ~Entry(); 108 ~Entry();
129 109
130 EventType type() const { return data_->type; } 110 NetLogEventType type() const { return data_->type; }
131 Source source() const { return data_->source; } 111 Source source() const { return data_->source; }
132 EventPhase phase() const { return data_->phase; } 112 NetLogEventPhase phase() const { return data_->phase; }
133 113
134 // Serializes the specified event to a Value. The Value also includes the 114 // Serializes the specified event to a Value. The Value also includes the
135 // current time. Takes in a time to allow back-dating entries. 115 // current time. Takes in a time to allow back-dating entries.
136 std::unique_ptr<base::Value> ToValue() const; 116 std::unique_ptr<base::Value> ToValue() const;
137 117
138 // Returns the parameters as a Value. Returns NULL if there are no 118 // Returns the parameters as a Value. Returns NULL if there are no
139 // parameters. 119 // parameters.
140 std::unique_ptr<base::Value> ParametersToValue() const; 120 std::unique_ptr<base::Value> ParametersToValue() const;
141 121
142 private: 122 private:
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 NetLogCaptureMode capture_mode_; 183 NetLogCaptureMode capture_mode_;
204 NetLog* net_log_; 184 NetLog* net_log_;
205 185
206 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); 186 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver);
207 }; 187 };
208 188
209 NetLog(); 189 NetLog();
210 virtual ~NetLog(); 190 virtual ~NetLog();
211 191
212 // Emits a global event to the log stream, with its own unique source ID. 192 // Emits a global event to the log stream, with its own unique source ID.
213 void AddGlobalEntry(EventType type); 193 void AddGlobalEntry(NetLogEventType type);
214 void AddGlobalEntry(EventType type, 194 void AddGlobalEntry(NetLogEventType type,
215 const NetLog::ParametersCallback& parameters_callback); 195 const NetLog::ParametersCallback& parameters_callback);
216 196
217 // Returns a unique ID which can be used as a source ID. All returned IDs 197 // Returns a unique ID which can be used as a source ID. All returned IDs
218 // will be unique and greater than 0. 198 // will be unique and greater than 0.
219 uint32_t NextID(); 199 uint32_t NextID();
220 200
221 // Returns true if there are any observers attached to the NetLog. This can be 201 // Returns true if there are any observers attached to the NetLog. This can be
222 // used as an optimization to avoid emitting log entries when there is no 202 // used as an optimization to avoid emitting log entries when there is no
223 // chance that the data will be consumed. 203 // chance that the data will be consumed.
224 bool IsCapturing() const; 204 bool IsCapturing() const;
(...skipping 21 matching lines...) Expand all
246 // DEPRECATED: The ability to watch the netlog stream is being phased out 226 // DEPRECATED: The ability to watch the netlog stream is being phased out
247 // (crbug.com/472693) as it can be misused in production code. Please consult 227 // (crbug.com/472693) as it can be misused in production code. Please consult
248 // with a net/log OWNER before introducing a new dependency on this. 228 // with a net/log OWNER before introducing a new dependency on this.
249 void DeprecatedRemoveObserver(ThreadSafeObserver* observer); 229 void DeprecatedRemoveObserver(ThreadSafeObserver* observer);
250 230
251 // Converts a time to the string format that the NetLog uses to represent 231 // Converts a time to the string format that the NetLog uses to represent
252 // times. Strings are used since integers may overflow. 232 // times. Strings are used since integers may overflow.
253 static std::string TickCountToString(const base::TimeTicks& time); 233 static std::string TickCountToString(const base::TimeTicks& time);
254 234
255 // Returns a C-String symbolic name for |event_type|. 235 // Returns a C-String symbolic name for |event_type|.
256 static const char* EventTypeToString(EventType event_type); 236 static const char* EventTypeToString(NetLogEventType event_type);
257 237
258 // Returns a dictionary that maps event type symbolic names to their enum 238 // Returns a dictionary that maps event type symbolic names to their enum
259 // values. Caller takes ownership of the returned Value. 239 // values. Caller takes ownership of the returned Value.
260 static base::Value* GetEventTypesAsValue(); 240 static base::Value* GetEventTypesAsValue();
261 241
262 // Returns a C-String symbolic name for |source_type|. 242 // Returns a C-String symbolic name for |source_type|.
263 static const char* SourceTypeToString(SourceType source_type); 243 static const char* SourceTypeToString(NetLogSourceType source_type);
264 244
265 // Returns a dictionary that maps source type symbolic names to their enum 245 // Returns a dictionary that maps source type symbolic names to their enum
266 // values. Caller takes ownership of the returned Value. 246 // values. Caller takes ownership of the returned Value.
267 static base::Value* GetSourceTypesAsValue(); 247 static base::Value* GetSourceTypesAsValue();
268 248
269 // Returns a C-String symbolic name for |event_phase|. 249 // Returns a C-String symbolic name for |event_phase|.
270 static const char* EventPhaseToString(EventPhase event_phase); 250 static const char* EventPhaseToString(NetLogEventPhase event_phase);
271 251
272 // Creates a ParametersCallback that encapsulates a single bool. 252 // Creates a ParametersCallback that encapsulates a single bool.
273 // Warning: |name| must remain valid for the life of the callback. 253 // Warning: |name| must remain valid for the life of the callback.
274 static ParametersCallback BoolCallback(const char* name, bool value); 254 static ParametersCallback BoolCallback(const char* name, bool value);
275 255
276 // Warning: |name| must remain valid for the life of the callback. 256 // Warning: |name| must remain valid for the life of the callback.
277 static ParametersCallback IntCallback(const char* name, int value); 257 static ParametersCallback IntCallback(const char* name, int value);
278 258
279 // Creates a ParametersCallback that encapsulates a single int64_t. The 259 // Creates a ParametersCallback that encapsulates a single int64_t. The
280 // callback will return the value as a StringValue, since IntegerValues 260 // callback will return the value as a StringValue, since IntegerValues
281 // only support 32-bit values. 261 // only support 32-bit values.
282 // Warning: |name| must remain valid for the life of the callback. 262 // Warning: |name| must remain valid for the life of the callback.
283 static ParametersCallback Int64Callback(const char* name, int64_t value); 263 static ParametersCallback Int64Callback(const char* name, int64_t value);
284 264
285 // Creates a ParametersCallback that encapsulates a single UTF8 string. Takes 265 // Creates a ParametersCallback that encapsulates a single UTF8 string. Takes
286 // |value| as a pointer to avoid copying, and emphasize it must be valid for 266 // |value| as a pointer to avoid copying, and emphasize it must be valid for
287 // the life of the callback. |value| may not be NULL. 267 // the life of the callback. |value| may not be NULL.
288 // Warning: |name| and |value| must remain valid for the life of the callback. 268 // Warning: |name| and |value| must remain valid for the life of the callback.
289 static ParametersCallback StringCallback(const char* name, 269 static ParametersCallback StringCallback(const char* name,
290 const std::string* value); 270 const std::string* value);
291 static ParametersCallback StringCallback(const char* name, const char* value); 271 static ParametersCallback StringCallback(const char* name, const char* value);
292 272
293 // Same as above, but takes in a UTF16 string. 273 // Same as above, but takes in a UTF16 string.
294 static ParametersCallback StringCallback(const char* name, 274 static ParametersCallback StringCallback(const char* name,
295 const base::string16* value); 275 const base::string16* value);
296 276
297 private: 277 private:
298 friend class BoundNetLog; 278 friend class BoundNetLog;
299 279
300 void AddEntry(EventType type, 280 void AddEntry(NetLogEventType type,
301 const Source& source, 281 const Source& source,
302 EventPhase phase, 282 NetLogEventPhase phase,
303 const NetLog::ParametersCallback* parameters_callback); 283 const NetLog::ParametersCallback* parameters_callback);
304 284
305 // Called whenever an observer is added or removed, to update 285 // Called whenever an observer is added or removed, to update
306 // |has_observers_|. Must have acquired |lock_| prior to calling. 286 // |has_observers_|. Must have acquired |lock_| prior to calling.
307 void UpdateIsCapturing(); 287 void UpdateIsCapturing();
308 288
309 // |lock_| protects access to |observers_|. 289 // |lock_| protects access to |observers_|.
310 base::Lock lock_; 290 base::Lock lock_;
311 291
312 // Last assigned source ID. Incremented to get the next one. 292 // Last assigned source ID. Incremented to get the next one.
(...skipping 11 matching lines...) Expand all
324 }; 304 };
325 305
326 // Helper that binds a Source to a NetLog, and exposes convenience methods to 306 // Helper that binds a Source to a NetLog, and exposes convenience methods to
327 // output log messages without needing to pass in the source. 307 // output log messages without needing to pass in the source.
328 class NET_EXPORT BoundNetLog { 308 class NET_EXPORT BoundNetLog {
329 public: 309 public:
330 BoundNetLog() : net_log_(NULL) {} 310 BoundNetLog() : net_log_(NULL) {}
331 ~BoundNetLog(); 311 ~BoundNetLog();
332 312
333 // Add a log entry to the NetLog for the bound source. 313 // Add a log entry to the NetLog for the bound source.
334 void AddEntry(NetLog::EventType type, NetLog::EventPhase phase) const; 314 void AddEntry(NetLogEventType type, NetLogEventPhase phase) const;
335 void AddEntry(NetLog::EventType type, 315 void AddEntry(NetLogEventType type,
336 NetLog::EventPhase phase, 316 NetLogEventPhase phase,
337 const NetLog::ParametersCallback& get_parameters) const; 317 const NetLog::ParametersCallback& get_parameters) const;
338 318
339 // Convenience methods that call AddEntry with a fixed "capture phase" 319 // Convenience methods that call AddEntry with a fixed "capture phase"
340 // (begin, end, or none). 320 // (begin, end, or none).
341 void BeginEvent(NetLog::EventType type) const; 321 void BeginEvent(NetLogEventType type) const;
342 void BeginEvent(NetLog::EventType type, 322 void BeginEvent(NetLogEventType type,
343 const NetLog::ParametersCallback& get_parameters) const; 323 const NetLog::ParametersCallback& get_parameters) const;
344 324
345 void EndEvent(NetLog::EventType type) const; 325 void EndEvent(NetLogEventType type) const;
346 void EndEvent(NetLog::EventType type, 326 void EndEvent(NetLogEventType type,
347 const NetLog::ParametersCallback& get_parameters) const; 327 const NetLog::ParametersCallback& get_parameters) const;
348 328
349 void AddEvent(NetLog::EventType type) const; 329 void AddEvent(NetLogEventType type) const;
350 void AddEvent(NetLog::EventType type, 330 void AddEvent(NetLogEventType type,
351 const NetLog::ParametersCallback& get_parameters) const; 331 const NetLog::ParametersCallback& get_parameters) const;
352 332
353 // Just like AddEvent, except |net_error| is a net error code. A parameter 333 // Just like AddEvent, except |net_error| is a net error code. A parameter
354 // called "net_error" with the indicated value will be recorded for the event. 334 // called "net_error" with the indicated value will be recorded for the event.
355 // |net_error| must be negative, and not ERR_IO_PENDING, as it's not a true 335 // |net_error| must be negative, and not ERR_IO_PENDING, as it's not a true
356 // error. 336 // error.
357 void AddEventWithNetErrorCode(NetLog::EventType event_type, 337 void AddEventWithNetErrorCode(NetLogEventType event_type,
358 int net_error) const; 338 int net_error) const;
359 339
360 // Just like EndEvent, except |net_error| is a net error code. If it's 340 // Just like EndEvent, except |net_error| is a net error code. If it's
361 // negative, a parameter called "net_error" with a value of |net_error| is 341 // negative, a parameter called "net_error" with a value of |net_error| is
362 // associated with the event. Otherwise, the end event has no parameters. 342 // associated with the event. Otherwise, the end event has no parameters.
363 // |net_error| must not be ERR_IO_PENDING, as it's not a true error. 343 // |net_error| must not be ERR_IO_PENDING, as it's not a true error.
364 void EndEventWithNetErrorCode(NetLog::EventType event_type, 344 void EndEventWithNetErrorCode(NetLogEventType event_type,
365 int net_error) const; 345 int net_error) const;
366 346
367 // Logs a byte transfer event to the NetLog. Determines whether to log the 347 // Logs a byte transfer event to the NetLog. Determines whether to log the
368 // received bytes or not based on the current logging level. 348 // received bytes or not based on the current logging level.
369 void AddByteTransferEvent(NetLog::EventType event_type, 349 void AddByteTransferEvent(NetLogEventType event_type,
370 int byte_count, 350 int byte_count,
371 const char* bytes) const; 351 const char* bytes) const;
372 352
373 bool IsCapturing() const; 353 bool IsCapturing() const;
374 354
375 // Helper to create a BoundNetLog given a NetLog and a SourceType. Takes care 355 // Helper to create a BoundNetLog given a NetLog and a NetLogSourceType.
376 // of creating a unique source ID, and handles the case of NULL net_log. 356 // Takes care of creating a unique source ID, and handles
377 static BoundNetLog Make(NetLog* net_log, NetLog::SourceType source_type); 357 // the case of NULL net_log.
358 static BoundNetLog Make(NetLog* net_log, NetLogSourceType source_type);
378 359
379 const NetLog::Source& source() const { return source_; } 360 const NetLog::Source& source() const { return source_; }
380 NetLog* net_log() const { return net_log_; } 361 NetLog* net_log() const { return net_log_; }
381 362
382 private: 363 private:
383 // TODO(eroman): Temporary until crbug.com/467797 is solved. 364 // TODO(eroman): Temporary until crbug.com/467797 is solved.
384 enum Liveness { 365 enum Liveness {
385 ALIVE = 0xCA11AB13, 366 ALIVE = 0xCA11AB13,
386 DEAD = 0xDEADBEEF, 367 DEAD = 0xDEADBEEF,
387 }; 368 };
388 369
389 BoundNetLog(const NetLog::Source& source, NetLog* net_log) 370 BoundNetLog(const NetLog::Source& source, NetLog* net_log)
390 : source_(source), net_log_(net_log) {} 371 : source_(source), net_log_(net_log) {}
391 372
392 // TODO(eroman): Temporary until crbug.com/467797 is solved. 373 // TODO(eroman): Temporary until crbug.com/467797 is solved.
393 void CrashIfInvalid() const; 374 void CrashIfInvalid() const;
394 375
395 NetLog::Source source_; 376 NetLog::Source source_;
396 NetLog* net_log_; 377 NetLog* net_log_;
397 378
398 // TODO(eroman): Temporary until crbug.com/467797 is solved. 379 // TODO(eroman): Temporary until crbug.com/467797 is solved.
399 Liveness liveness_ = ALIVE; 380 Liveness liveness_ = ALIVE;
400 }; 381 };
401 382
402 } // namespace net 383 } // namespace net
403 384
404 #endif // NET_LOG_NET_LOG_H_ 385 #endif // NET_LOG_NET_LOG_H_
OLDNEW
« no previous file with comments | « net/log/bounded_file_net_log_observer_unittest.cc ('k') | net/log/net_log.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698