| 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_CAPTURING_NET_LOG_H_ | 5 #ifndef NET_BASE_CAPTURING_NET_LOG_H_ |
| 6 #define NET_BASE_CAPTURING_NET_LOG_H_ | 6 #define NET_BASE_CAPTURING_NET_LOG_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/atomicops.h" | 11 #include "base/atomicops.h" |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/time.h" | 16 #include "base/time.h" |
| 17 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
| 18 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
| 19 | 19 |
| 20 namespace base { |
| 21 class Value; |
| 22 } |
| 23 |
| 20 namespace net { | 24 namespace net { |
| 21 | 25 |
| 22 // CapturingNetLog is an implementation of NetLog that saves messages to a | 26 // CapturingNetLog is an implementation of NetLog that saves messages to a |
| 23 // bounded buffer. | 27 // bounded buffer. |
| 24 class NET_EXPORT CapturingNetLog : public NetLog { | 28 class NET_EXPORT CapturingNetLog : public NetLog { |
| 25 public: | 29 public: |
| 26 struct NET_EXPORT Entry { | 30 struct NET_EXPORT CapturedEntry { |
| 27 Entry(EventType type, | 31 CapturedEntry(EventType type, |
| 28 const base::TimeTicks& time, | 32 const base::TimeTicks& time, |
| 29 Source source, | 33 Source source, |
| 30 EventPhase phase, | 34 EventPhase phase); |
| 31 EventParameters* extra_parameters); | 35 ~CapturedEntry(); |
| 32 ~Entry(); | |
| 33 | 36 |
| 34 EventType type; | 37 EventType type; |
| 35 base::TimeTicks time; | 38 base::TimeTicks time; |
| 36 Source source; | 39 Source source; |
| 37 EventPhase phase; | 40 EventPhase phase; |
| 38 scoped_refptr<EventParameters> extra_parameters; | |
| 39 }; | 41 }; |
| 40 | 42 |
| 41 // Ordered set of entries that were logged. | 43 // Ordered set of entries that were logged. |
| 42 typedef std::vector<Entry> EntryList; | 44 typedef std::vector<CapturedEntry> CapturedEntryList; |
| 43 | 45 |
| 44 enum { kUnbounded = -1 }; | 46 enum { kUnbounded = -1 }; |
| 45 | 47 |
| 46 // Creates a CapturingNetLog that logs a maximum of |max_num_entries| | 48 // Creates a CapturingNetLog that logs a maximum of |max_num_entries| |
| 47 // messages. | 49 // messages. |
| 48 explicit CapturingNetLog(size_t max_num_entries); | 50 explicit CapturingNetLog(size_t max_num_entries); |
| 49 virtual ~CapturingNetLog(); | 51 virtual ~CapturingNetLog(); |
| 50 | 52 |
| 51 // Returns the list of all entries in the log. | 53 // Returns the list of all entries in the log. |
| 52 void GetEntries(EntryList* entry_list) const; | 54 void GetEntries(CapturedEntryList* entry_list) const; |
| 53 | 55 |
| 54 void Clear(); | 56 void Clear(); |
| 55 | 57 |
| 56 void SetLogLevel(NetLog::LogLevel log_level); | 58 void SetLogLevel(NetLog::LogLevel log_level); |
| 57 | 59 |
| 58 // NetLog implementation: | 60 // NetLog implementation: |
| 59 virtual void AddEntry( | 61 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; |
| 60 EventType type, | |
| 61 const Source& source, | |
| 62 EventPhase phase, | |
| 63 const scoped_refptr<EventParameters>& extra_parameters) OVERRIDE; | |
| 64 virtual uint32 NextID() OVERRIDE; | 62 virtual uint32 NextID() OVERRIDE; |
| 65 virtual LogLevel GetLogLevel() const OVERRIDE; | 63 virtual LogLevel GetLogLevel() const OVERRIDE; |
| 66 virtual void AddThreadSafeObserver(ThreadSafeObserver* observer, | 64 virtual void AddThreadSafeObserver(ThreadSafeObserver* observer, |
| 67 LogLevel log_level) OVERRIDE; | 65 LogLevel log_level) OVERRIDE; |
| 68 virtual void SetObserverLogLevel(ThreadSafeObserver* observer, | 66 virtual void SetObserverLogLevel(ThreadSafeObserver* observer, |
| 69 LogLevel log_level) OVERRIDE; | 67 LogLevel log_level) OVERRIDE; |
| 70 virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) OVERRIDE; | 68 virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) OVERRIDE; |
| 71 | 69 |
| 72 private: | 70 private: |
| 73 // Needs to be "mutable" so can use it in GetEntries(). | 71 // Needs to be "mutable" so can use it in GetEntries(). |
| 74 mutable base::Lock lock_; | 72 mutable base::Lock lock_; |
| 75 | 73 |
| 76 // Last assigned source ID. Incremented to get the next one. | 74 // Last assigned source ID. Incremented to get the next one. |
| 77 base::subtle::Atomic32 last_id_; | 75 base::subtle::Atomic32 last_id_; |
| 78 | 76 |
| 79 size_t max_num_entries_; | 77 size_t max_num_entries_; |
| 80 EntryList entries_; | 78 CapturedEntryList captured_entries_; |
| 81 | 79 |
| 82 NetLog::LogLevel log_level_; | 80 NetLog::LogLevel log_level_; |
| 83 | 81 |
| 84 DISALLOW_COPY_AND_ASSIGN(CapturingNetLog); | 82 DISALLOW_COPY_AND_ASSIGN(CapturingNetLog); |
| 85 }; | 83 }; |
| 86 | 84 |
| 87 // Helper class that exposes a similar API as BoundNetLog, but uses a | 85 // Helper class that exposes a similar API as BoundNetLog, but uses a |
| 88 // CapturingNetLog rather than the more generic NetLog. | 86 // CapturingNetLog rather than the more generic NetLog. |
| 89 // | 87 // |
| 90 // CapturingBoundNetLog can easily be converted to a BoundNetLog using the | 88 // CapturingBoundNetLog can easily be converted to a BoundNetLog using the |
| 91 // bound() method. | 89 // bound() method. |
| 92 class NET_EXPORT_PRIVATE CapturingBoundNetLog { | 90 class NET_EXPORT_PRIVATE CapturingBoundNetLog { |
| 93 public: | 91 public: |
| 94 explicit CapturingBoundNetLog(size_t max_num_entries); | 92 explicit CapturingBoundNetLog(size_t max_num_entries); |
| 95 | 93 |
| 96 ~CapturingBoundNetLog(); | 94 ~CapturingBoundNetLog(); |
| 97 | 95 |
| 98 // The returned BoundNetLog is only valid while |this| is alive. | 96 // The returned BoundNetLog is only valid while |this| is alive. |
| 99 BoundNetLog bound() const { return net_log_; } | 97 BoundNetLog bound() const { return net_log_; } |
| 100 | 98 |
| 101 // Fills |entry_list| with all entries in the log. | 99 // Fills |entry_list| with all entries in the log. |
| 102 void GetEntries(CapturingNetLog::EntryList* entry_list) const; | 100 void GetEntries(CapturingNetLog::CapturedEntryList* entry_list) const; |
| 103 | 101 |
| 104 void Clear(); | 102 void Clear(); |
| 105 | 103 |
| 106 // Sets the log level of the underlying CapturingNetLog. | 104 // Sets the log level of the underlying CapturingNetLog. |
| 107 void SetLogLevel(NetLog::LogLevel log_level); | 105 void SetLogLevel(NetLog::LogLevel log_level); |
| 108 | 106 |
| 109 private: | 107 private: |
| 110 CapturingNetLog capturing_net_log_; | 108 CapturingNetLog capturing_net_log_; |
| 111 const BoundNetLog net_log_; | 109 const BoundNetLog net_log_; |
| 112 | 110 |
| 113 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); | 111 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); |
| 114 }; | 112 }; |
| 115 | 113 |
| 116 } // namespace net | 114 } // namespace net |
| 117 | 115 |
| 118 #endif // NET_BASE_CAPTURING_NET_LOG_H_ | 116 #endif // NET_BASE_CAPTURING_NET_LOG_H_ |
| OLD | NEW |