[email protected] | 988394e | 2012-02-22 16:37:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 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] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 6 | |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 7 | #include "base/logging.h" |
| 8 | #include "base/string_number_conversions.h" |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 9 | #include "base/time.h" |
[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 11 | #include "base/values.h" |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 12 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 13 | |
| 14 | namespace net { |
| 15 | |
[email protected] | 267a0d66d | 2011-06-01 21:15:19 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | // Parameters for logging data transferred events. Includes bytes transferred |
| 19 | // and, if |bytes| is not NULL, the bytes themselves. |
| 20 | class NetLogBytesTransferredParameter : public NetLog::EventParameters { |
| 21 | public: |
| 22 | NetLogBytesTransferredParameter(int byte_count, const char* bytes); |
| 23 | |
| 24 | virtual Value* ToValue() const; |
| 25 | |
| 26 | private: |
| 27 | const int byte_count_; |
| 28 | std::string hex_encoded_bytes_; |
| 29 | bool has_bytes_; |
| 30 | }; |
| 31 | |
| 32 | NetLogBytesTransferredParameter::NetLogBytesTransferredParameter( |
| 33 | int byte_count, const char* transferred_bytes) |
| 34 | : byte_count_(byte_count), |
| 35 | has_bytes_(false) { |
| 36 | if (transferred_bytes) { |
| 37 | hex_encoded_bytes_ = base::HexEncode(transferred_bytes, byte_count); |
| 38 | has_bytes_ = true; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | Value* NetLogBytesTransferredParameter::ToValue() const { |
| 43 | DictionaryValue* dict = new DictionaryValue(); |
| 44 | dict->SetInteger("byte_count", byte_count_); |
| 45 | if (has_bytes_ && byte_count_ > 0) |
| 46 | dict->SetString("hex_encoded_bytes", hex_encoded_bytes_); |
| 47 | return dict; |
| 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 52 | Value* NetLog::Source::ToValue() const { |
| 53 | DictionaryValue* dict = new DictionaryValue(); |
| 54 | dict->SetInteger("type", static_cast<int>(type)); |
| 55 | dict->SetInteger("id", static_cast<int>(id)); |
| 56 | return dict; |
| 57 | } |
| 58 | |
[email protected] | ae6e991 | 2011-07-27 01:18:28 | [diff] [blame] | 59 | NetLog::ThreadSafeObserver::ThreadSafeObserver(LogLevel log_level) |
| 60 | : log_level_(log_level) { |
| 61 | } |
| 62 | |
| 63 | NetLog::ThreadSafeObserver::~ThreadSafeObserver() { |
| 64 | } |
| 65 | |
| 66 | NetLog::LogLevel NetLog::ThreadSafeObserver::log_level() const { |
| 67 | return log_level_; |
| 68 | } |
| 69 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 70 | // static |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 71 | std::string NetLog::TickCountToString(const base::TimeTicks& time) { |
| 72 | int64 delta_time = (time - base::TimeTicks()).InMilliseconds(); |
| 73 | return base::Int64ToString(delta_time); |
| 74 | } |
| 75 | |
| 76 | // static |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 77 | const char* NetLog::EventTypeToString(EventType event) { |
| 78 | switch (event) { |
| 79 | #define EVENT_TYPE(label) case TYPE_ ## label: return #label; |
| 80 | #include "net/base/net_log_event_type_list.h" |
| 81 | #undef EVENT_TYPE |
[email protected] | 988394e | 2012-02-22 16:37:55 | [diff] [blame] | 82 | default: |
| 83 | NOTREACHED(); |
| 84 | return NULL; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 85 | } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | fd9c0d9 | 2010-03-23 17:47:49 | [diff] [blame] | 88 | // static |
[email protected] | 988394e | 2012-02-22 16:37:55 | [diff] [blame] | 89 | base::Value* NetLog::GetEventTypesAsValue() { |
| 90 | DictionaryValue* dict = new DictionaryValue(); |
| 91 | for (int i = 0; i < EVENT_COUNT; ++i) { |
| 92 | dict->SetInteger(EventTypeToString(static_cast<EventType>(i)), i); |
| 93 | } |
| 94 | return dict; |
[email protected] | fd9c0d9 | 2010-03-23 17:47:49 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 97 | // static |
| 98 | const char* NetLog::SourceTypeToString(SourceType source) { |
| 99 | switch (source) { |
[email protected] | 988394e | 2012-02-22 16:37:55 | [diff] [blame] | 100 | #define SOURCE_TYPE(label) case SOURCE_ ## label: return #label; |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 101 | #include "net/base/net_log_source_type_list.h" |
| 102 | #undef SOURCE_TYPE |
[email protected] | 988394e | 2012-02-22 16:37:55 | [diff] [blame] | 103 | default: |
| 104 | NOTREACHED(); |
| 105 | return NULL; |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 106 | } |
[email protected] | 988394e | 2012-02-22 16:37:55 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // static |
| 110 | base::Value* NetLog::GetSourceTypesAsValue() { |
| 111 | DictionaryValue* dict = new DictionaryValue(); |
| 112 | for (int i = 0; i < SOURCE_COUNT; ++i) { |
| 113 | dict->SetInteger(SourceTypeToString(static_cast<SourceType>(i)), i); |
| 114 | } |
| 115 | return dict; |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // static |
| 119 | const char* NetLog::EventPhaseToString(EventPhase phase) { |
| 120 | switch (phase) { |
| 121 | case PHASE_BEGIN: |
| 122 | return "PHASE_BEGIN"; |
| 123 | case PHASE_END: |
| 124 | return "PHASE_END"; |
| 125 | case PHASE_NONE: |
| 126 | return "PHASE_NONE"; |
| 127 | } |
| 128 | NOTREACHED(); |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | // static |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 133 | Value* NetLog::EntryToDictionaryValue(NetLog::EventType type, |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 134 | const base::TimeTicks& time, |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 135 | const NetLog::Source& source, |
| 136 | NetLog::EventPhase phase, |
| 137 | NetLog::EventParameters* params, |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 138 | bool use_strings) { |
| 139 | DictionaryValue* entry_dict = new DictionaryValue(); |
| 140 | |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 141 | entry_dict->SetString("time", TickCountToString(time)); |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 142 | |
| 143 | // Set the entry source. |
| 144 | DictionaryValue* source_dict = new DictionaryValue(); |
| 145 | source_dict->SetInteger("id", source.id); |
| 146 | if (!use_strings) { |
| 147 | source_dict->SetInteger("type", static_cast<int>(source.type)); |
| 148 | } else { |
| 149 | source_dict->SetString("type", |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 150 | NetLog::SourceTypeToString(source.type)); |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 151 | } |
| 152 | entry_dict->Set("source", source_dict); |
| 153 | |
| 154 | // Set the event info. |
| 155 | if (!use_strings) { |
| 156 | entry_dict->SetInteger("type", static_cast<int>(type)); |
| 157 | entry_dict->SetInteger("phase", static_cast<int>(phase)); |
| 158 | } else { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 159 | entry_dict->SetString("type", NetLog::EventTypeToString(type)); |
| 160 | entry_dict->SetString("phase", NetLog::EventPhaseToString(phase)); |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Set the event-specific parameters. |
| 164 | if (params) |
| 165 | entry_dict->Set("params", params->ToValue()); |
| 166 | |
| 167 | return entry_dict; |
| 168 | } |
| 169 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 170 | void BoundNetLog::AddEntry( |
| 171 | NetLog::EventType type, |
| 172 | NetLog::EventPhase phase, |
| 173 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 174 | if (net_log_) { |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 175 | net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase, params); |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | void BoundNetLog::AddEntryWithTime( |
| 180 | NetLog::EventType type, |
| 181 | const base::TimeTicks& time, |
| 182 | NetLog::EventPhase phase, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 183 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 184 | if (net_log_) { |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 185 | net_log_->AddEntry(type, time, source_, phase, params); |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 186 | } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 189 | void BoundNetLog::AddEvent( |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 190 | NetLog::EventType event_type, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 191 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 192 | AddEntry(event_type, NetLog::PHASE_NONE, params); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 195 | void BoundNetLog::BeginEvent( |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 196 | NetLog::EventType event_type, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 197 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 198 | AddEntry(event_type, NetLog::PHASE_BEGIN, params); |
| 199 | } |
| 200 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 201 | void BoundNetLog::EndEvent( |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 202 | NetLog::EventType event_type, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 203 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 204 | AddEntry(event_type, NetLog::PHASE_END, params); |
| 205 | } |
| 206 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 207 | void BoundNetLog::AddEventWithNetErrorCode(NetLog::EventType event_type, |
| 208 | int net_error) const { |
| 209 | DCHECK_GT(0, net_error); |
| 210 | DCHECK_NE(ERR_IO_PENDING, net_error); |
| 211 | AddEvent( |
| 212 | event_type, |
| 213 | make_scoped_refptr(new NetLogIntegerParameter("net_error", net_error))); |
| 214 | } |
| 215 | |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 216 | void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type, |
| 217 | int net_error) const { |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 218 | DCHECK_NE(ERR_IO_PENDING, net_error); |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 219 | if (net_error >= 0) { |
| 220 | EndEvent(event_type, NULL); |
| 221 | } else { |
| 222 | EndEvent( |
| 223 | event_type, |
| 224 | make_scoped_refptr(new NetLogIntegerParameter("net_error", net_error))); |
| 225 | } |
| 226 | } |
| 227 | |
[email protected] | 267a0d66d | 2011-06-01 21:15:19 | [diff] [blame] | 228 | void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type, |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 229 | int byte_count, |
| 230 | const char* bytes) const { |
[email protected] | 267a0d66d | 2011-06-01 21:15:19 | [diff] [blame] | 231 | scoped_refptr<NetLog::EventParameters> params; |
| 232 | if (IsLoggingBytes()) { |
| 233 | params = new NetLogBytesTransferredParameter(byte_count, bytes); |
| 234 | } else { |
| 235 | params = new NetLogBytesTransferredParameter(byte_count, NULL); |
| 236 | } |
| 237 | AddEvent(event_type, params); |
| 238 | } |
| 239 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 240 | NetLog::LogLevel BoundNetLog::GetLogLevel() const { |
| 241 | if (net_log_) |
| 242 | return net_log_->GetLogLevel(); |
| 243 | return NetLog::LOG_BASIC; |
| 244 | } |
| 245 | |
| 246 | bool BoundNetLog::IsLoggingBytes() const { |
| 247 | return GetLogLevel() == NetLog::LOG_ALL; |
| 248 | } |
| 249 | |
| 250 | bool BoundNetLog::IsLoggingAllEvents() const { |
| 251 | return GetLogLevel() <= NetLog::LOG_ALL_BUT_BYTES; |
| 252 | } |
| 253 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 254 | // static |
| 255 | BoundNetLog BoundNetLog::Make(NetLog* net_log, |
| 256 | NetLog::SourceType source_type) { |
| 257 | if (!net_log) |
| 258 | return BoundNetLog(); |
| 259 | |
| 260 | NetLog::Source source(source_type, net_log->NextID()); |
| 261 | return BoundNetLog(source, net_log); |
| 262 | } |
| 263 | |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 264 | NetLogStringParameter::NetLogStringParameter(const char* name, |
| 265 | const std::string& value) |
| 266 | : name_(name), value_(value) { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 267 | } |
| 268 | |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 269 | NetLogStringParameter::~NetLogStringParameter() { |
| 270 | } |
| 271 | |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 272 | Value* NetLogIntegerParameter::ToValue() const { |
| 273 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 274 | dict->SetInteger(name_, value_); |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 275 | return dict; |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 276 | } |
| 277 | |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 278 | Value* NetLogStringParameter::ToValue() const { |
| 279 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 280 | dict->SetString(name_, value_); |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 281 | return dict; |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 282 | } |
| 283 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 284 | Value* NetLogSourceParameter::ToValue() const { |
| 285 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | 68697a4 | 2011-12-19 21:47:55 | [diff] [blame] | 286 | if (value_.is_valid()) |
| 287 | dict->Set(name_, value_.ToValue()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 288 | return dict; |
| 289 | } |
| 290 | |
[email protected] | e8188fe | 2010-12-09 17:33:21 | [diff] [blame] | 291 | ScopedNetLogEvent::ScopedNetLogEvent( |
| 292 | const BoundNetLog& net_log, |
| 293 | NetLog::EventType event_type, |
| 294 | const scoped_refptr<NetLog::EventParameters>& params) |
| 295 | : net_log_(net_log), |
| 296 | event_type_(event_type) { |
| 297 | net_log_.BeginEvent(event_type, params); |
| 298 | } |
| 299 | |
| 300 | ScopedNetLogEvent::~ScopedNetLogEvent() { |
| 301 | net_log_.EndEvent(event_type_, end_event_params_); |
| 302 | } |
| 303 | |
| 304 | void ScopedNetLogEvent::SetEndEventParameters( |
| 305 | const scoped_refptr<NetLog::EventParameters>& end_event_params) { |
| 306 | DCHECK(!end_event_params_.get()); |
| 307 | end_event_params_ = end_event_params; |
| 308 | } |
| 309 | |
| 310 | const BoundNetLog& ScopedNetLogEvent::net_log() const { |
| 311 | return net_log_; |
| 312 | } |
| 313 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 314 | } // namespace net |