diff options
author | dcastagna@google.com <dcastagna@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-13 22:12:27 +0000 |
---|---|---|
committer | dcastagna@google.com <dcastagna@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-13 22:12:27 +0000 |
commit | 7e929f48631779ae0264733b1a7769712c204d42 (patch) | |
tree | 166810d325fc37cba123c79067edb3ad0a8642a9 /net/tools | |
parent | b34617b77decc96de2f84bde0520a575c5d61ef7 (diff) | |
download | chromium_src-7e929f48631779ae0264733b1a7769712c204d42.zip chromium_src-7e929f48631779ae0264733b1a7769712c204d42.tar.gz chromium_src-7e929f48631779ae0264733b1a7769712c204d42.tar.bz2 |
Adding logging functionality to the utility gdig
Added a new class FileNetLog that extends NetLog and prints all the events received on a stream.
The utility gdig has been modified accordingly to use this new class for logging HostResolver events and print them out on the stderr.
BUG=128212
TEST=build and run gdig with the parameter --net_log
Review URL: https://chromiumcodereview.appspot.com/10536117
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@141991 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/tools')
-rw-r--r-- | net/tools/gdig/file_net_log.cc | 75 | ||||
-rw-r--r-- | net/tools/gdig/file_net_log.h | 48 | ||||
-rw-r--r-- | net/tools/gdig/gdig.cc | 36 |
3 files changed, 154 insertions, 5 deletions
diff --git a/net/tools/gdig/file_net_log.cc b/net/tools/gdig/file_net_log.cc new file mode 100644 index 0000000..13d271e --- /dev/null +++ b/net/tools/gdig/file_net_log.cc @@ -0,0 +1,75 @@ +// Copyright (c) 2012 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 <stdio.h> + +#include "base/json/json_string_value_serializer.h" +#include "base/logging.h" +#include "base/values.h" +#include "net/tools/gdig/file_net_log.h" + +namespace net { + +FileNetLog::FileNetLog(FILE* destination, LogLevel level) + : log_level_(level), + destination_(destination) { + DCHECK(destination != NULL); + // Without calling GetNext() once here, the first GetNext will return 0 + // that is not a valid id. + sequence_number_.GetNext(); +} + +FileNetLog::~FileNetLog() { +} + +void FileNetLog::OnAddEntry(const net::NetLog::Entry& entry) { + // Only BoundNetLogs without a NetLog should have an invalid source. + DCHECK(entry.source().is_valid()); + + const char* source = SourceTypeToString(entry.source().type); + const char* type = EventTypeToString(entry.type()); + + scoped_ptr<Value> param_value(entry.ParametersToValue()); + std::string params; + if (param_value.get() != NULL) { + JSONStringValueSerializer serializer(¶ms); + bool ret = serializer.Serialize(*param_value); + DCHECK(ret); + } + base::Time now = base::Time::NowFromSystemTime(); + base::AutoLock lock(lock_); + if (first_event_time_.is_null()) { + first_event_time_ = now; + } + base::TimeDelta elapsed_time = now - first_event_time_; + fprintf(destination_ , "%u\t%u\t%s\t%s\t%s\n", + static_cast<unsigned>(elapsed_time.InMilliseconds()), + entry.source().id, source, type, params.c_str()); +} + +uint32 FileNetLog::NextID() { + return sequence_number_.GetNext(); +} + +NetLog::LogLevel FileNetLog::GetLogLevel() const { + return log_level_; +} + +void FileNetLog::AddThreadSafeObserver( + NetLog::ThreadSafeObserver* observer, + NetLog::LogLevel log_level) { + NOTIMPLEMENTED() << "Not currently used by gdig."; +} + +void FileNetLog::SetObserverLogLevel(ThreadSafeObserver* observer, + LogLevel log_level) { + NOTIMPLEMENTED() << "Not currently used by gdig."; +} + +void FileNetLog::RemoveThreadSafeObserver( + NetLog::ThreadSafeObserver* observer) { + NOTIMPLEMENTED() << "Not currently used by gdig."; +} + +} // namespace net diff --git a/net/tools/gdig/file_net_log.h b/net/tools/gdig/file_net_log.h new file mode 100644 index 0000000..2d77895 --- /dev/null +++ b/net/tools/gdig/file_net_log.h @@ -0,0 +1,48 @@ +// Copyright (c) 2012 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. + +#ifndef NET_TOOLS_GDIG_FILE_NET_LOG_H_ +#define NET_TOOLS_GDIG_FILE_NET_LOG_H_ +#pragma once + +#include <string> + +#include "base/atomic_sequence_num.h" +#include "base/basictypes.h" +#include "base/synchronization/lock.h" +#include "base/time.h" +#include "net/base/net_log.h" + +namespace net { + +// FileNetLog is a simple implementation of NetLog that prints out all +// the events received into the stream passed to the constructor. +class FileNetLog : public NetLog { + public: + explicit FileNetLog(FILE* destination, LogLevel level); + virtual ~FileNetLog(); + + private: + // NetLog implementation: + virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; + virtual uint32 NextID() OVERRIDE; + virtual LogLevel GetLogLevel() const OVERRIDE; + virtual void AddThreadSafeObserver(ThreadSafeObserver* observer, + LogLevel log_level) OVERRIDE; + virtual void SetObserverLogLevel(ThreadSafeObserver* observer, + LogLevel log_level) OVERRIDE; + virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) OVERRIDE; + + base::AtomicSequenceNumber sequence_number_; + const NetLog::LogLevel log_level_; + + FILE* const destination_; + base::Lock lock_; + + base::Time first_event_time_; +}; + +} // namespace net + +#endif // NET_TOOLS_GDIG_FILE_NET_LOG_H_ diff --git a/net/tools/gdig/gdig.cc b/net/tools/gdig/gdig.cc index be7b05a..eafc021 100644 --- a/net/tools/gdig/gdig.cc +++ b/net/tools/gdig/gdig.cc @@ -18,9 +18,11 @@ #include "net/base/host_cache.h" #include "net/base/host_resolver_impl.h" #include "net/base/net_errors.h" +#include "net/base/net_log.h" #include "net/base/net_util.h" #include "net/dns/dns_client.h" #include "net/dns/dns_config_service.h" +#include "net/tools/gdig/file_net_log.h" #if defined(OS_MACOSX) #include "base/mac/scoped_nsautorelease_pool.h" @@ -60,6 +62,7 @@ class GDig { base::CancelableClosure timeout_closure_; scoped_ptr<DnsConfigService> dns_config_service_; + scoped_ptr<FileNetLog> log_; scoped_ptr<HostResolver> resolver_; }; @@ -71,7 +74,8 @@ GDig::GDig() GDig::Result GDig::Main(int argc, const char* argv[]) { if (!ParseCommandLine(argc, argv)) { fprintf(stderr, - "usage: %s [--config_timeout=<seconds>] domain_name\n", + "usage: %s [--net_log[=<basic|no_bytes|all>]]" + " [--config_timeout=<seconds>] domain_name\n", argv[0]); return RESULT_WRONG_USAGE; } @@ -137,13 +141,15 @@ void GDig::OnDnsConfig(const DnsConfig& dns_config) { HostResolverImpl::ProcTaskParams(NULL, 1), scoped_ptr<DnsConfigService>(NULL), dns_client.Pass(), - NULL)); + log_.get())); HostResolver::RequestInfo info(HostPortPair(domain_name_.c_str(), 80)); CompletionCallback callback = base::Bind(&GDig::OnResolveComplete, base::Unretained(this)); - int ret = resolver_->Resolve(info, &addrlist_, callback, NULL, BoundNetLog()); + int ret = resolver_->Resolve( + info, &addrlist_, callback, NULL, + BoundNetLog::Make(log_.get(), net::NetLog::SOURCE_NONE)); DCHECK(ret == ERR_IO_PENDING); } @@ -168,9 +174,29 @@ bool GDig::ParseCommandLine(int argc, const char* argv[]) { if (parsed && timeout_seconds > 0) { timeout_ = base::TimeDelta::FromSeconds(timeout_seconds); } else { - fprintf(stderr, - "Invalid config_timeout parameter, using the default value\n"); + fprintf(stderr, "Invalid config_timeout parameter\n"); + return false; + } + } + + if (parsed_command_line.HasSwitch("net_log")) { + std::string log_param = parsed_command_line.GetSwitchValueASCII("net_log"); + NetLog::LogLevel level = NetLog::LOG_ALL_BUT_BYTES; + + if (log_param.length() > 0) { + std::map<std::string, NetLog::LogLevel> log_levels; + log_levels["all"] = NetLog::LOG_ALL; + log_levels["no_bytes"] = NetLog::LOG_ALL_BUT_BYTES; + log_levels["basic"] = NetLog::LOG_BASIC; + + if (log_levels.find(log_param) != log_levels.end()) { + level = log_levels[log_param]; + } else { + fprintf(stderr, "Invalid net_log parameter\n"); + return false; + } } + log_.reset(new FileNetLog(stderr, level)); } return true; |