diff options
Diffstat (limited to 'net/tools')
-rw-r--r-- | net/tools/gdig/file_net_log.cc | 40 | ||||
-rw-r--r-- | net/tools/gdig/file_net_log.h | 26 | ||||
-rw-r--r-- | net/tools/gdig/gdig.cc | 13 | ||||
-rw-r--r-- | net/tools/get_server_time/get_server_time.cc | 67 |
4 files changed, 50 insertions, 96 deletions
diff --git a/net/tools/gdig/file_net_log.cc b/net/tools/gdig/file_net_log.cc index 51cf78d..7c755c8 100644 --- a/net/tools/gdig/file_net_log.cc +++ b/net/tools/gdig/file_net_log.cc @@ -12,24 +12,20 @@ namespace net { -FileNetLog::FileNetLog(FILE* destination, LogLevel level) - : log_level_(level), - destination_(destination) { +FileNetLogObserver::FileNetLogObserver(FILE* destination) + : 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() { +FileNetLogObserver::~FileNetLogObserver() { } -void FileNetLog::OnAddEntry(const net::NetLog::Entry& entry) { +void FileNetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { // Only BoundNetLogs without a NetLog should have an invalid source. DCHECK(entry.source().IsValid()); - const char* source = SourceTypeToString(entry.source().type); - const char* type = EventTypeToString(entry.type()); + const char* source = NetLog::SourceTypeToString(entry.source().type); + const char* type = NetLog::EventTypeToString(entry.type()); scoped_ptr<Value> param_value(entry.ParametersToValue()); std::string params; @@ -49,28 +45,4 @@ void FileNetLog::OnAddEntry(const net::NetLog::Entry& entry) { 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 index cb80380..1ad78f1 100644 --- a/net/tools/gdig/file_net_log.h +++ b/net/tools/gdig/file_net_log.h @@ -7,7 +7,6 @@ #include <string> -#include "base/atomic_sequence_num.h" #include "base/basictypes.h" #include "base/synchronization/lock.h" #include "base/time.h" @@ -15,27 +14,18 @@ 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 { +// FileNetLogObserver is a simple implementation of NetLog::ThreadSafeObserver +// that prints out all the events received into the stream passed +// to the constructor. +class FileNetLogObserver : public NetLog::ThreadSafeObserver { public: - explicit FileNetLog(FILE* destination, LogLevel level); - virtual ~FileNetLog(); + explicit FileNetLogObserver(FILE* destination); + virtual ~FileNetLogObserver(); - private: - // NetLog implementation: + // NetLog::ThreadSafeObserver 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_; + private: FILE* const destination_; base::Lock lock_; diff --git a/net/tools/gdig/gdig.cc b/net/tools/gdig/gdig.cc index 1640408..7cbb9a4 100644 --- a/net/tools/gdig/gdig.cc +++ b/net/tools/gdig/gdig.cc @@ -182,6 +182,7 @@ bool LoadReplayLog(const base::FilePath& file_path, ReplayLog* replay_log) { class GDig { public: GDig(); + ~GDig(); enum Result { RESULT_NO_RESOLVE = -3, @@ -219,7 +220,8 @@ class GDig { base::CancelableClosure timeout_closure_; scoped_ptr<DnsConfigService> dns_config_service_; - scoped_ptr<FileNetLog> log_; + scoped_ptr<FileNetLogObserver> log_observer_; + scoped_ptr<NetLog> log_; scoped_ptr<HostResolver> resolver_; }; @@ -232,6 +234,11 @@ GDig::GDig() active_resolves_(0) { } +GDig::~GDig() { + if (log_) + log_->RemoveThreadSafeObserver(log_observer_.get()); +} + GDig::Result GDig::Main(int argc, const char* argv[]) { if (!ParseCommandLine(argc, argv)) { fprintf(stderr, @@ -299,7 +306,9 @@ bool GDig::ParseCommandLine(int argc, const char* argv[]) { return false; } } - log_.reset(new FileNetLog(stderr, level)); + log_.reset(new NetLog); + log_observer_.reset(new FileNetLogObserver(stderr)); + log_->AddThreadSafeObserver(log_observer_.get(), level); } print_config_ = parsed_command_line.HasSwitch("print_config"); diff --git a/net/tools/get_server_time/get_server_time.cc b/net/tools/get_server_time/get_server_time.cc index df9e3fc..1fca24e 100644 --- a/net/tools/get_server_time/get_server_time.cc +++ b/net/tools/get_server_time/get_server_time.cc @@ -103,14 +103,18 @@ class QuitDelegate : public net::URLFetcherDelegate { DISALLOW_COPY_AND_ASSIGN(QuitDelegate); }; -// NetLog implementation that simply prints events to the logs. -class PrintingLog : public net::NetLog { +// NetLog::ThreadSafeObserver implementation that simply prints events +// to the logs. +class PrintingLogObserver : public net::NetLog::ThreadSafeObserver { public: - PrintingLog() : next_id_(1) {} + PrintingLogObserver() {} - virtual ~PrintingLog() {} + virtual ~PrintingLogObserver() { + // This is guaranteed to be safe as this program is single threaded. + net_log()->RemoveThreadSafeObserver(this); + } - // NetLog implementation: + // NetLog::ThreadSafeObserver implementation: virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE { // The log level of the entry is unknown, so just assume it maps // to VLOG(1). @@ -134,43 +138,13 @@ class PrintingLog : public net::NetLog { << event_type << ": " << event_phase << params_str; } - virtual uint32 NextID() OVERRIDE { - return next_id_++; - } - - virtual LogLevel GetLogLevel() const OVERRIDE { - const int vlog_level = logging::GetVlogLevel(__FILE__); - if (vlog_level <= 0) { - return LOG_BASIC; - } - if (vlog_level == 1) { - return LOG_ALL_BUT_BYTES; - } - return LOG_ALL; - } - - virtual void AddThreadSafeObserver(ThreadSafeObserver* observer, - LogLevel log_level) OVERRIDE { - NOTIMPLEMENTED(); - } - - virtual void SetObserverLogLevel(ThreadSafeObserver* observer, - LogLevel log_level) OVERRIDE { - NOTIMPLEMENTED(); - } - - virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) OVERRIDE { - NOTIMPLEMENTED(); - } - private: - uint32 next_id_; - - DISALLOW_COPY_AND_ASSIGN(PrintingLog); + DISALLOW_COPY_AND_ASSIGN(PrintingLogObserver); }; // Builds a URLRequestContext assuming there's only a single loop. -scoped_ptr<net::URLRequestContext> BuildURLRequestContext() { +scoped_ptr<net::URLRequestContext> +BuildURLRequestContext(net::NetLog* net_log) { net::URLRequestContextBuilder builder; #if defined(OS_LINUX) // On Linux, use a fixed ProxyConfigService, since the default one @@ -181,7 +155,7 @@ scoped_ptr<net::URLRequestContext> BuildURLRequestContext() { new net::ProxyConfigServiceFixed(net::ProxyConfig())); #endif scoped_ptr<net::URLRequestContext> context(builder.Build()); - context->set_net_log(new PrintingLog()); + context->set_net_log(net_log); return context.Pass(); } @@ -189,9 +163,10 @@ class SingleThreadRequestContextGetter : public net::URLRequestContextGetter { public: // Since there's only a single thread, there's no need to worry // about when |context_| gets created. - explicit SingleThreadRequestContextGetter( + SingleThreadRequestContextGetter( + net::NetLog* net_log, const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) - : context_(BuildURLRequestContext()), + : context_(BuildURLRequestContext(net_log)), main_task_runner_(main_task_runner) {} virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE { @@ -281,8 +256,16 @@ int main(int argc, char* argv[]) { // which causes the DNS resolution to abort. It's simpler to just // not instantiate one, since only a single request is sent anyway. + // The declaration order for net_log and printing_log_observer is + // important. The destructor of PrintingLogObserver removes itself + // from net_log, so net_log must be available for entire lifetime of + // printing_log_observer. + net::NetLog net_log; + PrintingLogObserver printing_log_observer; + net_log.AddThreadSafeObserver(&printing_log_observer, net::NetLog::LOG_ALL); scoped_refptr<SingleThreadRequestContextGetter> context_getter( - new SingleThreadRequestContextGetter(main_loop.message_loop_proxy())); + new SingleThreadRequestContextGetter(&net_log, + main_loop.message_loop_proxy())); QuitDelegate delegate; scoped_ptr<net::URLFetcher> fetcher( |