diff options
author | eroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-09 04:34:47 +0000 |
---|---|---|
committer | eroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-09 04:34:47 +0000 |
commit | 1f0e32b947057d19c6772587e72799f2644119f1 (patch) | |
tree | 57fcee3c417e40c87cb81bf316ccef45a1878e0f /net/base | |
parent | 3ef4bc6355b48c74928feefd8bbaff501bb8d59f (diff) | |
download | chromium_src-1f0e32b947057d19c6772587e72799f2644119f1.zip chromium_src-1f0e32b947057d19c6772587e72799f2644119f1.tar.gz chromium_src-1f0e32b947057d19c6772587e72799f2644119f1.tar.bz2 |
Add support for attaching custom parameters to NetLog events.
BUG=37421
Review URL: http://codereview.chromium.org/1556018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44057 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/host_resolver_impl.cc | 16 | ||||
-rw-r--r-- | net/base/host_resolver_impl.h | 2 | ||||
-rw-r--r-- | net/base/net_log.cc | 161 | ||||
-rw-r--r-- | net/base/net_log.h | 181 | ||||
-rw-r--r-- | net/base/net_log_event_type_list.h | 19 | ||||
-rw-r--r-- | net/base/net_log_unittest.h | 23 | ||||
-rw-r--r-- | net/base/net_log_util.cc | 109 | ||||
-rw-r--r-- | net/base/net_log_util.h | 3 | ||||
-rw-r--r-- | net/base/net_log_util_unittest.cc | 46 |
9 files changed, 353 insertions, 207 deletions
diff --git a/net/base/host_resolver_impl.cc b/net/base/host_resolver_impl.cc index 4fe2ccd..1f43c60 100644 --- a/net/base/host_resolver_impl.cc +++ b/net/base/host_resolver_impl.cc @@ -163,16 +163,16 @@ class HostResolverImpl::RequestsTrace RequestsTrace() {} void Add(const std::string& msg) { - NetLog::Entry entry; - entry.type = NetLog::Entry::TYPE_STRING; - entry.time = base::TimeTicks::Now(); - entry.string = msg; - + CapturingNetLog::Entry entry(NetLog::TYPE_TODO_STRING, + base::TimeTicks::Now(), + NetLog::Source(), + NetLog::PHASE_NONE, + new NetLogStringParameter(msg)); AutoLock l(lock_); entries_.push_back(entry); } - void Get(std::vector<NetLog::Entry>* entries) { + void Get(CapturingNetLog::EntryList* entries) { AutoLock l(lock_); *entries = entries_; } @@ -184,7 +184,7 @@ class HostResolverImpl::RequestsTrace private: Lock lock_; - std::vector<NetLog::Entry> entries_; + CapturingNetLog::EntryList entries_; }; //----------------------------------------------------------------------------- @@ -912,7 +912,7 @@ bool HostResolverImpl::IsRequestsTracingEnabled() const { return !!requests_trace_; // Cast to bool. } -bool HostResolverImpl::GetRequestsTrace(std::vector<NetLog::Entry>* entries) { +bool HostResolverImpl::GetRequestsTrace(CapturingNetLog::EntryList* entries) { if (!requests_trace_) return false; requests_trace_->Get(entries); diff --git a/net/base/host_resolver_impl.h b/net/base/host_resolver_impl.h index 1a87dfe..4814921 100644 --- a/net/base/host_resolver_impl.h +++ b/net/base/host_resolver_impl.h @@ -113,7 +113,7 @@ class HostResolverImpl : public HostResolver { bool IsRequestsTracingEnabled() const; // Gets a copy of the requests trace log. - bool GetRequestsTrace(std::vector<NetLog::Entry>* entries); + bool GetRequestsTrace(CapturingNetLog::EntryList* entries); // Applies a set of constraints for requests that belong to the specified // pool. NOTE: Don't call this after requests have been already been started. diff --git a/net/base/net_log.cc b/net/base/net_log.cc index 6bb9297..1ebdfdb 100644 --- a/net/base/net_log.cc +++ b/net/base/net_log.cc @@ -4,6 +4,7 @@ #include "net/base/net_log.h" #include "base/logging.h" +#include "base/string_util.h" namespace net { @@ -26,9 +27,23 @@ std::vector<NetLog::EventType> NetLog::GetAllEventTypes() { return types; } -void BoundNetLog::AddEntry(const NetLog::Entry& entry) const { - if (net_log_) - net_log_->AddEntry(entry); +void BoundNetLog::AddEntry(NetLog::EventType type, + NetLog::EventPhase phase, + NetLog::EventParameters* extra_parameters) const { + if (net_log_) { + net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase, + extra_parameters); + } +} + +void BoundNetLog::AddEntryWithTime( + NetLog::EventType type, + const base::TimeTicks& time, + NetLog::EventPhase phase, + NetLog::EventParameters* extra_parameters) const { + if (net_log_) { + net_log_->AddEntry(type, time, source_, phase, extra_parameters); + } } bool BoundNetLog::HasListener() const { @@ -38,91 +53,72 @@ bool BoundNetLog::HasListener() const { } void BoundNetLog::AddEvent(NetLog::EventType event_type) const { - if (net_log_) { - NetLog::Entry entry; - entry.source = source_; - entry.type = NetLog::Entry::TYPE_EVENT; - entry.time = base::TimeTicks::Now(); - entry.event = NetLog::Event(event_type, NetLog::PHASE_NONE); - AddEntry(entry); - } + AddEventWithParameters(event_type, NULL); +} + +void BoundNetLog::AddEventWithParameters( + NetLog::EventType event_type, + NetLog::EventParameters* params) const { + AddEntry(event_type, NetLog::PHASE_NONE, params); +} + +void BoundNetLog::AddEventWithInteger(NetLog::EventType event_type, + int integer) const { + scoped_refptr<NetLog::EventParameters> params = + new NetLogIntegerParameter(integer); + AddEventWithParameters(event_type, params); } void BoundNetLog::BeginEvent(NetLog::EventType event_type) const { - if (net_log_) { - NetLog::Entry entry; - entry.source = source_; - entry.type = NetLog::Entry::TYPE_EVENT; - entry.time = base::TimeTicks::Now(); - entry.event = NetLog::Event(event_type, NetLog::PHASE_BEGIN); - AddEntry(entry); - } + BeginEventWithParameters(event_type, NULL); +} + +void BoundNetLog::BeginEventWithParameters( + NetLog::EventType event_type, + NetLog::EventParameters* params) const { + AddEntry(event_type, NetLog::PHASE_BEGIN, params); } void BoundNetLog::BeginEventWithString(NetLog::EventType event_type, const std::string& string) const { - NetLog::Entry entry; - entry.source = source_; - entry.type = NetLog::Entry::TYPE_EVENT; - entry.time = base::TimeTicks::Now(); - entry.event = NetLog::Event(event_type, NetLog::PHASE_BEGIN); - entry.string = string; - AddEntry(entry); + scoped_refptr<NetLog::EventParameters> params = + new NetLogStringParameter(string); + BeginEventWithParameters(event_type, params); } -void BoundNetLog::AddEventWithInteger(NetLog::EventType event_type, - int integer) const { - NetLog::Entry entry; - entry.source = source_; - entry.type = NetLog::Entry::TYPE_EVENT; - entry.time = base::TimeTicks::Now(); - entry.event = NetLog::Event(event_type, NetLog::PHASE_NONE); - entry.error_code = integer; - AddEntry(entry); +void BoundNetLog::EndEvent(NetLog::EventType event_type) const { + EndEventWithParameters(event_type, NULL); } -void BoundNetLog::EndEvent(NetLog::EventType event_type) const { - if (net_log_) { - NetLog::Entry entry; - entry.source = source_; - entry.type = NetLog::Entry::TYPE_EVENT; - entry.time = base::TimeTicks::Now(); - entry.event = NetLog::Event(event_type, NetLog::PHASE_END); - AddEntry(entry); - } +void BoundNetLog::EndEventWithParameters( + NetLog::EventType event_type, + NetLog::EventParameters* params) const { + AddEntry(event_type, NetLog::PHASE_END, params); } -void BoundNetLog::AddStringLiteral(const char* literal) const { - if (net_log_) { - NetLog::Entry entry; - entry.source = source_; - entry.type = NetLog::Entry::TYPE_STRING_LITERAL; - entry.time = base::TimeTicks::Now(); - entry.literal = literal; - AddEntry(entry); - } +void BoundNetLog::EndEventWithInteger(NetLog::EventType event_type, + int integer) const { + scoped_refptr<NetLog::EventParameters> params = + new NetLogIntegerParameter(integer); + EndEventWithParameters(event_type, params); } void BoundNetLog::AddString(const std::string& string) const { - if (net_log_) { - NetLog::Entry entry; - entry.source = source_; - entry.type = NetLog::Entry::TYPE_STRING; - entry.time = base::TimeTicks::Now(); - entry.string = string; - AddEntry(entry); - } + // We pass TYPE_TODO_STRING since we have no event type to associate this + // with. (AddString() is deprecated, and should be replaced with + // AddEventWithParameters()). + scoped_refptr<NetLog::EventParameters> params = + new NetLogStringParameter(string); + AddEventWithParameters(NetLog::TYPE_TODO_STRING, params); } -void BoundNetLog::AddErrorCode(int error) const { - if (net_log_) { - NetLog::Entry entry; - entry.source = source_; - entry.type = NetLog::Entry::TYPE_ERROR_CODE; - entry.time = base::TimeTicks::Now(); - entry.error_code = error; - AddEntry(entry); - } +void BoundNetLog::AddStringLiteral(const char* literal) const { + // We pass TYPE_TODO_STRING_LITERAL since we have no event type to associate + // this with. (AddString() is deprecated, and should be replaced with + // AddEventWithParameters()). + scoped_refptr<NetLog::EventParameters> params = + new NetLogStringLiteralParameter(literal); + AddEventWithParameters(NetLog::TYPE_TODO_STRING_LITERAL, params); } // static @@ -135,7 +131,24 @@ BoundNetLog BoundNetLog::Make(NetLog* net_log, return BoundNetLog(source, net_log); } -void CapturingNetLog::AddEntry(const Entry& entry) { +NetLogStringParameter::NetLogStringParameter(const std::string& value) + : value_(value) { +} + +std::string NetLogIntegerParameter::ToString() const { + return IntToString(value_); +} + +std::string NetLogStringLiteralParameter::ToString() const { + return std::string(value_); +} + +void CapturingNetLog::AddEntry(EventType type, + const base::TimeTicks& time, + const Source& source, + EventPhase phase, + EventParameters* extra_parameters) { + Entry entry(type, time, source, phase, extra_parameters); if (entries_.size() + 1 < max_num_entries_) entries_.push_back(entry); } @@ -154,9 +167,9 @@ void CapturingBoundNetLog::Clear() { void CapturingBoundNetLog::AppendTo(const BoundNetLog& net_log) const { for (size_t i = 0; i < entries().size(); ++i) { - NetLog::Entry entry = entries()[i]; - entry.source = net_log.source(); - net_log.AddEntry(entry); + const CapturingNetLog::Entry& entry = entries()[i]; + net_log.AddEntryWithTime(entry.type, entry.time, entry.phase, + entry.extra_parameters); } } diff --git a/net/base/net_log.h b/net/base/net_log.h index ca83edd..3a85131 100644 --- a/net/base/net_log.h +++ b/net/base/net_log.h @@ -8,6 +8,8 @@ #include <string> #include <vector> +#include "base/basictypes.h" +#include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "base/time.h" #include "net/base/net_log.h" @@ -24,11 +26,26 @@ namespace net { // specific source ID. // // Note that NetLog is NOT THREADSAFE. +// +// ******** The NetLog (and associated logging) is a work in progress ******** +// +// TODO(eroman): Remove the 'const' qualitifer from the BoundNetLog methods. +// TODO(eroman): Remove the AddString() and AddStringLiteral() methods. +// These are a carry-over from old approach. Really, consumers +// should be calling AddEventWithParameters(), and passing a +// custom EventParameters* object that encapsulates all of the +// interesting state. +// TODO(eroman): Remove NetLogUtil. Pretty printing should only be done from +// javascript, and should be very context-aware. +// TODO(eroman): Move Capturing*NetLog to its own file. (And eventually remove +// all the consumers of it). +// TODO(eroman): Make the DNS jobs emit directly into the NetLog. +// TODO(eroman): Start a new Source each time URLRequest redirects +// (simpler to reason about each as a separate entity). +// TODO(eroman): Add the URLRequest load flags to the start entry. + class NetLog { public: - // TODO(eroman): Really, EventType and EventPhase should be - // Event::Type and Event::Phase, to be consisent with Entry. - // But there lots of consumers to change! enum EventType { #define EVENT_TYPE(label) TYPE_ ## label, #include "net/base/net_log_event_type_list.h" @@ -43,14 +60,6 @@ class NetLog { PHASE_END, }; - struct Event { - Event(EventType type, EventPhase phase) : type(type), phase(phase) {} - Event() {} - - EventType type; - EventPhase phase; - }; - // The "source" identifies the entity that generated the log message. enum SourceType { SOURCE_NONE, @@ -71,41 +80,39 @@ class NetLog { int id; }; - // TODO(eroman): generalize the entries so events can specify multiple - // parameters, and TYPE_STRING is rarely needed. - struct Entry { - enum Type { - // This entry describes an event trace. - TYPE_EVENT, - - // This entry describes a network error code that was returned. - TYPE_ERROR_CODE, - - // This entry is a free-form std::string. - TYPE_STRING, + // Base class for associating additional parameters with an event. Log + // observers need to know what specific derivations of EventParameters a + // particular EventType uses, in order to get at the individual components. + class EventParameters : public base::RefCounted<EventParameters> { + public: + EventParameters() {} + virtual ~EventParameters() {} - // This entry is a C-string literal. - TYPE_STRING_LITERAL, - }; + // Serializes the parameters to a string representation (this should be a + // lossless conversion). + virtual std::string ToString() const = 0; - Source source; - - Type type; - base::TimeTicks time; - - // The following is basically a union, only one of them should be - // used depending on what |type| is. - Event event; // valid when (type == TYPE_EVENT). - int error_code; // valid when (type == TYPE_ERROR_CODE). - std::string string; // valid when (type == TYPE_STRING). - const char* literal; // valid when (type == TYPE_STRING_LITERAL). + private: + DISALLOW_COPY_AND_ASSIGN(EventParameters); }; NetLog() {} virtual ~NetLog() {} - // Adds a message to the log. - virtual void AddEntry(const Entry& entry) = 0; + // Emits an event to the log stream. + // |type| - The type of the event. + // |time| - The time when the event occurred. + // |source| - The source that generated the event. + // |phase| - An optional parameter indicating whether this is the start/end + // of an action. + // |extra_parameters| - Optional (may be NULL) parameters for this event. + // The specific subclass of EventParameters is defined + // by the contract for events of this |type|. + virtual void AddEntry(EventType type, + const base::TimeTicks& time, + const Source& source, + EventPhase phase, + EventParameters* extra_parameters) = 0; // Returns a unique ID which can be used as a source ID. virtual int NextID() = 0; @@ -140,20 +147,36 @@ class BoundNetLog { : source_(source), net_log_(net_log) { } - void AddEntry(const NetLog::Entry& entry) const; + void AddEntry(NetLog::EventType type, + NetLog::EventPhase phase, + NetLog::EventParameters* extra_parameters) const; + + void AddEntryWithTime(NetLog::EventType type, + const base::TimeTicks& time, + NetLog::EventPhase phase, + NetLog::EventParameters* extra_parameters) const; // Convenience methods that call through to the NetLog, passing in the // currently bound source. void AddEvent(NetLog::EventType event_type) const; + void AddEventWithParameters(NetLog::EventType event_type, + NetLog::EventParameters* params) const; bool HasListener() const; void BeginEvent(NetLog::EventType event_type) const; + void BeginEventWithParameters(NetLog::EventType event_type, + NetLog::EventParameters* params) const; void BeginEventWithString(NetLog::EventType event_type, const std::string& string) const; void AddEventWithInteger(NetLog::EventType event_type, int integer) const; void EndEvent(NetLog::EventType event_type) const; - void AddStringLiteral(const char* literal) const; + void EndEventWithParameters(NetLog::EventType event_type, + NetLog::EventParameters* params) const; + void EndEventWithInteger(NetLog::EventType event_type, int integer) const; + + // Deprecated: Don't add new dependencies that use these methods. Instead, use + // AddEventWithParameters(). void AddString(const std::string& string) const; - void AddErrorCode(int error) const; + void AddStringLiteral(const char* literal) const; // Helper to create a BoundNetLog given a NetLog and a SourceType. Takes care // of creating a unique source ID, and handles the case of NULL net_log. @@ -167,10 +190,78 @@ class BoundNetLog { NetLog* net_log_; }; +// NetLogStringParameter is a subclass of EventParameters that encapsulates a +// single std::string parameter. +class NetLogStringParameter : public NetLog::EventParameters { + public: + explicit NetLogStringParameter(const std::string& value); + + const std::string& value() const { + return value_; + } + + virtual std::string ToString() const { + return value_; + } + + private: + std::string value_; +}; + +// NetLogIntegerParameter is a subclass of EventParameters that encapsulates a +// single integer parameter. +class NetLogIntegerParameter : public NetLog::EventParameters { + public: + explicit NetLogIntegerParameter(int value) : value_(value) {} + + int value() const { + return value_; + } + + virtual std::string ToString() const; + + private: + const int value_; +}; + +// NetLogStringLiteralParameter is a subclass of EventParameters that +// encapsulates a single string literal parameter. +class NetLogStringLiteralParameter : public NetLog::EventParameters { + public: + explicit NetLogStringLiteralParameter(const char* value) : value_(value) {} + + const char* const value() const { + return value_; + } + + virtual std::string ToString() const; + + private: + const char* const value_; +}; + + // CapturingNetLog is an implementation of NetLog that saves messages to a // bounded buffer. class CapturingNetLog : public NetLog { public: + struct Entry { + Entry(EventType type, + const base::TimeTicks& time, + Source source, + EventPhase phase, + EventParameters* extra_parameters) + : type(type), time(time), source(source), phase(phase), + extra_parameters(extra_parameters) { + } + + EventType type; + base::TimeTicks time; + Source source; + EventPhase phase; + scoped_refptr<EventParameters> extra_parameters; + }; + // Ordered set of entries that were logged. typedef std::vector<Entry> EntryList; @@ -182,7 +273,11 @@ class CapturingNetLog : public NetLog { : next_id_(0), max_num_entries_(max_num_entries) {} // NetLog implementation: - virtual void AddEntry(const Entry& entry); + virtual void AddEntry(EventType type, + const base::TimeTicks& time, + const Source& source, + EventPhase phase, + EventParameters* extra_parameters); virtual int NextID(); virtual bool HasListener() const { return true; } diff --git a/net/base/net_log_event_type_list.h b/net/base/net_log_event_type_list.h index cee9466..9dc8569 100644 --- a/net/base/net_log_event_type_list.h +++ b/net/base/net_log_event_type_list.h @@ -13,6 +13,11 @@ // log context around it.) EVENT_TYPE(CANCELLED) +// TODO(eroman): These are placeholders used by the deprecated +// BoundNetLog::AddString() / BoundNetLog::AddStringLiteral(). +EVENT_TYPE(TODO_STRING) +EVENT_TYPE(TODO_STRING_LITERAL) + // Marks the creation/destruction of a request (URLRequest or SocketStream). // In the begin phase of this event, the message will contain a string which // is the URL. @@ -139,6 +144,13 @@ EVENT_TYPE(SOCKET_POOL_CONNECT_JOB_ID) // Measures the time between URLRequest::Start() and // URLRequest::ResponseStarted(). +// +// For the BEGIN phase, the |extra_parameters| of the event will be of type +// NetLogStringParameter, and will contain the URL. +// +// For the END phase, the |extra_parameters| of the event will be of type +// NetLogIntegerParameter, and will contain the net error code. Altenately, +// the extra_parameters may be NULL indicating no error code. EVENT_TYPE(URL_REQUEST_START) // ------------------------------------------------------------------------ @@ -227,6 +239,13 @@ EVENT_TYPE(HTTP_STREAM_PARSER_READ_HEADERS) // Measures the time between SocketStream::Connect() and // SocketStream::DidEstablishConnection() +// +// For the BEGIN phase, the |extra_parameters| of the event will be of type +// NetLogStringParameter, and will contain the URL. +// +// For the END phase, the |extra_parameters| of the event will be of type +// NetLogIntegerParameter, and will contain the net error code. Altenately, +// the extra_parameters may be NULL indicating no error code. EVENT_TYPE(SOCKET_STREAM_CONNECT) // A message sent on the SocketStream. diff --git a/net/base/net_log_unittest.h b/net/base/net_log_unittest.h index 39a1f36..b7a6147 100644 --- a/net/base/net_log_unittest.h +++ b/net/base/net_log_unittest.h @@ -30,18 +30,16 @@ inline ::testing::AssertionResult LogContainsEventHelper( size_t j = (i < 0) ? entries.size() + i : i; if (j >= entries.size()) return ::testing::AssertionFailure() << j << " is out of bounds."; - const NetLog::Entry& entry = entries[j]; - if (entry.type != NetLog::Entry::TYPE_EVENT) - return ::testing::AssertionFailure() << "Not a TYPE_EVENT entry"; - if (expected_event != entry.event.type) { + const CapturingNetLog::Entry& entry = entries[j]; + if (expected_event != entry.type) { return ::testing::AssertionFailure() - << "Actual event: " << NetLog::EventTypeToString(entry.event.type) + << "Actual event: " << NetLog::EventTypeToString(entry.type) << ". Expected event: " << NetLog::EventTypeToString(expected_event) << "."; } - if (expected_phase != entry.event.phase) { + if (expected_phase != entry.phase) { return ::testing::AssertionFailure() - << "Actual phase: " << entry.event.phase + << "Actual phase: " << entry.phase << ". Expected phase: " << expected_phase << "."; } if (check_time) { @@ -94,12 +92,12 @@ inline ::testing::AssertionResult LogContainsEndEvent( inline ::testing::AssertionResult LogContainsEntryWithType( const CapturingNetLog::EntryList& entries, int i, // Negative indices are reverse indices. - NetLog::Entry::Type type) { + NetLog::EventType type) { // Negative indices are reverse indices. size_t j = (i < 0) ? entries.size() + i : i; if (j >= entries.size()) return ::testing::AssertionFailure() << j << " is out of bounds."; - const NetLog::Entry& entry = entries[j]; + const CapturingNetLog::Entry& entry = entries[j]; if (entry.type != type) return ::testing::AssertionFailure() << "Type does not match."; return ::testing::AssertionSuccess(); @@ -116,10 +114,9 @@ inline size_t ExpectLogContainsSomewhere( NetLog::EventPhase expected_phase) { size_t i = 0; for (; i < entries.size(); ++i) { - const NetLog::Entry& entry = entries[i]; - if (entry.type == NetLog::Entry::TYPE_EVENT && - entry.event.type == expected_event && - entry.event.phase == expected_phase) + const CapturingNetLog::Entry& entry = entries[i]; + if (entry.type == expected_event && + entry.phase == expected_phase) break; } EXPECT_LT(i, entries.size()); diff --git a/net/base/net_log_util.cc b/net/base/net_log_util.cc index 420fb2d..2bcd907 100644 --- a/net/base/net_log_util.cc +++ b/net/base/net_log_util.cc @@ -13,7 +13,7 @@ namespace { class FormatHelper { public: - std::string ToString(const std::vector<NetLog::Entry>& entries, + std::string ToString(const std::vector<CapturingNetLog::Entry>& entries, size_t num_entries_truncated) { entries_.clear(); @@ -49,6 +49,15 @@ class FormatHelper { int indentation_spaces = entries_[i].indentation * kSpacesPerIndentation; std::string entry_str = GetEntryString(i); + // Hack to better print known event types. + if (entries_[i].log_entry->type == NetLog::TYPE_TODO_STRING || + entries_[i].log_entry->type == NetLog::TYPE_TODO_STRING_LITERAL) { + // Don't display the TODO_STRING type. + entry_str = StringPrintf( + " \"%s\"", + entries_[i].log_entry->extra_parameters->ToString().c_str()); + } + StringAppendF(&result, "t=%s: %s%s", PadStringLeft(GetTimeString(i), max_time_width).c_str(), PadStringLeft("", indentation_spaces).c_str(), @@ -63,6 +72,47 @@ class FormatHelper { PadStringLeft(GetBlockDtString(i), max_dt_width).c_str()); } + // Append any custom parameters. + NetLog::EventParameters* extra_params = + entries_[i].log_entry->extra_parameters; + NetLog::EventType type = entries_[i].log_entry->type; + NetLog::EventPhase phase = entries_[i].log_entry->phase; + + if (type != NetLog::TYPE_TODO_STRING && + type != NetLog::TYPE_TODO_STRING_LITERAL && + extra_params) { + std::string extra_details; + + // Hacks to better print known event types. + if (type == NetLog::TYPE_URL_REQUEST_START || + type == NetLog::TYPE_SOCKET_STREAM_CONNECT) { + if (phase == NetLog::PHASE_BEGIN) { + extra_details = + StringPrintf("url: %s", extra_params->ToString().c_str()); + } else if (phase == NetLog::PHASE_END) { + int error_code = static_cast<NetLogIntegerParameter*>( + extra_params)->value(); + extra_details = StringPrintf("net error: %d (%s)", + error_code, + ErrorToString(error_code)); + } + } else if (type == NetLog::TYPE_SOCKET_POOL_CONNECT_JOB) { + extra_details = + StringPrintf("group: %s", extra_params->ToString().c_str()); + } else { + extra_details = extra_params->ToString(); + } + + int indentation = max_time_width + indentation_spaces + + kSpacesPerIndentation + 5; + + StringAppendF( + &result, + "\n%s%s", + PadStringLeft("", indentation).c_str(), + extra_details.c_str()); + } + if (i + 1 != entries_.size()) result += "\n"; } @@ -72,25 +122,23 @@ class FormatHelper { private: struct Entry { - explicit Entry(const NetLog::Entry* log_entry) + explicit Entry(const CapturingNetLog::Entry* log_entry) : log_entry(log_entry), indentation(0), block_index(-1) {} bool IsBeginEvent() const { - return log_entry->type == NetLog::Entry::TYPE_EVENT && - log_entry->event.phase == NetLog::PHASE_BEGIN; + return log_entry->phase == NetLog::PHASE_BEGIN; } bool IsEndEvent() const { - return log_entry->type == NetLog::Entry::TYPE_EVENT && - log_entry->event.phase == NetLog::PHASE_END; + return log_entry->phase == NetLog::PHASE_END; } - const NetLog::Entry* log_entry; + const CapturingNetLog::Entry* log_entry; size_t indentation; int block_index; // The index of the matching start / end of block. }; - void PopulateEntries(const std::vector<NetLog::Entry>& entries) { + void PopulateEntries(const std::vector<CapturingNetLog::Entry>& entries) { int current_indentation = 0; for (size_t i = 0; i < entries.size(); ++i) { @@ -127,7 +175,7 @@ class FormatHelper { // Find the matching start of block by scanning backwards. for (int i = entries_.size() - 1; i >= 0; --i) { if (entries_[i].IsBeginEvent() && - entries_[i].log_entry->event.type == entry.log_entry->event.type) { + entries_[i].log_entry->type == entry.log_entry->type) { return i; } } @@ -141,7 +189,7 @@ class FormatHelper { *max_time_width = *max_indentation = *max_type_width = *max_dt_width = 0; for (size_t i = 0; i < entries_.size(); ++i) { *max_time_width = std::max(*max_time_width, GetTimeString(i).size()); - if (entries_[i].log_entry->type == NetLog::Entry::TYPE_EVENT) + if (entries_[i].log_entry->phase != NetLog::PHASE_NONE) *max_type_width = std::max(*max_type_width, GetEntryString(i).size()); *max_indentation = std::max(*max_indentation, entries_[i].indentation); @@ -169,36 +217,19 @@ class FormatHelper { } std::string GetEntryString(size_t index) { - const NetLog::Entry* entry = entries_[index].log_entry; + const CapturingNetLog::Entry* entry = entries_[index].log_entry; std::string entry_str; - NetLog::EventPhase phase = NetLog::PHASE_NONE; - switch (entry->type) { - case NetLog::Entry::TYPE_EVENT: - entry_str = NetLog::EventTypeToString(entry->event.type); - phase = entry->event.phase; - - if (phase == NetLog::PHASE_BEGIN && - index + 1 < entries_.size() && - static_cast<size_t>(entries_[index + 1].block_index) == index) { - // If this starts an empty block, we will pretend it is a PHASE_NONE - // so we don't print the "+" prefix. - phase = NetLog::PHASE_NONE; - } - break; - case NetLog::Entry::TYPE_ERROR_CODE: - entry_str = StringPrintf("error code: %d (%s)", - entry->error_code, - ErrorToString(entry->error_code)); - break; - case NetLog::Entry::TYPE_STRING: - entry_str = StringPrintf("\"%s\"", entry->string.c_str()); - break; - case NetLog::Entry::TYPE_STRING_LITERAL: - entry_str = StringPrintf("\"%s\"", entry->literal); - break; - default: - NOTREACHED(); + NetLog::EventPhase phase = entry->phase; + + entry_str = NetLog::EventTypeToString(entry->type); + + if (phase == NetLog::PHASE_BEGIN && + index + 1 < entries_.size() && + static_cast<size_t>(entries_[index + 1].block_index) == index) { + // If this starts an empty block, we will pretend it is a PHASE_NONE + // so we don't print the "+" prefix. + phase = NetLog::PHASE_NONE; } switch (phase) { @@ -228,7 +259,7 @@ class FormatHelper { // static std::string NetLogUtil::PrettyPrintAsEventTree( - const std::vector<NetLog::Entry>& entries, + const std::vector<CapturingNetLog::Entry>& entries, size_t num_entries_truncated) { FormatHelper helper; return helper.ToString(entries, num_entries_truncated); diff --git a/net/base/net_log_util.h b/net/base/net_log_util.h index 716a941..7ca5e65 100644 --- a/net/base/net_log_util.h +++ b/net/base/net_log_util.h @@ -64,7 +64,8 @@ class NetLogUtil { // - Time units are given as milliseconds. // static std::string PrettyPrintAsEventTree( - const std::vector<NetLog::Entry>& entries, size_t num_entries_truncated); + const std::vector<CapturingNetLog::Entry>& entries, + size_t num_entries_truncated); private: DISALLOW_IMPLICIT_CONSTRUCTORS(NetLogUtil); diff --git a/net/base/net_log_util_unittest.cc b/net/base/net_log_util_unittest.cc index 713773d..9306fa9 100644 --- a/net/base/net_log_util_unittest.cc +++ b/net/base/net_log_util_unittest.cc @@ -10,34 +10,26 @@ namespace net { namespace { -NetLog::Entry MakeEventEntry(int t, - NetLog::EventType event_type, - NetLog::EventPhase event_phase) { - NetLog::Entry entry; - entry.type = NetLog::Entry::TYPE_EVENT; - entry.time = MakeTime(t); - entry.event = NetLog::Event(event_type, event_phase); - return entry; +CapturingNetLog::Entry MakeEventEntry(int t, + NetLog::EventType event_type, + NetLog::EventPhase event_phase) { + return CapturingNetLog::Entry(event_type, + MakeTime(t), + NetLog::Source(), + event_phase, + NULL); } -NetLog::Entry MakeStringEntry(int t, const std::string& string) { - NetLog::Entry entry; - entry.type = NetLog::Entry::TYPE_STRING; - entry.time = MakeTime(t); - entry.string = string; - return entry; -} - -NetLog::Entry MakeErrorCodeEntry(int t, int error_code) { - NetLog::Entry entry; - entry.type = NetLog::Entry::TYPE_ERROR_CODE; - entry.time = MakeTime(t); - entry.error_code = error_code; - return entry; +CapturingNetLog::Entry MakeStringEntry(int t, const std::string& string) { + return CapturingNetLog::Entry(NetLog::TYPE_TODO_STRING, + MakeTime(t), + NetLog::Source(), + NetLog::PHASE_NONE, + new NetLogStringParameter(string)); } TEST(NetLogUtilTest, Basic) { - std::vector<NetLog::Entry> log; + CapturingNetLog::EntryList log; log.push_back(MakeEventEntry(1, NetLog::TYPE_HOST_RESOLVER_IMPL, NetLog::PHASE_BEGIN)); @@ -63,13 +55,12 @@ TEST(NetLogUtilTest, Basic) { } TEST(NetLogUtilTest, Basic2) { - std::vector<NetLog::Entry> log; + CapturingNetLog::EntryList log; log.push_back(MakeEventEntry(1, NetLog::TYPE_HOST_RESOLVER_IMPL, NetLog::PHASE_BEGIN)); log.push_back(MakeStringEntry(12, "Sup foo")); - log.push_back(MakeErrorCodeEntry(12, ERR_UNEXPECTED)); log.push_back(MakeStringEntry(14, "Multiline\nString")); log.push_back(MakeEventEntry(131, NetLog::TYPE_HOST_RESOLVER_IMPL, @@ -78,7 +69,6 @@ TEST(NetLogUtilTest, Basic2) { EXPECT_EQ( "t= 1: +HOST_RESOLVER_IMPL [dt=130]\n" "t= 12: \"Sup foo\"\n" - "t= 12: error code: -9 (net::ERR_UNEXPECTED)\n" "t= 14: \"Multiline\n" "String\"\n" "t=131: -HOST_RESOLVER_IMPL", @@ -86,7 +76,7 @@ TEST(NetLogUtilTest, Basic2) { } TEST(NetLogUtilTest, UnmatchedOpen) { - std::vector<NetLog::Entry> log; + CapturingNetLog::EntryList log; log.push_back(MakeEventEntry(3, NetLog::TYPE_HOST_RESOLVER_IMPL, NetLog::PHASE_BEGIN)); @@ -117,7 +107,7 @@ TEST(NetLogUtilTest, UnmatchedOpen) { } TEST(NetLogUtilTest, DisplayOfTruncated) { - std::vector<NetLog::Entry> log; + CapturingNetLog::EntryList log; log.push_back(MakeEventEntry(0, NetLog::TYPE_TCP_CONNECT, |