[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/net_log.h" |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 6 | #include "base/logging.h" |
| 7 | #include "base/string_number_conversions.h" |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 8 | #include "base/time.h" |
[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 9 | #include "base/utf_string_conversions.h" |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 10 | #include "base/values.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 11 | |
| 12 | namespace net { |
| 13 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 14 | Value* NetLog::Source::ToValue() const { |
| 15 | DictionaryValue* dict = new DictionaryValue(); |
| 16 | dict->SetInteger("type", static_cast<int>(type)); |
| 17 | dict->SetInteger("id", static_cast<int>(id)); |
| 18 | return dict; |
| 19 | } |
| 20 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 21 | // static |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 22 | std::string NetLog::TickCountToString(const base::TimeTicks& time) { |
| 23 | int64 delta_time = (time - base::TimeTicks()).InMilliseconds(); |
| 24 | return base::Int64ToString(delta_time); |
| 25 | } |
| 26 | |
| 27 | // static |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 28 | const char* NetLog::EventTypeToString(EventType event) { |
| 29 | switch (event) { |
| 30 | #define EVENT_TYPE(label) case TYPE_ ## label: return #label; |
| 31 | #include "net/base/net_log_event_type_list.h" |
| 32 | #undef EVENT_TYPE |
| 33 | } |
| 34 | return NULL; |
| 35 | } |
| 36 | |
[email protected] | fd9c0d9 | 2010-03-23 17:47:49 | [diff] [blame] | 37 | // static |
| 38 | std::vector<NetLog::EventType> NetLog::GetAllEventTypes() { |
| 39 | std::vector<NetLog::EventType> types; |
| 40 | #define EVENT_TYPE(label) types.push_back(TYPE_ ## label); |
| 41 | #include "net/base/net_log_event_type_list.h" |
| 42 | #undef EVENT_TYPE |
| 43 | return types; |
| 44 | } |
| 45 | |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 46 | // static |
| 47 | const char* NetLog::SourceTypeToString(SourceType source) { |
| 48 | switch (source) { |
| 49 | #define SOURCE_TYPE(label, id) case id: return #label; |
| 50 | #include "net/base/net_log_source_type_list.h" |
| 51 | #undef SOURCE_TYPE |
| 52 | } |
| 53 | NOTREACHED(); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | // static |
| 58 | const char* NetLog::EventPhaseToString(EventPhase phase) { |
| 59 | switch (phase) { |
| 60 | case PHASE_BEGIN: |
| 61 | return "PHASE_BEGIN"; |
| 62 | case PHASE_END: |
| 63 | return "PHASE_END"; |
| 64 | case PHASE_NONE: |
| 65 | return "PHASE_NONE"; |
| 66 | } |
| 67 | NOTREACHED(); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | // static |
| 72 | Value* NetLog::EntryToDictionaryValue(net::NetLog::EventType type, |
| 73 | const base::TimeTicks& time, |
| 74 | const net::NetLog::Source& source, |
| 75 | net::NetLog::EventPhase phase, |
| 76 | net::NetLog::EventParameters* params, |
| 77 | bool use_strings) { |
| 78 | DictionaryValue* entry_dict = new DictionaryValue(); |
| 79 | |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 80 | entry_dict->SetString("time", TickCountToString(time)); |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 81 | |
| 82 | // Set the entry source. |
| 83 | DictionaryValue* source_dict = new DictionaryValue(); |
| 84 | source_dict->SetInteger("id", source.id); |
| 85 | if (!use_strings) { |
| 86 | source_dict->SetInteger("type", static_cast<int>(source.type)); |
| 87 | } else { |
| 88 | source_dict->SetString("type", |
| 89 | net::NetLog::SourceTypeToString(source.type)); |
| 90 | } |
| 91 | entry_dict->Set("source", source_dict); |
| 92 | |
| 93 | // Set the event info. |
| 94 | if (!use_strings) { |
| 95 | entry_dict->SetInteger("type", static_cast<int>(type)); |
| 96 | entry_dict->SetInteger("phase", static_cast<int>(phase)); |
| 97 | } else { |
| 98 | entry_dict->SetString("type", net::NetLog::EventTypeToString(type)); |
| 99 | entry_dict->SetString("phase", net::NetLog::EventPhaseToString(phase)); |
| 100 | } |
| 101 | |
| 102 | // Set the event-specific parameters. |
| 103 | if (params) |
| 104 | entry_dict->Set("params", params->ToValue()); |
| 105 | |
| 106 | return entry_dict; |
| 107 | } |
| 108 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 109 | void BoundNetLog::AddEntry( |
| 110 | NetLog::EventType type, |
| 111 | NetLog::EventPhase phase, |
| 112 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 113 | if (net_log_) { |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 114 | net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase, params); |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | void BoundNetLog::AddEntryWithTime( |
| 119 | NetLog::EventType type, |
| 120 | const base::TimeTicks& time, |
| 121 | NetLog::EventPhase phase, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 122 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 123 | if (net_log_) { |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 124 | net_log_->AddEntry(type, time, source_, phase, params); |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 125 | } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 126 | } |
| 127 | |
[email protected] | 095c7cf | 2010-08-31 21:07:33 | [diff] [blame] | 128 | NetLog::LogLevel BoundNetLog::GetLogLevel() const { |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 129 | if (net_log_) |
[email protected] | 095c7cf | 2010-08-31 21:07:33 | [diff] [blame] | 130 | return net_log_->GetLogLevel(); |
| 131 | return NetLog::LOG_BASIC; |
| 132 | } |
| 133 | |
[email protected] | 465aeb94 | 2010-10-14 19:58:14 | [diff] [blame] | 134 | bool BoundNetLog::IsLoggingBytes() const { |
[email protected] | 095c7cf | 2010-08-31 21:07:33 | [diff] [blame] | 135 | return GetLogLevel() == NetLog::LOG_ALL; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 465aeb94 | 2010-10-14 19:58:14 | [diff] [blame] | 138 | bool BoundNetLog::IsLoggingAllEvents() const { |
| 139 | return GetLogLevel() <= NetLog::LOG_ALL_BUT_BYTES; |
| 140 | } |
| 141 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 142 | void BoundNetLog::AddEvent( |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 143 | NetLog::EventType event_type, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 144 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 145 | AddEntry(event_type, NetLog::PHASE_NONE, params); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 148 | void BoundNetLog::BeginEvent( |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 149 | NetLog::EventType event_type, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 150 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 151 | AddEntry(event_type, NetLog::PHASE_BEGIN, params); |
| 152 | } |
| 153 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 154 | void BoundNetLog::EndEvent( |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 155 | NetLog::EventType event_type, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 156 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 157 | AddEntry(event_type, NetLog::PHASE_END, params); |
| 158 | } |
| 159 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 160 | // static |
| 161 | BoundNetLog BoundNetLog::Make(NetLog* net_log, |
| 162 | NetLog::SourceType source_type) { |
| 163 | if (!net_log) |
| 164 | return BoundNetLog(); |
| 165 | |
| 166 | NetLog::Source source(source_type, net_log->NextID()); |
| 167 | return BoundNetLog(source, net_log); |
| 168 | } |
| 169 | |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 170 | NetLogStringParameter::NetLogStringParameter(const char* name, |
| 171 | const std::string& value) |
| 172 | : name_(name), value_(value) { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 173 | } |
| 174 | |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 175 | NetLogStringParameter::~NetLogStringParameter() { |
| 176 | } |
| 177 | |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 178 | Value* NetLogIntegerParameter::ToValue() const { |
| 179 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 180 | dict->SetInteger(name_, value_); |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 181 | return dict; |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 184 | Value* NetLogStringParameter::ToValue() const { |
| 185 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 186 | dict->SetString(name_, value_); |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 187 | return dict; |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 190 | Value* NetLogSourceParameter::ToValue() const { |
| 191 | DictionaryValue* dict = new DictionaryValue(); |
| 192 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 193 | dict->Set(name_, value_.ToValue()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 194 | return dict; |
| 195 | } |
| 196 | |
[email protected] | e8188fe | 2010-12-09 17:33:21 | [diff] [blame^] | 197 | ScopedNetLogEvent::ScopedNetLogEvent( |
| 198 | const BoundNetLog& net_log, |
| 199 | NetLog::EventType event_type, |
| 200 | const scoped_refptr<NetLog::EventParameters>& params) |
| 201 | : net_log_(net_log), |
| 202 | event_type_(event_type) { |
| 203 | net_log_.BeginEvent(event_type, params); |
| 204 | } |
| 205 | |
| 206 | ScopedNetLogEvent::~ScopedNetLogEvent() { |
| 207 | net_log_.EndEvent(event_type_, end_event_params_); |
| 208 | } |
| 209 | |
| 210 | void ScopedNetLogEvent::SetEndEventParameters( |
| 211 | const scoped_refptr<NetLog::EventParameters>& end_event_params) { |
| 212 | DCHECK(!end_event_params_.get()); |
| 213 | end_event_params_ = end_event_params; |
| 214 | } |
| 215 | |
| 216 | const BoundNetLog& ScopedNetLogEvent::net_log() const { |
| 217 | return net_log_; |
| 218 | } |
| 219 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 220 | } // namespace net |