| OLD | NEW |
| 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/net_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/json/json_writer.h" |
| 8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/values.h" |
| 9 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 10 | 12 |
| 11 namespace net { | 13 namespace net { |
| 12 namespace { | 14 namespace { |
| 13 | 15 |
| 14 class FormatHelper { | 16 class FormatHelper { |
| 15 public: | 17 public: |
| 16 std::string ToString(const std::vector<CapturingNetLog::Entry>& entries, | 18 std::string ToString(const std::vector<CapturingNetLog::Entry>& entries, |
| 17 size_t num_entries_truncated) { | 19 size_t num_entries_truncated) { |
| 18 entries_.clear(); | 20 entries_.clear(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 42 static_cast<size_t>(entries_[i].block_index + 1) == i) { | 44 static_cast<size_t>(entries_[i].block_index + 1) == i) { |
| 43 // If there were no entries in between the START/END block then don't | 45 // If there were no entries in between the START/END block then don't |
| 44 // bother printing a line for END (it just adds noise, and we already | 46 // bother printing a line for END (it just adds noise, and we already |
| 45 // show the time delta besides START anyway). | 47 // show the time delta besides START anyway). |
| 46 continue; | 48 continue; |
| 47 } | 49 } |
| 48 | 50 |
| 49 int indentation_spaces = entries_[i].indentation * kSpacesPerIndentation; | 51 int indentation_spaces = entries_[i].indentation * kSpacesPerIndentation; |
| 50 std::string entry_str = GetEntryString(i); | 52 std::string entry_str = GetEntryString(i); |
| 51 | 53 |
| 52 // Hack to better print known event types. | |
| 53 if (entries_[i].log_entry->type == NetLog::TYPE_TODO_STRING || | |
| 54 entries_[i].log_entry->type == NetLog::TYPE_TODO_STRING_LITERAL) { | |
| 55 // Don't display the TODO_STRING type. | |
| 56 entry_str = StringPrintf( | |
| 57 " \"%s\"", | |
| 58 entries_[i].log_entry->extra_parameters->ToString().c_str()); | |
| 59 } | |
| 60 | |
| 61 StringAppendF(&result, "t=%s: %s%s", | 54 StringAppendF(&result, "t=%s: %s%s", |
| 62 PadStringLeft(GetTimeString(i), max_time_width).c_str(), | 55 PadStringLeft(GetTimeString(i), max_time_width).c_str(), |
| 63 PadStringLeft("", indentation_spaces).c_str(), | 56 PadStringLeft("", indentation_spaces).c_str(), |
| 64 entry_str.c_str()); | 57 entry_str.c_str()); |
| 65 | 58 |
| 66 if (entries_[i].IsBeginEvent()) { | 59 if (entries_[i].IsBeginEvent()) { |
| 67 // Summarize how long this block lasted. | 60 // Summarize how long this block lasted. |
| 68 int padding = ((max_indentation - entries_[i].indentation) * | 61 int padding = ((max_indentation - entries_[i].indentation) * |
| 69 kSpacesPerIndentation) + (max_type_width - entry_str.size()); | 62 kSpacesPerIndentation) + (max_type_width - entry_str.size()); |
| 70 StringAppendF(&result, "%s [dt=%s]", | 63 StringAppendF(&result, "%s [dt=%s]", |
| 71 PadStringLeft("", padding).c_str(), | 64 PadStringLeft("", padding).c_str(), |
| 72 PadStringLeft(GetBlockDtString(i), max_dt_width).c_str()); | 65 PadStringLeft(GetBlockDtString(i), max_dt_width).c_str()); |
| 73 } | 66 } |
| 74 | 67 |
| 75 // Append any custom parameters. | 68 // Append any custom parameters. |
| 76 NetLog::EventParameters* extra_params = | 69 NetLog::EventParameters* extra_params = |
| 77 entries_[i].log_entry->extra_parameters; | 70 entries_[i].log_entry->extra_parameters; |
| 78 NetLog::EventType type = entries_[i].log_entry->type; | |
| 79 NetLog::EventPhase phase = entries_[i].log_entry->phase; | |
| 80 | 71 |
| 81 if (type != NetLog::TYPE_TODO_STRING && | 72 if (extra_params) { |
| 82 type != NetLog::TYPE_TODO_STRING_LITERAL && | |
| 83 extra_params) { | |
| 84 std::string extra_details; | 73 std::string extra_details; |
| 85 | 74 scoped_ptr<Value> extra_details_value(extra_params->ToValue()); |
| 86 // Hacks to better print known event types. | 75 base::JSONWriter::Write(extra_details_value.get(), true, |
| 87 if (type == NetLog::TYPE_URL_REQUEST_START || | 76 &extra_details); |
| 88 type == NetLog::TYPE_SOCKET_STREAM_CONNECT) { | 77 // JSON writer uses CR LF in its pretty-printer. Normalize to newlines. |
| 89 if (phase == NetLog::PHASE_BEGIN) { | 78 ReplaceSubstringsAfterOffset(&extra_details, 0, "\r\n", "\n"); |
| 90 extra_details = | 79 result.append("\n"); |
| 91 StringPrintf("url: %s", extra_params->ToString().c_str()); | 80 result.append(extra_details); |
| 92 } else if (phase == NetLog::PHASE_END) { | |
| 93 int error_code = static_cast<NetLogIntegerParameter*>( | |
| 94 extra_params)->value(); | |
| 95 extra_details = StringPrintf("net error: %d (%s)", | |
| 96 error_code, | |
| 97 ErrorToString(error_code)); | |
| 98 } | |
| 99 } else if (type == NetLog::TYPE_SOCKET_POOL_CONNECT_JOB) { | |
| 100 extra_details = | |
| 101 StringPrintf("group: %s", extra_params->ToString().c_str()); | |
| 102 } else { | |
| 103 extra_details = extra_params->ToString(); | |
| 104 } | |
| 105 | |
| 106 int indentation = max_time_width + indentation_spaces + | |
| 107 kSpacesPerIndentation + 5; | |
| 108 | |
| 109 StringAppendF( | |
| 110 &result, | |
| 111 "\n%s%s", | |
| 112 PadStringLeft("", indentation).c_str(), | |
| 113 extra_details.c_str()); | |
| 114 } | 81 } |
| 115 | 82 |
| 116 if (i + 1 != entries_.size()) | 83 if (!extra_params && i + 1 != entries_.size()) |
| 117 result += "\n"; | 84 result += "\n"; |
| 118 } | 85 } |
| 119 | 86 |
| 120 return result; | 87 return result; |
| 121 } | 88 } |
| 122 | 89 |
| 123 private: | 90 private: |
| 124 struct Entry { | 91 struct Entry { |
| 125 explicit Entry(const CapturingNetLog::Entry* log_entry) | 92 explicit Entry(const CapturingNetLog::Entry* log_entry) |
| 126 : log_entry(log_entry), indentation(0), block_index(-1) {} | 93 : log_entry(log_entry), indentation(0), block_index(-1) {} |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 | 226 |
| 260 // static | 227 // static |
| 261 std::string NetLogUtil::PrettyPrintAsEventTree( | 228 std::string NetLogUtil::PrettyPrintAsEventTree( |
| 262 const std::vector<CapturingNetLog::Entry>& entries, | 229 const std::vector<CapturingNetLog::Entry>& entries, |
| 263 size_t num_entries_truncated) { | 230 size_t num_entries_truncated) { |
| 264 FormatHelper helper; | 231 FormatHelper helper; |
| 265 return helper.ToString(entries, num_entries_truncated); | 232 return helper.ToString(entries, num_entries_truncated); |
| 266 } | 233 } |
| 267 | 234 |
| 268 } // namespace net | 235 } // namespace net |
| OLD | NEW |