Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: net/base/net_log.cc

Issue 1556018: Add support for attaching custom parameters to NetLog events. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Address willchan's comments Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/net_log.h ('k') | net/base/net_log_event_type_list.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/net_log.h" 5 #include "net/base/net_log.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/string_util.h"
7 8
8 namespace net { 9 namespace net {
9 10
10 // static 11 // static
11 const char* NetLog::EventTypeToString(EventType event) { 12 const char* NetLog::EventTypeToString(EventType event) {
12 switch (event) { 13 switch (event) {
13 #define EVENT_TYPE(label) case TYPE_ ## label: return #label; 14 #define EVENT_TYPE(label) case TYPE_ ## label: return #label;
14 #include "net/base/net_log_event_type_list.h" 15 #include "net/base/net_log_event_type_list.h"
15 #undef EVENT_TYPE 16 #undef EVENT_TYPE
16 } 17 }
17 return NULL; 18 return NULL;
18 } 19 }
19 20
20 // static 21 // static
21 std::vector<NetLog::EventType> NetLog::GetAllEventTypes() { 22 std::vector<NetLog::EventType> NetLog::GetAllEventTypes() {
22 std::vector<NetLog::EventType> types; 23 std::vector<NetLog::EventType> types;
23 #define EVENT_TYPE(label) types.push_back(TYPE_ ## label); 24 #define EVENT_TYPE(label) types.push_back(TYPE_ ## label);
24 #include "net/base/net_log_event_type_list.h" 25 #include "net/base/net_log_event_type_list.h"
25 #undef EVENT_TYPE 26 #undef EVENT_TYPE
26 return types; 27 return types;
27 } 28 }
28 29
29 void BoundNetLog::AddEntry(const NetLog::Entry& entry) const { 30 void BoundNetLog::AddEntry(NetLog::EventType type,
30 if (net_log_) 31 NetLog::EventPhase phase,
31 net_log_->AddEntry(entry); 32 NetLog::EventParameters* extra_parameters) const {
33 if (net_log_) {
34 net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase,
35 extra_parameters);
36 }
37 }
38
39 void BoundNetLog::AddEntryWithTime(
40 NetLog::EventType type,
41 const base::TimeTicks& time,
42 NetLog::EventPhase phase,
43 NetLog::EventParameters* extra_parameters) const {
44 if (net_log_) {
45 net_log_->AddEntry(type, time, source_, phase, extra_parameters);
46 }
32 } 47 }
33 48
34 bool BoundNetLog::HasListener() const { 49 bool BoundNetLog::HasListener() const {
35 if (net_log_) 50 if (net_log_)
36 return net_log_->HasListener(); 51 return net_log_->HasListener();
37 return false; 52 return false;
38 } 53 }
39 54
40 void BoundNetLog::AddEvent(NetLog::EventType event_type) const { 55 void BoundNetLog::AddEvent(NetLog::EventType event_type) const {
41 if (net_log_) { 56 AddEventWithParameters(event_type, NULL);
42 NetLog::Entry entry; 57 }
43 entry.source = source_; 58
44 entry.type = NetLog::Entry::TYPE_EVENT; 59 void BoundNetLog::AddEventWithParameters(
45 entry.time = base::TimeTicks::Now(); 60 NetLog::EventType event_type,
46 entry.event = NetLog::Event(event_type, NetLog::PHASE_NONE); 61 NetLog::EventParameters* params) const {
47 AddEntry(entry); 62 AddEntry(event_type, NetLog::PHASE_NONE, params);
48 } 63 }
64
65 void BoundNetLog::AddEventWithInteger(NetLog::EventType event_type,
66 int integer) const {
67 scoped_refptr<NetLog::EventParameters> params =
68 new NetLogIntegerParameter(integer);
69 AddEventWithParameters(event_type, params);
49 } 70 }
50 71
51 void BoundNetLog::BeginEvent(NetLog::EventType event_type) const { 72 void BoundNetLog::BeginEvent(NetLog::EventType event_type) const {
52 if (net_log_) { 73 BeginEventWithParameters(event_type, NULL);
53 NetLog::Entry entry; 74 }
54 entry.source = source_; 75
55 entry.type = NetLog::Entry::TYPE_EVENT; 76 void BoundNetLog::BeginEventWithParameters(
56 entry.time = base::TimeTicks::Now(); 77 NetLog::EventType event_type,
57 entry.event = NetLog::Event(event_type, NetLog::PHASE_BEGIN); 78 NetLog::EventParameters* params) const {
58 AddEntry(entry); 79 AddEntry(event_type, NetLog::PHASE_BEGIN, params);
59 }
60 } 80 }
61 81
62 void BoundNetLog::BeginEventWithString(NetLog::EventType event_type, 82 void BoundNetLog::BeginEventWithString(NetLog::EventType event_type,
63 const std::string& string) const { 83 const std::string& string) const {
64 NetLog::Entry entry; 84 scoped_refptr<NetLog::EventParameters> params =
65 entry.source = source_; 85 new NetLogStringParameter(string);
66 entry.type = NetLog::Entry::TYPE_EVENT; 86 BeginEventWithParameters(event_type, params);
67 entry.time = base::TimeTicks::Now();
68 entry.event = NetLog::Event(event_type, NetLog::PHASE_BEGIN);
69 entry.string = string;
70 AddEntry(entry);
71 }
72
73 void BoundNetLog::AddEventWithInteger(NetLog::EventType event_type,
74 int integer) const {
75 NetLog::Entry entry;
76 entry.source = source_;
77 entry.type = NetLog::Entry::TYPE_EVENT;
78 entry.time = base::TimeTicks::Now();
79 entry.event = NetLog::Event(event_type, NetLog::PHASE_NONE);
80 entry.error_code = integer;
81 AddEntry(entry);
82 } 87 }
83 88
84 void BoundNetLog::EndEvent(NetLog::EventType event_type) const { 89 void BoundNetLog::EndEvent(NetLog::EventType event_type) const {
85 if (net_log_) { 90 EndEventWithParameters(event_type, NULL);
86 NetLog::Entry entry; 91 }
87 entry.source = source_; 92
88 entry.type = NetLog::Entry::TYPE_EVENT; 93 void BoundNetLog::EndEventWithParameters(
89 entry.time = base::TimeTicks::Now(); 94 NetLog::EventType event_type,
90 entry.event = NetLog::Event(event_type, NetLog::PHASE_END); 95 NetLog::EventParameters* params) const {
91 AddEntry(entry); 96 AddEntry(event_type, NetLog::PHASE_END, params);
92 } 97 }
98
99 void BoundNetLog::EndEventWithInteger(NetLog::EventType event_type,
100 int integer) const {
101 scoped_refptr<NetLog::EventParameters> params =
102 new NetLogIntegerParameter(integer);
103 EndEventWithParameters(event_type, params);
104 }
105
106 void BoundNetLog::AddString(const std::string& string) const {
107 // We pass TYPE_TODO_STRING since we have no event type to associate this
108 // with. (AddString() is deprecated, and should be replaced with
109 // AddEventWithParameters()).
110 scoped_refptr<NetLog::EventParameters> params =
111 new NetLogStringParameter(string);
112 AddEventWithParameters(NetLog::TYPE_TODO_STRING, params);
93 } 113 }
94 114
95 void BoundNetLog::AddStringLiteral(const char* literal) const { 115 void BoundNetLog::AddStringLiteral(const char* literal) const {
96 if (net_log_) { 116 // We pass TYPE_TODO_STRING_LITERAL since we have no event type to associate
97 NetLog::Entry entry; 117 // this with. (AddString() is deprecated, and should be replaced with
98 entry.source = source_; 118 // AddEventWithParameters()).
99 entry.type = NetLog::Entry::TYPE_STRING_LITERAL; 119 scoped_refptr<NetLog::EventParameters> params =
100 entry.time = base::TimeTicks::Now(); 120 new NetLogStringLiteralParameter(literal);
101 entry.literal = literal; 121 AddEventWithParameters(NetLog::TYPE_TODO_STRING_LITERAL, params);
102 AddEntry(entry);
103 }
104 }
105
106 void BoundNetLog::AddString(const std::string& string) const {
107 if (net_log_) {
108 NetLog::Entry entry;
109 entry.source = source_;
110 entry.type = NetLog::Entry::TYPE_STRING;
111 entry.time = base::TimeTicks::Now();
112 entry.string = string;
113 AddEntry(entry);
114 }
115 }
116
117 void BoundNetLog::AddErrorCode(int error) const {
118 if (net_log_) {
119 NetLog::Entry entry;
120 entry.source = source_;
121 entry.type = NetLog::Entry::TYPE_ERROR_CODE;
122 entry.time = base::TimeTicks::Now();
123 entry.error_code = error;
124 AddEntry(entry);
125 }
126 } 122 }
127 123
128 // static 124 // static
129 BoundNetLog BoundNetLog::Make(NetLog* net_log, 125 BoundNetLog BoundNetLog::Make(NetLog* net_log,
130 NetLog::SourceType source_type) { 126 NetLog::SourceType source_type) {
131 if (!net_log) 127 if (!net_log)
132 return BoundNetLog(); 128 return BoundNetLog();
133 129
134 NetLog::Source source(source_type, net_log->NextID()); 130 NetLog::Source source(source_type, net_log->NextID());
135 return BoundNetLog(source, net_log); 131 return BoundNetLog(source, net_log);
136 } 132 }
137 133
138 void CapturingNetLog::AddEntry(const Entry& entry) { 134 NetLogStringParameter::NetLogStringParameter(const std::string& value)
135 : value_(value) {
136 }
137
138 std::string NetLogIntegerParameter::ToString() const {
139 return IntToString(value_);
140 }
141
142 std::string NetLogStringLiteralParameter::ToString() const {
143 return std::string(value_);
144 }
145
146 void CapturingNetLog::AddEntry(EventType type,
147 const base::TimeTicks& time,
148 const Source& source,
149 EventPhase phase,
150 EventParameters* extra_parameters) {
151 Entry entry(type, time, source, phase, extra_parameters);
139 if (entries_.size() + 1 < max_num_entries_) 152 if (entries_.size() + 1 < max_num_entries_)
140 entries_.push_back(entry); 153 entries_.push_back(entry);
141 } 154 }
142 155
143 int CapturingNetLog::NextID() { 156 int CapturingNetLog::NextID() {
144 return next_id_++; 157 return next_id_++;
145 } 158 }
146 159
147 void CapturingNetLog::Clear() { 160 void CapturingNetLog::Clear() {
148 entries_.clear(); 161 entries_.clear();
149 } 162 }
150 163
151 void CapturingBoundNetLog::Clear() { 164 void CapturingBoundNetLog::Clear() {
152 capturing_net_log_->Clear(); 165 capturing_net_log_->Clear();
153 } 166 }
154 167
155 void CapturingBoundNetLog::AppendTo(const BoundNetLog& net_log) const { 168 void CapturingBoundNetLog::AppendTo(const BoundNetLog& net_log) const {
156 for (size_t i = 0; i < entries().size(); ++i) { 169 for (size_t i = 0; i < entries().size(); ++i) {
157 NetLog::Entry entry = entries()[i]; 170 const CapturingNetLog::Entry& entry = entries()[i];
158 entry.source = net_log.source(); 171 net_log.AddEntryWithTime(entry.type, entry.time, entry.phase,
159 net_log.AddEntry(entry); 172 entry.extra_parameters);
160 } 173 }
161 } 174 }
162 175
163 } // namespace net 176 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_log.h ('k') | net/base/net_log_event_type_list.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698