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 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/observer_list.h" | |
| 13 #include "base/string16.h" | 14 #include "base/string16.h" |
| 14 #include "base/time.h" | 15 #include "base/time.h" |
| 15 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 class DictionaryValue; | 19 class DictionaryValue; |
| 19 class Value; | 20 class Value; |
| 20 } | 21 } |
| 21 | 22 |
| 22 namespace net { | 23 namespace net { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 private: | 192 private: |
| 192 friend class NetLog; | 193 friend class NetLog; |
| 193 | 194 |
| 194 // Both of these values are only modified by the NetLog. | 195 // Both of these values are only modified by the NetLog. |
| 195 LogLevel log_level_; | 196 LogLevel log_level_; |
| 196 NetLog* net_log_; | 197 NetLog* net_log_; |
| 197 | 198 |
| 198 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); | 199 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); |
| 199 }; | 200 }; |
| 200 | 201 |
| 201 NetLog() {} | 202 explicit NetLog(LogLevel base_log_level = LOG_NONE); |
|
mmenke
2013/05/30 18:18:59
The Google style guide comes out pretty strongly a
kouhei (in TOK)
2013/05/31 06:30:03
Done.
| |
| 202 virtual ~NetLog() {} | 203 virtual ~NetLog(); |
| 203 | 204 |
| 204 // Emits a global event to the log stream, with its own unique source ID. | 205 // Emits a global event to the log stream, with its own unique source ID. |
| 205 void AddGlobalEntry(EventType type); | 206 void AddGlobalEntry(EventType type); |
| 206 void AddGlobalEntry(EventType type, | 207 void AddGlobalEntry(EventType type, |
| 207 const NetLog::ParametersCallback& parameters_callback); | 208 const NetLog::ParametersCallback& parameters_callback); |
| 208 | 209 |
| 209 // Returns a unique ID which can be used as a source ID. All returned IDs | 210 // Returns a unique ID which can be used as a source ID. All returned IDs |
| 210 // will be unique and greater than 0. | 211 // will be unique and greater than 0. |
| 211 virtual uint32 NextID() = 0; | 212 uint32 NextID(); |
| 212 | 213 |
| 213 // Returns the logging level for this NetLog. This is used to avoid computing | 214 // Returns the logging level for this NetLog. This is used to avoid computing |
| 214 // and saving expensive log entries. | 215 // and saving expensive log entries. |
| 215 virtual LogLevel GetLogLevel() const = 0; | 216 LogLevel GetLogLevel() const; |
| 216 | 217 |
| 217 // Adds an observer and sets its log level. The observer must not be | 218 // Adds an observer and sets its log level. The observer must not be |
| 218 // watching any NetLog, including this one, when this is called. | 219 // watching any NetLog, including this one, when this is called. |
| 219 // | 220 // |
| 220 // Typical observers should specify LOG_BASIC. | 221 // Typical observers should specify LOG_BASIC. |
| 221 // | 222 // |
| 222 // Observers that need to see the full granularity of events can specify | 223 // Observers that need to see the full granularity of events can specify |
| 223 // LOG_ALL_BUT_BYTES. However, doing so will have performance consequences. | 224 // LOG_ALL_BUT_BYTES. However, doing so will have performance consequences. |
| 224 // | 225 // |
| 225 // NetLog implementations must call NetLog::OnAddObserver to update the | 226 // NetLog implementations must call NetLog::OnAddObserver to update the |
| 226 // observer's internal state. | 227 // observer's internal state. |
| 227 virtual void AddThreadSafeObserver(ThreadSafeObserver* observer, | 228 void AddThreadSafeObserver(ThreadSafeObserver* observer, LogLevel log_level); |
| 228 LogLevel log_level) = 0; | |
| 229 | 229 |
| 230 // Sets the log level of |observer| to |log_level|. |observer| must be | 230 // Sets the log level of |observer| to |log_level|. |observer| must be |
| 231 // watching |this|. NetLog implementations must call | 231 // watching |this|. NetLog implementations must call |
| 232 // NetLog::OnSetObserverLogLevel to update the observer's internal state. | 232 // NetLog::OnSetObserverLogLevel to update the observer's internal state. |
| 233 virtual void SetObserverLogLevel(ThreadSafeObserver* observer, | 233 void SetObserverLogLevel(ThreadSafeObserver* observer, LogLevel log_level); |
| 234 LogLevel log_level) = 0; | |
| 235 | 234 |
| 236 // Removes an observer. NetLog implementations must call | 235 // Removes an observer. NetLog implementations must call |
| 237 // NetLog::OnAddObserver to update the observer's internal state. | 236 // NetLog::OnAddObserver to update the observer's internal state. |
| 238 // | 237 // |
| 239 // For thread safety reasons, it is recommended that this not be called in | 238 // For thread safety reasons, it is recommended that this not be called in |
| 240 // an object's destructor. | 239 // an object's destructor. |
| 241 virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) = 0; | 240 void RemoveThreadSafeObserver(ThreadSafeObserver* observer); |
| 242 | 241 |
| 243 // Converts a time to the string format that the NetLog uses to represent | 242 // Converts a time to the string format that the NetLog uses to represent |
| 244 // times. Strings are used since integers may overflow. | 243 // times. Strings are used since integers may overflow. |
| 245 static std::string TickCountToString(const base::TimeTicks& time); | 244 static std::string TickCountToString(const base::TimeTicks& time); |
| 246 | 245 |
| 247 // Returns a C-String symbolic name for |event_type|. | 246 // Returns a C-String symbolic name for |event_type|. |
| 248 static const char* EventTypeToString(EventType event_type); | 247 static const char* EventTypeToString(EventType event_type); |
| 249 | 248 |
| 250 // Returns a dictionary that maps event type symbolic names to their enum | 249 // Returns a dictionary that maps event type symbolic names to their enum |
| 251 // values. Caller takes ownership of the returned Value. | 250 // values. Caller takes ownership of the returned Value. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 static ParametersCallback StringCallback(const char* name, | 287 static ParametersCallback StringCallback(const char* name, |
| 289 const std::string* value); | 288 const std::string* value); |
| 290 | 289 |
| 291 // Same as above, but takes in a UTF16 string. | 290 // Same as above, but takes in a UTF16 string. |
| 292 static ParametersCallback StringCallback(const char* name, | 291 static ParametersCallback StringCallback(const char* name, |
| 293 const base::string16* value); | 292 const base::string16* value); |
| 294 | 293 |
| 295 protected: | 294 protected: |
| 296 // Child classes should respond to the new entry here. This includes | 295 // Child classes should respond to the new entry here. This includes |
| 297 // creating the Entry object and alerting their observers. | 296 // creating the Entry object and alerting their observers. |
| 298 virtual void OnAddEntry(const Entry& entry) = 0; | 297 void OnAddEntry(const Entry& entry); |
| 299 | 298 |
| 300 // Subclasses must call these in the corresponding functions to set an | 299 // Subclasses must call these in the corresponding functions to set an |
| 301 // observer's |net_log_| and |log_level_| values. | 300 // observer's |net_log_| and |log_level_| values. |
| 302 void OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level); | 301 void OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level); |
| 303 void OnSetObserverLogLevel(ThreadSafeObserver* observer, | 302 void OnSetObserverLogLevel(ThreadSafeObserver* observer, |
| 304 LogLevel log_level); | 303 LogLevel log_level); |
| 305 void OnRemoveObserver(ThreadSafeObserver* observer); | 304 void OnRemoveObserver(ThreadSafeObserver* observer); |
| 306 | 305 |
| 307 private: | 306 private: |
| 308 friend class BoundNetLog; | 307 friend class BoundNetLog; |
| 309 | 308 |
| 310 void AddEntry(EventType type, | 309 void AddEntry(EventType type, |
| 311 const Source& source, | 310 const Source& source, |
| 312 EventPhase phase, | 311 EventPhase phase, |
| 313 const NetLog::ParametersCallback* parameters_callback); | 312 const NetLog::ParametersCallback* parameters_callback); |
| 314 | 313 |
| 314 // Called whenever an observer is added or removed, or has its log level | |
| 315 // changed. Must have acquired |lock_| prior to calling. | |
| 316 void UpdateLogLevel(); | |
| 317 | |
| 318 // |lock_| protects access to |observers_|. | |
| 319 base::Lock lock_; | |
|
mmenke
2013/05/30 18:18:59
Need a header for this.
kouhei (in TOK)
2013/05/31 06:30:03
Done.
| |
| 320 | |
| 321 // Last assigned source ID. Incremented to get the next one. | |
| 322 base::subtle::Atomic32 last_id_; | |
|
mmenke
2013/05/30 18:18:59
Need a header for this.
kouhei (in TOK)
2013/05/31 06:30:03
Done.
| |
| 323 | |
| 324 // The lowest allowed log level, regardless of any Observers. | |
| 325 // Normally defaults to LOG_NONE, but can be changed with command line flags. | |
| 326 const LogLevel base_log_level_; | |
| 327 | |
| 328 // The current log level. | |
| 329 base::subtle::Atomic32 effective_log_level_; | |
| 330 | |
| 331 // |lock_| must be acquired whenever reading or writing to this. | |
| 332 ObserverList<ThreadSafeObserver, true> observers_; | |
| 333 | |
| 315 DISALLOW_COPY_AND_ASSIGN(NetLog); | 334 DISALLOW_COPY_AND_ASSIGN(NetLog); |
| 316 }; | 335 }; |
| 317 | 336 |
| 318 // Helper that binds a Source to a NetLog, and exposes convenience methods to | 337 // Helper that binds a Source to a NetLog, and exposes convenience methods to |
| 319 // output log messages without needing to pass in the source. | 338 // output log messages without needing to pass in the source. |
| 320 class NET_EXPORT BoundNetLog { | 339 class NET_EXPORT BoundNetLog { |
| 321 public: | 340 public: |
| 322 BoundNetLog() : net_log_(NULL) {} | 341 BoundNetLog() : net_log_(NULL) {} |
| 323 | 342 |
| 324 // Add a log entry to the NetLog for the bound source. | 343 // Add a log entry to the NetLog for the bound source. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 : source_(source), net_log_(net_log) { | 399 : source_(source), net_log_(net_log) { |
| 381 } | 400 } |
| 382 | 401 |
| 383 NetLog::Source source_; | 402 NetLog::Source source_; |
| 384 NetLog* net_log_; | 403 NetLog* net_log_; |
| 385 }; | 404 }; |
| 386 | 405 |
| 387 } // namespace net | 406 } // namespace net |
| 388 | 407 |
| 389 #endif // NET_BASE_NET_LOG_H_ | 408 #endif // NET_BASE_NET_LOG_H_ |
| OLD | NEW |