capturing_net_log.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 3// found in the LICENSE file. 4 5#ifndef NET_BASE_CAPTURING_NET_LOG_H_ 6#define NET_BASE_CAPTURING_NET_LOG_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/atomicops.h" 12#include "base/basictypes.h" 13#include "base/compiler_specific.h" 14#include "base/memory/ref_counted.h" 15#include "base/memory/scoped_ptr.h" 16#include "base/synchronization/lock.h" 17#include "base/time.h" 18#include "net/base/net_log.h" 19 20namespace base { 21class DictionaryValue; 22} 23 24namespace net { 25 26// CapturingNetLog is an implementation of NetLog that saves messages to a 27// bounded buffer. It is intended for testing only, and is part of the 28// net_test_support project. 29class CapturingNetLog : public NetLog { 30 public: 31 struct CapturedEntry { 32 CapturedEntry(EventType type, 33 const base::TimeTicks& time, 34 Source source, 35 EventPhase phase, 36 scoped_ptr<base::DictionaryValue> params); 37 // Copy constructor needed to store in a std::vector because of the 38 // scoped_ptr. 39 CapturedEntry(const CapturedEntry& entry); 40 41 ~CapturedEntry(); 42 43 // Equality operator needed to store in a std::vector because of the 44 // scoped_ptr. 45 CapturedEntry& operator=(const CapturedEntry& entry); 46 47 // Attempt to retrieve an value of the specified type with the given name 48 // from |params|. Returns true on success, false on failure. Does not 49 // modify |value| on failure. 50 bool GetStringValue(const std::string& name, std::string* value) const; 51 bool GetIntegerValue(const std::string& name, int* value) const; 52 53 // Same as GetIntegerValue, but returns the error code associated with a 54 // log entry. 55 bool GetNetErrorCode(int* value) const; 56 57 EventType type; 58 base::TimeTicks time; 59 Source source; 60 EventPhase phase; 61 scoped_ptr<base::DictionaryValue> params; 62 }; 63 64 // Ordered set of entries that were logged. 65 typedef std::vector<CapturedEntry> CapturedEntryList; 66 67 CapturingNetLog(); 68 virtual ~CapturingNetLog(); 69 70 // Returns the list of all entries in the log. 71 void GetEntries(CapturedEntryList* entry_list) const; 72 73 // Returns number of entries in the log. 74 size_t GetSize() const; 75 76 void Clear(); 77 78 void SetLogLevel(NetLog::LogLevel log_level); 79 80 // NetLog implementation: 81 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; 82 virtual uint32 NextID() OVERRIDE; 83 virtual LogLevel GetLogLevel() const OVERRIDE; 84 virtual void AddThreadSafeObserver(ThreadSafeObserver* observer, 85 LogLevel log_level) OVERRIDE; 86 virtual void SetObserverLogLevel(ThreadSafeObserver* observer, 87 LogLevel log_level) OVERRIDE; 88 virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) OVERRIDE; 89 90 private: 91 // Needs to be "mutable" so can use it in GetEntries(). 92 mutable base::Lock lock_; 93 94 // Last assigned source ID. Incremented to get the next one. 95 base::subtle::Atomic32 last_id_; 96 97 CapturedEntryList captured_entries_; 98 99 NetLog::LogLevel log_level_; 100 101 DISALLOW_COPY_AND_ASSIGN(CapturingNetLog); 102}; 103 104// Helper class that exposes a similar API as BoundNetLog, but uses a 105// CapturingNetLog rather than the more generic NetLog. 106// 107// CapturingBoundNetLog can easily be converted to a BoundNetLog using the 108// bound() method. 109class CapturingBoundNetLog { 110 public: 111 CapturingBoundNetLog(); 112 ~CapturingBoundNetLog(); 113 114 // The returned BoundNetLog is only valid while |this| is alive. 115 BoundNetLog bound() const { return net_log_; } 116 117 // Fills |entry_list| with all entries in the log. 118 void GetEntries(CapturingNetLog::CapturedEntryList* entry_list) const; 119 120 // Returns number of entries in the log. 121 size_t GetSize() const; 122 123 void Clear(); 124 125 // Sets the log level of the underlying CapturingNetLog. 126 void SetLogLevel(NetLog::LogLevel log_level); 127 128 private: 129 CapturingNetLog capturing_net_log_; 130 const BoundNetLog net_log_; 131 132 DISALLOW_COPY_AND_ASSIGN(CapturingBoundNetLog); 133}; 134 135} // namespace net 136 137#endif // NET_BASE_CAPTURING_NET_LOG_H_ 138