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