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_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 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class TimeTicks; | 17 class TimeTicks; |
18 class Value; | 18 class Value; |
19 } | 19 } |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 | 22 |
23 // NetLog is the destination for log messages generated by the network stack. | 23 // 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 | 24 // Each log message has a "source" field which identifies the specific entity |
25 // that generated the message (for example, which URLRequest or which | 25 // that generated the message (for example, which URLRequest or which |
26 // SocketStream). | 26 // SocketStream). |
27 // | 27 // |
28 // To avoid needing to pass in the "source id" to the logging functions, NetLog | 28 // 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 | 29 // is usually accessed through a BoundNetLog, which will always pass in a |
30 // specific source ID. | 30 // specific source ID. |
31 // | 31 // |
32 class NET_EXPORT NetLog { | 32 class NET_EXPORT NetLog { |
33 public: | 33 public: |
34 enum EventType { | 34 enum EventType { |
35 #define EVENT_TYPE(label) TYPE_ ## label, | 35 #define EVENT_TYPE(label) TYPE_ ## label, |
36 #include "net/base/net_log_event_type_list.h" | 36 #include "net/base/net_log_event_type_list.h" |
37 #undef EVENT_TYPE | 37 #undef EVENT_TYPE |
38 EVENT_COUNT | 38 EVENT_COUNT |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // Returns the NetLog we are currently watching, if any. Returns NULL | 126 // Returns the NetLog we are currently watching, if any. Returns NULL |
127 // otherwise. | 127 // otherwise. |
128 NetLog* net_log() const; | 128 NetLog* net_log() const; |
129 | 129 |
130 // This method will be called on the thread that the event occurs on. It | 130 // This method will be called on the thread that the event occurs on. It |
131 // is the responsibility of the observer to handle it in a thread safe | 131 // is the responsibility of the observer to handle it in a thread safe |
132 // manner. | 132 // manner. |
133 // | 133 // |
134 // It is illegal for an Observer to call any NetLog or | 134 // It is illegal for an Observer to call any NetLog or |
135 // NetLog::Observer functions in response to a call to OnAddEntry. | 135 // NetLog::Observer functions in response to a call to OnAddEntry. |
| 136 // |
| 137 // |type| - The type of the event. |
| 138 // |time| - The time when the event occurred. |
| 139 // |source| - The source that generated the event. |
| 140 // |phase| - An optional parameter indicating whether this is the start/end |
| 141 // of an action. |
| 142 // |params| - Optional (may be NULL) parameters for this event. |
| 143 // The specific subclass of EventParameters is defined |
| 144 // by the contract for events of this |type|. |
| 145 // TODO(eroman): Take a scoped_refptr<EventParameters> instead. |
136 virtual void OnAddEntry(EventType type, | 146 virtual void OnAddEntry(EventType type, |
137 const base::TimeTicks& time, | 147 const base::TimeTicks& time, |
138 const Source& source, | 148 const Source& source, |
139 EventPhase phase, | 149 EventPhase phase, |
140 EventParameters* params) = 0; | 150 EventParameters* params) = 0; |
141 | 151 |
142 private: | 152 private: |
143 friend class NetLog; | 153 friend class NetLog; |
144 | 154 |
145 // Both of these values are only modified by the NetLog. | 155 // Both of these values are only modified by the NetLog. |
146 LogLevel log_level_; | 156 LogLevel log_level_; |
147 NetLog* net_log_; | 157 NetLog* net_log_; |
148 | 158 |
149 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); | 159 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); |
150 }; | 160 }; |
151 | 161 |
152 NetLog() {} | 162 NetLog() {} |
153 virtual ~NetLog() {} | 163 virtual ~NetLog() {} |
154 | 164 |
155 // Emits an event to the log stream. | 165 // Emits a global event to the log stream, with its own unique source ID. |
156 // |type| - The type of the event. | 166 void AddGlobalEntry(EventType type, |
157 // |time| - The time when the event occurred. | 167 const scoped_refptr<EventParameters>& params); |
158 // |source| - The source that generated the event. | |
159 // |phase| - An optional parameter indicating whether this is the start/end | |
160 // of an action. | |
161 // |params| - Optional (may be NULL) parameters for this event. | |
162 // The specific subclass of EventParameters is defined | |
163 // by the contract for events of this |type|. | |
164 // TODO(eroman): Take a scoped_refptr<> instead. | |
165 virtual void AddEntry(EventType type, | |
166 const base::TimeTicks& time, | |
167 const Source& source, | |
168 EventPhase phase, | |
169 EventParameters* params) = 0; | |
170 | 168 |
171 // Returns a unique ID which can be used as a source ID. | 169 // Returns a unique ID which can be used as a source ID. |
172 virtual uint32 NextID() = 0; | 170 virtual uint32 NextID() = 0; |
173 | 171 |
174 // Returns the logging level for this NetLog. This is used to avoid computing | 172 // Returns the logging level for this NetLog. This is used to avoid computing |
175 // and saving expensive log entries. | 173 // and saving expensive log entries. |
176 virtual LogLevel GetLogLevel() const = 0; | 174 virtual LogLevel GetLogLevel() const = 0; |
177 | 175 |
178 // Adds an observer and sets its log level. The observer must not be | 176 // Adds an observer and sets its log level. The observer must not be |
179 // watching any NetLog, including this one, when this is called. | 177 // watching any NetLog, including this one, when this is called. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // Serializes the specified event to a DictionaryValue. | 223 // Serializes the specified event to a DictionaryValue. |
226 // If |use_strings| is true, uses strings rather than numeric ids. | 224 // If |use_strings| is true, uses strings rather than numeric ids. |
227 static base::Value* EntryToDictionaryValue(NetLog::EventType type, | 225 static base::Value* EntryToDictionaryValue(NetLog::EventType type, |
228 const base::TimeTicks& time, | 226 const base::TimeTicks& time, |
229 const NetLog::Source& source, | 227 const NetLog::Source& source, |
230 NetLog::EventPhase phase, | 228 NetLog::EventPhase phase, |
231 NetLog::EventParameters* params, | 229 NetLog::EventParameters* params, |
232 bool use_strings); | 230 bool use_strings); |
233 | 231 |
234 protected: | 232 protected: |
| 233 // This is the internal function used by AddGlobalEntry and BoundNetLogs. |
| 234 virtual void AddEntry( |
| 235 EventType type, |
| 236 const Source& source, |
| 237 EventPhase phase, |
| 238 const scoped_refptr<NetLog::EventParameters>& params) = 0; |
| 239 |
235 // Subclasses must call these in the corresponding functions to set an | 240 // Subclasses must call these in the corresponding functions to set an |
236 // observer's |net_log_| and |log_level_| values. | 241 // observer's |net_log_| and |log_level_| values. |
237 void OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level); | 242 void OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level); |
238 void OnSetObserverLogLevel(ThreadSafeObserver* observer, | 243 void OnSetObserverLogLevel(ThreadSafeObserver* observer, |
239 LogLevel log_level); | 244 LogLevel log_level); |
240 void OnRemoveObserver(ThreadSafeObserver* observer); | 245 void OnRemoveObserver(ThreadSafeObserver* observer); |
241 | 246 |
242 private: | 247 private: |
| 248 friend class BoundNetLog; |
| 249 |
243 DISALLOW_COPY_AND_ASSIGN(NetLog); | 250 DISALLOW_COPY_AND_ASSIGN(NetLog); |
244 }; | 251 }; |
245 | 252 |
246 // Helper that binds a Source to a NetLog, and exposes convenience methods to | 253 // Helper that binds a Source to a NetLog, and exposes convenience methods to |
247 // output log messages without needing to pass in the source. | 254 // output log messages without needing to pass in the source. |
248 class NET_EXPORT BoundNetLog { | 255 class NET_EXPORT BoundNetLog { |
249 public: | 256 public: |
250 BoundNetLog() : net_log_(NULL) {} | 257 BoundNetLog() : net_log_(NULL) {} |
251 | 258 |
252 BoundNetLog(const NetLog::Source& source, NetLog* net_log) | |
253 : source_(source), net_log_(net_log) { | |
254 } | |
255 | |
256 // Convenience methods that call through to the NetLog, passing in the | 259 // Convenience methods that call through to the NetLog, passing in the |
257 // currently bound source. | 260 // currently bound source. |
258 void AddEntry(NetLog::EventType type, | 261 void AddEntry(NetLog::EventType type, |
259 NetLog::EventPhase phase, | 262 NetLog::EventPhase phase, |
260 const scoped_refptr<NetLog::EventParameters>& params) const; | 263 const scoped_refptr<NetLog::EventParameters>& params) const; |
261 | 264 |
262 void AddEntryWithTime( | |
263 NetLog::EventType type, | |
264 const base::TimeTicks& time, | |
265 NetLog::EventPhase phase, | |
266 const scoped_refptr<NetLog::EventParameters>& params) const; | |
267 | |
268 // Convenience methods that call through to the NetLog, passing in the | 265 // Convenience methods that call through to the NetLog, passing in the |
269 // currently bound source, current time, and a fixed "capture phase" | 266 // currently bound source, current time, and a fixed "capture phase" |
270 // (begin, end, or none). | 267 // (begin, end, or none). |
271 void AddEvent(NetLog::EventType event_type, | 268 void AddEvent(NetLog::EventType event_type, |
272 const scoped_refptr<NetLog::EventParameters>& params) const; | 269 const scoped_refptr<NetLog::EventParameters>& params) const; |
273 void BeginEvent(NetLog::EventType event_type, | 270 void BeginEvent(NetLog::EventType event_type, |
274 const scoped_refptr<NetLog::EventParameters>& params) const; | 271 const scoped_refptr<NetLog::EventParameters>& params) const; |
275 void EndEvent(NetLog::EventType event_type, | 272 void EndEvent(NetLog::EventType event_type, |
276 const scoped_refptr<NetLog::EventParameters>& params) const; | 273 const scoped_refptr<NetLog::EventParameters>& params) const; |
277 | 274 |
(...skipping 25 matching lines...) Expand all Loading... |
303 bool IsLoggingAllEvents() const; | 300 bool IsLoggingAllEvents() const; |
304 | 301 |
305 // Helper to create a BoundNetLog given a NetLog and a SourceType. Takes care | 302 // Helper to create a BoundNetLog given a NetLog and a SourceType. Takes care |
306 // of creating a unique source ID, and handles the case of NULL net_log. | 303 // of creating a unique source ID, and handles the case of NULL net_log. |
307 static BoundNetLog Make(NetLog* net_log, NetLog::SourceType source_type); | 304 static BoundNetLog Make(NetLog* net_log, NetLog::SourceType source_type); |
308 | 305 |
309 const NetLog::Source& source() const { return source_; } | 306 const NetLog::Source& source() const { return source_; } |
310 NetLog* net_log() const { return net_log_; } | 307 NetLog* net_log() const { return net_log_; } |
311 | 308 |
312 private: | 309 private: |
| 310 BoundNetLog(const NetLog::Source& source, NetLog* net_log) |
| 311 : source_(source), net_log_(net_log) { |
| 312 } |
| 313 |
313 NetLog::Source source_; | 314 NetLog::Source source_; |
314 NetLog* net_log_; | 315 NetLog* net_log_; |
315 }; | 316 }; |
316 | 317 |
317 // NetLogStringParameter is a subclass of EventParameters that encapsulates a | 318 // NetLogStringParameter is a subclass of EventParameters that encapsulates a |
318 // single std::string parameter. | 319 // single std::string parameter. |
319 class NET_EXPORT NetLogStringParameter : public NetLog::EventParameters { | 320 class NET_EXPORT NetLogStringParameter : public NetLog::EventParameters { |
320 public: | 321 public: |
321 // |name| must be a string literal. | 322 // |name| must be a string literal. |
322 NetLogStringParameter(const char* name, const std::string& value); | 323 NetLogStringParameter(const char* name, const std::string& value); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 | 392 |
392 private: | 393 private: |
393 BoundNetLog net_log_; | 394 BoundNetLog net_log_; |
394 const NetLog::EventType event_type_; | 395 const NetLog::EventType event_type_; |
395 scoped_refptr<NetLog::EventParameters> end_event_params_; | 396 scoped_refptr<NetLog::EventParameters> end_event_params_; |
396 }; | 397 }; |
397 | 398 |
398 } // namespace net | 399 } // namespace net |
399 | 400 |
400 #endif // NET_BASE_NET_LOG_H_ | 401 #endif // NET_BASE_NET_LOG_H_ |
OLD | NEW |