Chromium Code Reviews| 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/callback_forward.h" | |
| 12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 14 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
| 15 | 17 |
| 16 namespace base { | 18 namespace base { |
| 19 class DictionaryValue; | |
| 17 class TimeTicks; | 20 class TimeTicks; |
| 18 class Value; | 21 class Value; |
| 19 } | 22 } |
| 20 | 23 |
| 21 namespace net { | 24 namespace net { |
| 22 | 25 |
| 23 // NetLog is the destination for log messages generated by the network stack. | 26 // 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 | 27 // Each log message has a "source" field which identifies the specific entity |
| 25 // that generated the message (for example, which URLRequest or which | 28 // that generated the message (for example, which URLRequest or which |
| 26 // SocketStream). | 29 // SocketStream). |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 // uniquely identify the source, and is used by log observers to infer | 61 // uniquely identify the source, and is used by log observers to infer |
| 59 // message groupings. Can use NetLog::NextID() to create unique IDs. | 62 // message groupings. Can use NetLog::NextID() to create unique IDs. |
| 60 struct NET_EXPORT Source { | 63 struct NET_EXPORT Source { |
| 61 static const uint32 kInvalidId = 0; | 64 static const uint32 kInvalidId = 0; |
| 62 | 65 |
| 63 Source() : type(SOURCE_NONE), id(kInvalidId) {} | 66 Source() : type(SOURCE_NONE), id(kInvalidId) {} |
| 64 Source(SourceType type, uint32 id) : type(type), id(id) {} | 67 Source(SourceType type, uint32 id) : type(type), id(id) {} |
| 65 bool is_valid() const { return id != kInvalidId; } | 68 bool is_valid() const { return id != kInvalidId; } |
| 66 | 69 |
| 67 // The caller takes ownership of the returned Value*. | 70 // The caller takes ownership of the returned Value*. |
| 71 // TODO(mmenke): Remove this. | |
| 68 base::Value* ToValue() const; | 72 base::Value* ToValue() const; |
| 69 | 73 |
| 74 // Adds the source to a DictionaryValue containing event parameters, | |
| 75 // using the name "source_dependency". | |
| 76 void AddToEventParameters(base::DictionaryValue* event_params) const; | |
| 77 | |
| 78 // Attempts to extract a Source from a set of event parameters. Returns | |
| 79 // true and writes the result to |source| on success. Returns false and | |
| 80 // makes |source| an invalid source on failure. | |
| 81 static bool FromEventParameters(base::Value* event_params, Source* source); | |
| 82 | |
| 70 SourceType type; | 83 SourceType type; |
| 71 uint32 id; | 84 uint32 id; |
| 72 }; | 85 }; |
| 73 | 86 |
| 74 // Base class for associating additional parameters with an event. Log | 87 // Base class for associating additional parameters with an event. Log |
| 75 // observers need to know what specific derivations of EventParameters a | 88 // observers need to know what specific derivations of EventParameters a |
| 76 // particular EventType uses, in order to get at the individual components. | 89 // particular EventType uses, in order to get at the individual components. |
| 77 class NET_EXPORT EventParameters | 90 class NET_EXPORT EventParameters |
| 78 : public base::RefCountedThreadSafe<EventParameters> { | 91 : public base::RefCountedThreadSafe<EventParameters> { |
| 79 public: | 92 public: |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 100 LOG_ALL, | 113 LOG_ALL, |
| 101 | 114 |
| 102 // Log all events, but do not include the actual transferred bytes as | 115 // Log all events, but do not include the actual transferred bytes as |
| 103 // parameters for bytes sent/received events. | 116 // parameters for bytes sent/received events. |
| 104 LOG_ALL_BUT_BYTES, | 117 LOG_ALL_BUT_BYTES, |
| 105 | 118 |
| 106 // Only log events which are cheap, and don't consume much memory. | 119 // Only log events which are cheap, and don't consume much memory. |
| 107 LOG_BASIC, | 120 LOG_BASIC, |
| 108 }; | 121 }; |
| 109 | 122 |
| 123 // A callback function that return a Value representation of the parameters | |
| 124 // associated with an event. If called, it will be called synchonously, | |
| 125 // so it need not have owning references. May be called more than once, or | |
| 126 // not at all. | |
| 127 typedef base::Callback<base::Value*(LogLevel)> ParametersCallback; | |
| 128 | |
| 129 class NET_EXPORT Entry { | |
| 130 public: | |
| 131 Entry(EventType type, | |
| 132 Source source, | |
| 133 EventPhase phase, | |
| 134 const ParametersCallback* parameters_callback, | |
| 135 LogLevel log_level); | |
| 136 ~Entry(); | |
| 137 | |
| 138 EventType type() const { return type_; } | |
| 139 Source source() const { return source_; } | |
| 140 EventPhase phase() const { return phase_; } | |
| 141 | |
| 142 // Serializes the specified event to a Value. The Value also includes the | |
| 143 // current time. Caller takes ownership of returned Value. | |
| 144 base::Value* ToValue() const; | |
| 145 | |
| 146 // Returns the parameters as a Value. Returns NULL if there are no such | |
| 147 // objects. Caller takes ownership of returned Value. | |
| 148 base::Value* ParametersToValue() const; | |
| 149 | |
| 150 private: | |
| 151 const EventType type_; | |
| 152 const Source source_; | |
| 153 const EventPhase phase_; | |
| 154 const ParametersCallback* parameters_callback_; | |
| 155 | |
| 156 // Log level when the event occurred. | |
| 157 const LogLevel log_level_; | |
| 158 | |
| 159 DISALLOW_COPY_AND_ASSIGN(Entry); | |
|
eroman
2012/05/22 22:08:06
I suggest leaving a comment warning not to enable
| |
| 160 }; | |
| 161 | |
| 110 // An observer, that must ensure its own thread safety, for events | 162 // An observer, that must ensure its own thread safety, for events |
| 111 // being added to a NetLog. | 163 // being added to a NetLog. |
| 112 class NET_EXPORT ThreadSafeObserver { | 164 class NET_EXPORT ThreadSafeObserver { |
| 113 public: | 165 public: |
| 114 // Constructs an observer that wants to see network events, with | 166 // Constructs an observer that wants to see network events, with |
| 115 // the specified minimum event granularity. A ThreadSafeObserver can only | 167 // the specified minimum event granularity. A ThreadSafeObserver can only |
| 116 // observe a single NetLog at a time. | 168 // observe a single NetLog at a time. |
| 117 // | 169 // |
| 118 // Observers will be called on the same thread an entry is added on, | 170 // Observers will be called on the same thread an entry is added on, |
| 119 // and are responsible for ensuring their own thread safety. | 171 // and are responsible for ensuring their own thread safety. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 131 // otherwise. | 183 // otherwise. |
| 132 NetLog* net_log() const; | 184 NetLog* net_log() const; |
| 133 | 185 |
| 134 // This method will be called on the thread that the event occurs on. It | 186 // This method will be called on the thread that the event occurs on. It |
| 135 // is the responsibility of the observer to handle it in a thread safe | 187 // is the responsibility of the observer to handle it in a thread safe |
| 136 // manner. | 188 // manner. |
| 137 // | 189 // |
| 138 // It is illegal for an Observer to call any NetLog or | 190 // It is illegal for an Observer to call any NetLog or |
| 139 // NetLog::Observer functions in response to a call to OnAddEntry. | 191 // NetLog::Observer functions in response to a call to OnAddEntry. |
| 140 // | 192 // |
| 141 // |type| - The type of the event. | 193 // |type| - The type of the event. |
|
eroman
2012/05/22 22:08:06
Please update the parameters.
| |
| 142 // |time| - The time when the event occurred. | 194 // |time| - The time when the event occurred. |
| 143 // |source| - The source that generated the event. | 195 // |source| - The source that generated the event. |
| 144 // |phase| - An optional parameter indicating whether this is the start/end | 196 // |phase| - An optional parameter indicating whether this is the start/end |
| 145 // of an action. | 197 // of an action. |
| 146 // |params| - Optional (may be NULL) parameters for this event. | 198 // |params| - Optional (may be NULL) parameters for this event. |
| 147 // The specific subclass of EventParameters is defined | 199 // The specific subclass of EventParameters is defined |
| 148 // by the contract for events of this |type|. | 200 // by the contract for events of this |type|. |
| 149 // TODO(eroman): Take a scoped_refptr<EventParameters> instead. | 201 // TODO(eroman): Take a scoped_refptr<EventParameters> instead. |
|
eroman
2012/05/22 22:08:06
Please delete this comment.
| |
| 150 virtual void OnAddEntry(EventType type, | 202 virtual void OnAddEntry(const Entry& entry) = 0; |
| 151 const base::TimeTicks& time, | |
| 152 const Source& source, | |
| 153 EventPhase phase, | |
| 154 EventParameters* params) = 0; | |
| 155 | 203 |
| 156 private: | 204 private: |
| 157 friend class NetLog; | 205 friend class NetLog; |
| 158 | 206 |
| 159 // Both of these values are only modified by the NetLog. | 207 // Both of these values are only modified by the NetLog. |
| 160 LogLevel log_level_; | 208 LogLevel log_level_; |
| 161 NetLog* net_log_; | 209 NetLog* net_log_; |
| 162 | 210 |
| 163 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); | 211 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); |
| 164 }; | 212 }; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 // Returns a C-String symbolic name for |source_type|. | 265 // Returns a C-String symbolic name for |source_type|. |
| 218 static const char* SourceTypeToString(SourceType source_type); | 266 static const char* SourceTypeToString(SourceType source_type); |
| 219 | 267 |
| 220 // Returns a dictionary that maps source type symbolic names to their enum | 268 // Returns a dictionary that maps source type symbolic names to their enum |
| 221 // values. Caller takes ownership of the returned Value. | 269 // values. Caller takes ownership of the returned Value. |
| 222 static base::Value* GetSourceTypesAsValue(); | 270 static base::Value* GetSourceTypesAsValue(); |
| 223 | 271 |
| 224 // Returns a C-String symbolic name for |event_phase|. | 272 // Returns a C-String symbolic name for |event_phase|. |
| 225 static const char* EventPhaseToString(EventPhase event_phase); | 273 static const char* EventPhaseToString(EventPhase event_phase); |
| 226 | 274 |
| 227 // Serializes the specified event to a DictionaryValue. | 275 static Source GetSourceFromEventParameters(base::Value* event_params); |
| 228 // If |use_strings| is true, uses strings rather than numeric ids. | |
| 229 static base::Value* EntryToDictionaryValue(NetLog::EventType type, | |
| 230 const base::TimeTicks& time, | |
| 231 const NetLog::Source& source, | |
| 232 NetLog::EventPhase phase, | |
| 233 NetLog::EventParameters* params, | |
| 234 bool use_strings); | |
| 235 | 276 |
| 236 protected: | 277 protected: |
| 237 // This is the internal function used by AddGlobalEntry and BoundNetLogs. | 278 // Child classes should respond to the new entry here. This includes |
| 238 virtual void AddEntry( | 279 // creating the Entry object and alerting their observers. |
| 239 EventType type, | 280 virtual void OnAddEntry(const Entry& entry) = 0; |
| 240 const Source& source, | |
| 241 EventPhase phase, | |
| 242 const scoped_refptr<NetLog::EventParameters>& params) = 0; | |
| 243 | 281 |
| 244 // Subclasses must call these in the corresponding functions to set an | 282 // Subclasses must call these in the corresponding functions to set an |
| 245 // observer's |net_log_| and |log_level_| values. | 283 // observer's |net_log_| and |log_level_| values. |
| 246 void OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level); | 284 void OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level); |
| 247 void OnSetObserverLogLevel(ThreadSafeObserver* observer, | 285 void OnSetObserverLogLevel(ThreadSafeObserver* observer, |
| 248 LogLevel log_level); | 286 LogLevel log_level); |
| 249 void OnRemoveObserver(ThreadSafeObserver* observer); | 287 void OnRemoveObserver(ThreadSafeObserver* observer); |
| 250 | 288 |
| 251 private: | 289 private: |
| 252 friend class BoundNetLog; | 290 friend class BoundNetLog; |
| 253 | 291 |
| 292 void AddEntry(EventType type, | |
| 293 const Source& source, | |
| 294 EventPhase phase, | |
| 295 const NetLog::ParametersCallback* parameters_callback); | |
| 296 | |
| 254 DISALLOW_COPY_AND_ASSIGN(NetLog); | 297 DISALLOW_COPY_AND_ASSIGN(NetLog); |
| 255 }; | 298 }; |
| 256 | 299 |
| 257 // Helper that binds a Source to a NetLog, and exposes convenience methods to | 300 // Helper that binds a Source to a NetLog, and exposes convenience methods to |
| 258 // output log messages without needing to pass in the source. | 301 // output log messages without needing to pass in the source. |
| 259 class NET_EXPORT BoundNetLog { | 302 class NET_EXPORT BoundNetLog { |
| 260 public: | 303 public: |
| 261 BoundNetLog() : net_log_(NULL) {} | 304 BoundNetLog() : net_log_(NULL) {} |
| 262 | 305 |
| 263 // Convenience methods that call through to the NetLog, passing in the | 306 void AddEntry(NetLog::EventType type, NetLog::EventPhase phase); |
| 264 // currently bound source. | 307 void AddEntry(NetLog::EventType type, |
| 308 NetLog::EventPhase phase, | |
| 309 const NetLog::ParametersCallback& get_parameters); | |
| 310 | |
| 311 void AddEvent(NetLog::EventType type); | |
| 312 void AddEvent(NetLog::EventType type, | |
| 313 const NetLog::ParametersCallback& get_parameters); | |
| 314 | |
| 315 void BeginEvent(NetLog::EventType type); | |
| 316 void BeginEvent(NetLog::EventType type, | |
| 317 const NetLog::ParametersCallback& get_parameters); | |
| 318 | |
| 319 void EndEvent(NetLog::EventType type); | |
| 320 void EndEvent(NetLog::EventType type, | |
| 321 const NetLog::ParametersCallback& get_parameters); | |
| 322 | |
| 265 void AddEntry(NetLog::EventType type, | 323 void AddEntry(NetLog::EventType type, |
| 266 NetLog::EventPhase phase, | 324 NetLog::EventPhase phase, |
| 267 const scoped_refptr<NetLog::EventParameters>& params) const; | 325 const scoped_refptr<NetLog::EventParameters>& params) const; |
| 268 | 326 |
| 269 // Convenience methods that call through to the NetLog, passing in the | 327 // Convenience methods that call through to the NetLog, passing in the |
| 270 // currently bound source, current time, and a fixed "capture phase" | 328 // currently bound source, current time, and a fixed "capture phase" |
| 271 // (begin, end, or none). | 329 // (begin, end, or none). |
| 272 void AddEvent(NetLog::EventType event_type, | 330 void AddEvent(NetLog::EventType event_type, |
| 273 const scoped_refptr<NetLog::EventParameters>& params) const; | 331 const scoped_refptr<NetLog::EventParameters>& params) const; |
| 274 void BeginEvent(NetLog::EventType event_type, | 332 void BeginEvent(NetLog::EventType event_type, |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 404 | 462 |
| 405 private: | 463 private: |
| 406 BoundNetLog net_log_; | 464 BoundNetLog net_log_; |
| 407 const NetLog::EventType event_type_; | 465 const NetLog::EventType event_type_; |
| 408 scoped_refptr<NetLog::EventParameters> end_event_params_; | 466 scoped_refptr<NetLog::EventParameters> end_event_params_; |
| 409 }; | 467 }; |
| 410 | 468 |
| 411 } // namespace net | 469 } // namespace net |
| 412 | 470 |
| 413 #endif // NET_BASE_NET_LOG_H_ | 471 #endif // NET_BASE_NET_LOG_H_ |
| OLD | NEW |