summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authormmenke@chromium.org <mmenke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-09 17:33:21 +0000
committermmenke@chromium.org <mmenke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-09 17:33:21 +0000
commite8188fe70d217c47204c09396143c2dcb8b9a521 (patch)
tree14cce2cd6f5e36c96b66f3c372538f71fea9f6a9 /net
parent82cceaf8775c7512a7f3c3fd60949b1b3fcb5380 (diff)
downloadchromium_src-e8188fe70d217c47204c09396143c2dcb8b9a521.zip
chromium_src-e8188fe70d217c47204c09396143c2dcb8b9a521.tar.gz
chromium_src-e8188fe70d217c47204c09396143c2dcb8b9a521.tar.bz2
A ScopedNetLogEvent that logs a NetLog begin event on creation,
and the corresponding end event on destruction. BUG=64981 TEST=NetLog.ScopedNetLogEventTest Review URL: http://codereview.chromium.org/5560013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@68742 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/base/net_log.cc23
-rw-r--r--net/base/net_log.h24
-rw-r--r--net/base/net_log_unittest.cc34
-rw-r--r--net/net.gyp1
4 files changed, 82 insertions, 0 deletions
diff --git a/net/base/net_log.cc b/net/base/net_log.cc
index 1499d72..c9e7319 100644
--- a/net/base/net_log.cc
+++ b/net/base/net_log.cc
@@ -194,4 +194,27 @@ Value* NetLogSourceParameter::ToValue() const {
return dict;
}
+ScopedNetLogEvent::ScopedNetLogEvent(
+ const BoundNetLog& net_log,
+ NetLog::EventType event_type,
+ const scoped_refptr<NetLog::EventParameters>& params)
+ : net_log_(net_log),
+ event_type_(event_type) {
+ net_log_.BeginEvent(event_type, params);
+}
+
+ScopedNetLogEvent::~ScopedNetLogEvent() {
+ net_log_.EndEvent(event_type_, end_event_params_);
+}
+
+void ScopedNetLogEvent::SetEndEventParameters(
+ const scoped_refptr<NetLog::EventParameters>& end_event_params) {
+ DCHECK(!end_event_params_.get());
+ end_event_params_ = end_event_params;
+}
+
+const BoundNetLog& ScopedNetLogEvent::net_log() const {
+ return net_log_;
+}
+
} // namespace net
diff --git a/net/base/net_log.h b/net/base/net_log.h
index 5a8f197..b8f903d 100644
--- a/net/base/net_log.h
+++ b/net/base/net_log.h
@@ -270,6 +270,30 @@ class NetLogSourceParameter : public NetLog::EventParameters {
const NetLog::Source value_;
};
+// ScopedNetLogEvent logs a begin event on creation, and the corresponding end
+// event on destruction.
+class ScopedNetLogEvent {
+ public:
+ ScopedNetLogEvent(const BoundNetLog& net_log,
+ NetLog::EventType event_type,
+ const scoped_refptr<NetLog::EventParameters>& params);
+
+ ~ScopedNetLogEvent();
+
+ // Sets the parameters that will logged on object destruction. Can be called
+ // at most once for a given ScopedNetLogEvent object. If not called, the end
+ // event will have no parameters.
+ void SetEndEventParameters(
+ const scoped_refptr<NetLog::EventParameters>& end_event_params);
+
+ const BoundNetLog& net_log() const;
+
+ private:
+ BoundNetLog net_log_;
+ const NetLog::EventType event_type_;
+ scoped_refptr<NetLog::EventParameters> end_event_params_;
+};
+
} // namespace net
#endif // NET_BASE_NET_LOG_H_
diff --git a/net/base/net_log_unittest.cc b/net/base/net_log_unittest.cc
new file mode 100644
index 0000000..8523955
--- /dev/null
+++ b/net/base/net_log_unittest.cc
@@ -0,0 +1,34 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/capturing_net_log.h"
+#include "net/base/net_log.h"
+#include "net/base/net_log_unittest.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+
+namespace {
+
+TEST(NetLog, ScopedNetLogEventTest) {
+ CapturingNetLog log(CapturingNetLog::kUnbounded);
+ BoundNetLog net_log(BoundNetLog::Make(&log, NetLog::SOURCE_URL_REQUEST));
+
+ scoped_ptr<ScopedNetLogEvent> net_log_event(
+ new ScopedNetLogEvent(net_log, NetLog::TYPE_REQUEST_ALIVE, NULL));
+
+ CapturingNetLog::EntryList entries;
+ log.GetEntries(&entries);
+ EXPECT_EQ(1u, entries.size());
+ EXPECT_TRUE(LogContainsBeginEvent(entries, 0, NetLog::TYPE_REQUEST_ALIVE));
+
+ net_log_event.reset();
+ log.GetEntries(&entries);
+ EXPECT_EQ(2u, entries.size());
+ EXPECT_TRUE(LogContainsEndEvent(entries, 1, NetLog::TYPE_REQUEST_ALIVE));
+}
+
+} // namespace
+
+} // namespace net
diff --git a/net/net.gyp b/net/net.gyp
index 82359ab..8004873 100644
--- a/net/net.gyp
+++ b/net/net.gyp
@@ -851,6 +851,7 @@
'base/mapped_host_resolver_unittest.cc',
'base/mime_sniffer_unittest.cc',
'base/mime_util_unittest.cc',
+ 'base/net_log_unittest.cc',
'base/net_log_unittest.h',
'base/net_test_suite.h',
'base/net_util_unittest.cc',