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

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

Issue 848006: Generalize the net module's LoadLog facility from a passive container, to an event stream (NetLog). (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Split up RequestTracker into ConnectJobTracker+RequestTracker+RequestTrackerBase, address comments Created 10 years, 9 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_util.h ('k') | net/base/net_log_util_unittest.cc » ('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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/load_log_util.h" 5 #include "net/base/net_log_util.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 10
11 namespace net { 11 namespace net {
12 namespace { 12 namespace {
13 13
14 class FormatHelper { 14 class FormatHelper {
15 public: 15 public:
16 std::string ToString(const LoadLog* log) { 16 std::string ToString(const std::vector<NetLog::Entry>& entries,
17 size_t num_entries_truncated) {
17 entries_.clear(); 18 entries_.clear();
18 19
19 // Pass 1: Match the start/end of indentation blocks. Fills |entries_| 20 // Pass 1: Match the start/end of indentation blocks. Fills |entries_|
20 // with the results. 21 // with the results.
21 PopulateEntries(log); 22 PopulateEntries(entries);
22 23
23 // Pass 2: Figure out the maximum width of each column. This allows us 24 // Pass 2: Figure out the maximum width of each column. This allows us
24 // to right-justify text within each column. 25 // to right-justify text within each column.
25 size_t max_time_width, max_indentation, max_type_width, max_dt_width; 26 size_t max_time_width, max_indentation, max_type_width, max_dt_width;
26 GetMaxExtent( 27 GetMaxExtent(
27 &max_time_width, &max_indentation, &max_type_width, &max_dt_width); 28 &max_time_width, &max_indentation, &max_type_width, &max_dt_width);
28 29
29 // Pass 3: Assemble the string. 30 // Pass 3: Assemble the string.
30 std::string result; 31 std::string result;
31 32
32 const int kSpacesPerIndentation = 2; 33 const int kSpacesPerIndentation = 2;
33 34
34 for (size_t i = 0; i < entries_.size(); ++i) { 35 for (size_t i = 0; i < entries_.size(); ++i) {
35 if (log->num_entries_truncated() > 0 && i + 1 == entries_.size()) { 36 if (num_entries_truncated > 0 && i + 1 == entries_.size()) {
36 StringAppendF(&result, " ... Truncated %" PRIuS " entries ...\n", 37 StringAppendF(&result, " ... Truncated %" PRIuS " entries ...\n",
37 log->num_entries_truncated()); 38 num_entries_truncated);
38 } 39 }
39 40
40 if (entries_[i].block_index != -1 && 41 if (entries_[i].block_index != -1 &&
41 static_cast<size_t>(entries_[i].block_index + 1) == i) { 42 static_cast<size_t>(entries_[i].block_index + 1) == i) {
42 // If there were no entries in between the START/END block then don't 43 // If there were no entries in between the START/END block then don't
43 // bother printing a line for END (it just adds noise, and we already 44 // bother printing a line for END (it just adds noise, and we already
44 // show the time delta besides START anyway). 45 // show the time delta besides START anyway).
45 continue; 46 continue;
46 } 47 }
47 48
(...skipping 16 matching lines...) Expand all
64 65
65 if (i + 1 != entries_.size()) 66 if (i + 1 != entries_.size())
66 result += "\n"; 67 result += "\n";
67 } 68 }
68 69
69 return result; 70 return result;
70 } 71 }
71 72
72 private: 73 private:
73 struct Entry { 74 struct Entry {
74 explicit Entry(const LoadLog::Entry* log_entry) 75 explicit Entry(const NetLog::Entry* log_entry)
75 : log_entry(log_entry), indentation(0), block_index(-1) {} 76 : log_entry(log_entry), indentation(0), block_index(-1) {}
76 77
77 bool IsBeginEvent() const { 78 bool IsBeginEvent() const {
78 return log_entry->type == LoadLog::Entry::TYPE_EVENT && 79 return log_entry->type == NetLog::Entry::TYPE_EVENT &&
79 log_entry->event.phase == LoadLog::PHASE_BEGIN; 80 log_entry->event.phase == NetLog::PHASE_BEGIN;
80 } 81 }
81 82
82 bool IsEndEvent() const { 83 bool IsEndEvent() const {
83 return log_entry->type == LoadLog::Entry::TYPE_EVENT && 84 return log_entry->type == NetLog::Entry::TYPE_EVENT &&
84 log_entry->event.phase == LoadLog::PHASE_END; 85 log_entry->event.phase == NetLog::PHASE_END;
85 } 86 }
86 87
87 const LoadLog::Entry* log_entry; 88 const NetLog::Entry* log_entry;
88 size_t indentation; 89 size_t indentation;
89 int block_index; // The index of the matching start / end of block. 90 int block_index; // The index of the matching start / end of block.
90 }; 91 };
91 92
92 void PopulateEntries(const LoadLog* log) { 93 void PopulateEntries(const std::vector<NetLog::Entry>& entries) {
93 int current_indentation = 0; 94 int current_indentation = 0;
94 95
95 for (size_t i = 0; i < log->entries().size(); ++i) { 96 for (size_t i = 0; i < entries.size(); ++i) {
96 Entry entry(&log->entries()[i]); 97 Entry entry(&entries[i]);
97 98
98 entry.indentation = current_indentation; 99 entry.indentation = current_indentation;
99 100
100 if (entry.IsBeginEvent()) { 101 if (entry.IsBeginEvent()) {
101 // Indent everything contained in this block. 102 // Indent everything contained in this block.
102 current_indentation++; 103 current_indentation++;
103 } 104 }
104 105
105 if (entry.IsEndEvent()) { 106 if (entry.IsEndEvent()) {
106 int start_index = FindStartOfBlockIndex(entry); 107 int start_index = FindStartOfBlockIndex(entry);
(...skipping 26 matching lines...) Expand all
133 return -1; // Start not found. 134 return -1; // Start not found.
134 } 135 }
135 136
136 void GetMaxExtent(size_t* max_time_width, 137 void GetMaxExtent(size_t* max_time_width,
137 size_t* max_indentation, 138 size_t* max_indentation,
138 size_t* max_type_width, 139 size_t* max_type_width,
139 size_t* max_dt_width) { 140 size_t* max_dt_width) {
140 *max_time_width = *max_indentation = *max_type_width = *max_dt_width = 0; 141 *max_time_width = *max_indentation = *max_type_width = *max_dt_width = 0;
141 for (size_t i = 0; i < entries_.size(); ++i) { 142 for (size_t i = 0; i < entries_.size(); ++i) {
142 *max_time_width = std::max(*max_time_width, GetTimeString(i).size()); 143 *max_time_width = std::max(*max_time_width, GetTimeString(i).size());
143 if (entries_[i].log_entry->type == LoadLog::Entry::TYPE_EVENT) 144 if (entries_[i].log_entry->type == NetLog::Entry::TYPE_EVENT)
144 *max_type_width = std::max(*max_type_width, GetEntryString(i).size()); 145 *max_type_width = std::max(*max_type_width, GetEntryString(i).size());
145 *max_indentation = std::max(*max_indentation, entries_[i].indentation); 146 *max_indentation = std::max(*max_indentation, entries_[i].indentation);
146 147
147 if (entries_[i].IsBeginEvent()) 148 if (entries_[i].IsBeginEvent())
148 *max_dt_width = std::max(*max_dt_width, GetBlockDtString(i).size()); 149 *max_dt_width = std::max(*max_dt_width, GetBlockDtString(i).size());
149 } 150 }
150 } 151 }
151 152
152 std::string GetBlockDtString(size_t start_index) { 153 std::string GetBlockDtString(size_t start_index) {
153 int end_index = entries_[start_index].block_index; 154 int end_index = entries_[start_index].block_index;
154 if (end_index == -1) { 155 if (end_index == -1) {
155 // Block is not closed, implicitly close it at EOF. 156 // Block is not closed, implicitly close it at EOF.
156 end_index = entries_.size() - 1; 157 end_index = entries_.size() - 1;
157 } 158 }
158 int64 dt_ms = (entries_[end_index].log_entry->time - 159 int64 dt_ms = (entries_[end_index].log_entry->time -
159 entries_[start_index].log_entry->time).InMilliseconds(); 160 entries_[start_index].log_entry->time).InMilliseconds();
160 161
161 return Int64ToString(dt_ms); 162 return Int64ToString(dt_ms);
162 } 163 }
163 164
164 std::string GetTimeString(size_t index) { 165 std::string GetTimeString(size_t index) {
165 int64 t_ms = (entries_[index].log_entry->time - 166 int64 t_ms = (entries_[index].log_entry->time -
166 base::TimeTicks()).InMilliseconds(); 167 base::TimeTicks()).InMilliseconds();
167 return Int64ToString(t_ms); 168 return Int64ToString(t_ms);
168 } 169 }
169 170
170 std::string GetEntryString(size_t index) { 171 std::string GetEntryString(size_t index) {
171 const LoadLog::Entry* entry = entries_[index].log_entry; 172 const NetLog::Entry* entry = entries_[index].log_entry;
172 173
173 std::string entry_str; 174 std::string entry_str;
174 LoadLog::EventPhase phase = LoadLog::PHASE_NONE; 175 NetLog::EventPhase phase = NetLog::PHASE_NONE;
175 switch (entry->type) { 176 switch (entry->type) {
176 case LoadLog::Entry::TYPE_EVENT: 177 case NetLog::Entry::TYPE_EVENT:
177 entry_str = LoadLog::EventTypeToString(entry->event.type); 178 entry_str = NetLog::EventTypeToString(entry->event.type);
178 phase = entry->event.phase; 179 phase = entry->event.phase;
179 180
180 if (phase == LoadLog::PHASE_BEGIN && 181 if (phase == NetLog::PHASE_BEGIN &&
181 index + 1 < entries_.size() && 182 index + 1 < entries_.size() &&
182 static_cast<size_t>(entries_[index + 1].block_index) == index) { 183 static_cast<size_t>(entries_[index + 1].block_index) == index) {
183 // If this starts an empty block, we will pretend it is a PHASE_NONE 184 // If this starts an empty block, we will pretend it is a PHASE_NONE
184 // so we don't print the "+" prefix. 185 // so we don't print the "+" prefix.
185 phase = LoadLog::PHASE_NONE; 186 phase = NetLog::PHASE_NONE;
186 } 187 }
187 break; 188 break;
188 case LoadLog::Entry::TYPE_ERROR_CODE: 189 case NetLog::Entry::TYPE_ERROR_CODE:
189 entry_str = StringPrintf("error code: %d (%s)", 190 entry_str = StringPrintf("error code: %d (%s)",
190 entry->error_code, 191 entry->error_code,
191 ErrorToString(entry->error_code)); 192 ErrorToString(entry->error_code));
192 break; 193 break;
193 case LoadLog::Entry::TYPE_STRING: 194 case NetLog::Entry::TYPE_STRING:
194 entry_str = StringPrintf("\"%s\"", entry->string.c_str()); 195 entry_str = StringPrintf("\"%s\"", entry->string.c_str());
195 break; 196 break;
196 case LoadLog::Entry::TYPE_STRING_LITERAL: 197 case NetLog::Entry::TYPE_STRING_LITERAL:
197 entry_str = StringPrintf("\"%s\"", entry->literal); 198 entry_str = StringPrintf("\"%s\"", entry->literal);
198 break; 199 break;
199 default: 200 default:
200 NOTREACHED(); 201 NOTREACHED();
201 } 202 }
202 203
203 switch (phase) { 204 switch (phase) {
204 case LoadLog::PHASE_BEGIN: 205 case NetLog::PHASE_BEGIN:
205 return std::string("+") + entry_str; 206 return std::string("+") + entry_str;
206 case LoadLog::PHASE_END: 207 case NetLog::PHASE_END:
207 return std::string("-") + entry_str; 208 return std::string("-") + entry_str;
208 case LoadLog::PHASE_NONE: 209 case NetLog::PHASE_NONE:
209 return std::string(" ") + entry_str; 210 return std::string(" ") + entry_str;
210 default: 211 default:
211 NOTREACHED(); 212 NOTREACHED();
212 return std::string(); 213 return std::string();
213 } 214 }
214 } 215 }
215 216
216 static std::string PadStringLeft(const std::string& str, size_t width) { 217 static std::string PadStringLeft(const std::string& str, size_t width) {
217 DCHECK_LE(str.size(), width); 218 DCHECK_LE(str.size(), width);
218 std::string padding; 219 std::string padding;
219 padding.resize(width - str.size(), ' '); 220 padding.resize(width - str.size(), ' ');
220 return padding + str; 221 return padding + str;
221 } 222 }
222 223
223 std::vector<Entry> entries_; 224 std::vector<Entry> entries_;
224 }; 225 };
225 226
226 } // namespace 227 } // namespace
227 228
228 // static 229 // static
229 std::string LoadLogUtil::PrettyPrintAsEventTree(const LoadLog* log) { 230 std::string NetLogUtil::PrettyPrintAsEventTree(
231 const std::vector<NetLog::Entry>& entries,
232 size_t num_entries_truncated) {
230 FormatHelper helper; 233 FormatHelper helper;
231 return helper.ToString(log); 234 return helper.ToString(entries, num_entries_truncated);
232 } 235 }
233 236
234 } // namespace net 237 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_log_util.h ('k') | net/base/net_log_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698