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

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

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

Powered by Google App Engine
This is Rietveld 408576698