diff options
author | mmenke@chromium.org <mmenke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-30 01:32:30 +0000 |
---|---|---|
committer | mmenke@chromium.org <mmenke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-30 01:32:30 +0000 |
commit | 62bde744e6697d644964f18fdfd051fdb6fbf0bd (patch) | |
tree | be2fe5ca4e2bfe7f61b1c47306ca75e739e09490 /net | |
parent | b520507ab4ae017818047d22609a4d2fb1cf0e44 (diff) | |
download | chromium_src-62bde744e6697d644964f18fdfd051fdb6fbf0bd.zip chromium_src-62bde744e6697d644964f18fdfd051fdb6fbf0bd.tar.gz chromium_src-62bde744e6697d644964f18fdfd051fdb6fbf0bd.tar.bz2 |
Disable ChromeNetLog when there are no observers.
Performance gains are probably neglible, though it does
prevent locking.
Review URL: https://codereview.chromium.org/12209100
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@191485 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/net_log.cc | 5 | ||||
-rw-r--r-- | net/base/net_log.h | 10 | ||||
-rw-r--r-- | net/base/net_log_unittest.cc | 75 | ||||
-rw-r--r-- | net/net.gyp | 1 |
4 files changed, 89 insertions, 2 deletions
diff --git a/net/base/net_log.cc b/net/base/net_log.cc index 12cfd81..4d9e55a 100644 --- a/net/base/net_log.cc +++ b/net/base/net_log.cc @@ -322,8 +322,11 @@ void NetLog::AddEntry(EventType type, const Source& source, EventPhase phase, const NetLog::ParametersCallback* parameters_callback) { + LogLevel log_level = GetLogLevel(); + if (log_level == LOG_NONE) + return; Entry entry(type, source, phase, base::TimeTicks::Now(), - parameters_callback, GetLogLevel()); + parameters_callback, log_level); OnAddEntry(entry); } diff --git a/net/base/net_log.h b/net/base/net_log.h index 0dd97ee..8ae33e9 100644 --- a/net/base/net_log.h +++ b/net/base/net_log.h @@ -60,6 +60,10 @@ class NET_EXPORT NetLog { }; // Specifies the granularity of events that should be emitted to the log. + // + // Since the LogLevel may be read and set on any thread without locking, it + // may be possible for an Observer to receive an event or parameters that + // normally wouldn't be logged at the currently active log level. enum LogLevel { // Log everything possible, even if it is slow and memory expensive. // Includes logging of transferred bytes. @@ -69,8 +73,12 @@ class NET_EXPORT NetLog { // parameters for bytes sent/received events. LOG_ALL_BUT_BYTES, - // Only log events which are cheap, and don't consume much memory. + // Only log events which are cheap, and don't consume much memory. This is + // the default value for observers. LOG_BASIC, + + // Don't log any events. + LOG_NONE, }; // A callback function that return a Value representation of the parameters diff --git a/net/base/net_log_unittest.cc b/net/base/net_log_unittest.cc new file mode 100644 index 0000000..d52f3b7 --- /dev/null +++ b/net/base/net_log_unittest.cc @@ -0,0 +1,75 @@ +// Copyright (c) 2013 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/net_log_unittest.h" + +#include "base/bind.h" +#include "base/values.h" +#include "net/base/net_errors.h" + +namespace net { + +namespace { + +base::Value* NetLogLevelCallback(NetLog::LogLevel log_level) { + base::DictionaryValue* dict = new base::DictionaryValue(); + dict->SetInteger("log_level", log_level); + return dict; +} + +TEST(NetLogTest, Basic) { + CapturingNetLog net_log; + net::CapturingNetLog::CapturedEntryList entries; + net_log.GetEntries(&entries); + EXPECT_EQ(0u, entries.size()); + + net_log.AddGlobalEntry(NetLog::TYPE_CANCELLED); + + net_log.GetEntries(&entries); + ASSERT_EQ(1u, entries.size()); + EXPECT_EQ(NetLog::TYPE_CANCELLED, entries[0].type); + EXPECT_EQ(NetLog::SOURCE_NONE, entries[0].source.type); + EXPECT_NE(NetLog::Source::kInvalidId, entries[0].source.id); + EXPECT_EQ(NetLog::PHASE_NONE, entries[0].phase); + EXPECT_GE(base::TimeTicks::Now(), entries[0].time); + EXPECT_FALSE(entries[0].params); +} + +// Check that the correct LogLevel is sent to NetLog Value callbacks, and that +// LOG_NONE logs no events. +TEST(NetLogTest, LogLevels) { + CapturingNetLog net_log; + for (int log_level = NetLog::LOG_ALL; log_level <= NetLog::LOG_NONE; + ++log_level) { + net_log.SetLogLevel(static_cast<NetLog::LogLevel>(log_level)); + EXPECT_EQ(log_level, net_log.GetLogLevel()); + + net_log.AddGlobalEntry(NetLog::TYPE_SOCKET_ALIVE, + base::Bind(NetLogLevelCallback)); + + net::CapturingNetLog::CapturedEntryList entries; + net_log.GetEntries(&entries); + + if (log_level == NetLog::LOG_NONE) { + EXPECT_EQ(0u, entries.size()); + } else { + ASSERT_EQ(1u, entries.size()); + EXPECT_EQ(NetLog::TYPE_SOCKET_ALIVE, entries[0].type); + EXPECT_EQ(NetLog::SOURCE_NONE, entries[0].source.type); + EXPECT_NE(NetLog::Source::kInvalidId, entries[0].source.id); + EXPECT_EQ(NetLog::PHASE_NONE, entries[0].phase); + EXPECT_GE(base::TimeTicks::Now(), entries[0].time); + + int logged_log_level; + ASSERT_TRUE(entries[0].GetIntegerValue("log_level", &logged_log_level)); + EXPECT_EQ(log_level, logged_log_level); + } + + net_log.Clear(); + } +} + +} // namespace + +} // namespace net diff --git a/net/net.gyp b/net/net.gyp index 94c9e3c..715d7ef 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -1399,6 +1399,7 @@ 'base/mime_util_unittest.cc', 'base/mock_filter_context.cc', 'base/mock_filter_context.h', + 'base/net_log_unittest.cc', 'base/net_log_unittest.h', 'base/net_util_unittest.cc', 'base/network_change_notifier_win_unittest.cc', |