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