[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 1 | // Copyright (c) 2011 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 |
| 82 | } |
| 83 | return NULL; |
| 84 | } |
| 85 | |
[email protected] | fd9c0d9 | 2010-03-23 17:47:49 | [diff] [blame] | 86 | // static |
| 87 | std::vector<NetLog::EventType> NetLog::GetAllEventTypes() { |
| 88 | std::vector<NetLog::EventType> types; |
| 89 | #define EVENT_TYPE(label) types.push_back(TYPE_ ## label); |
| 90 | #include "net/base/net_log_event_type_list.h" |
| 91 | #undef EVENT_TYPE |
| 92 | return types; |
| 93 | } |
| 94 | |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 95 | // static |
| 96 | const char* NetLog::SourceTypeToString(SourceType source) { |
| 97 | switch (source) { |
| 98 | #define SOURCE_TYPE(label, id) case id: return #label; |
| 99 | #include "net/base/net_log_source_type_list.h" |
| 100 | #undef SOURCE_TYPE |
| 101 | } |
| 102 | NOTREACHED(); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | // static |
| 107 | const char* NetLog::EventPhaseToString(EventPhase phase) { |
| 108 | switch (phase) { |
| 109 | case PHASE_BEGIN: |
| 110 | return "PHASE_BEGIN"; |
| 111 | case PHASE_END: |
| 112 | return "PHASE_END"; |
| 113 | case PHASE_NONE: |
| 114 | return "PHASE_NONE"; |
| 115 | } |
| 116 | NOTREACHED(); |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | // static |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 121 | Value* NetLog::EntryToDictionaryValue(NetLog::EventType type, |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 122 | const base::TimeTicks& time, |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 123 | const NetLog::Source& source, |
| 124 | NetLog::EventPhase phase, |
| 125 | NetLog::EventParameters* params, |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 126 | bool use_strings) { |
| 127 | DictionaryValue* entry_dict = new DictionaryValue(); |
| 128 | |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 129 | entry_dict->SetString("time", TickCountToString(time)); |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 130 | |
| 131 | // Set the entry source. |
| 132 | DictionaryValue* source_dict = new DictionaryValue(); |
| 133 | source_dict->SetInteger("id", source.id); |
| 134 | if (!use_strings) { |
| 135 | source_dict->SetInteger("type", static_cast<int>(source.type)); |
| 136 | } else { |
| 137 | source_dict->SetString("type", |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 138 | NetLog::SourceTypeToString(source.type)); |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 139 | } |
| 140 | entry_dict->Set("source", source_dict); |
| 141 | |
| 142 | // Set the event info. |
| 143 | if (!use_strings) { |
| 144 | entry_dict->SetInteger("type", static_cast<int>(type)); |
| 145 | entry_dict->SetInteger("phase", static_cast<int>(phase)); |
| 146 | } else { |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 147 | entry_dict->SetString("type", NetLog::EventTypeToString(type)); |
| 148 | entry_dict->SetString("phase", NetLog::EventPhaseToString(phase)); |
[email protected] | 9319035 | 2010-08-13 13:55:10 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Set the event-specific parameters. |
| 152 | if (params) |
| 153 | entry_dict->Set("params", params->ToValue()); |
| 154 | |
| 155 | return entry_dict; |
| 156 | } |
| 157 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 158 | void BoundNetLog::AddEntry( |
| 159 | NetLog::EventType type, |
| 160 | NetLog::EventPhase phase, |
| 161 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 162 | if (net_log_) { |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 163 | net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase, params); |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | void BoundNetLog::AddEntryWithTime( |
| 168 | NetLog::EventType type, |
| 169 | const base::TimeTicks& time, |
| 170 | NetLog::EventPhase phase, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 171 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 172 | if (net_log_) { |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 173 | net_log_->AddEntry(type, time, source_, phase, params); |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 174 | } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 177 | void BoundNetLog::AddEvent( |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 178 | NetLog::EventType event_type, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 179 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 180 | AddEntry(event_type, NetLog::PHASE_NONE, params); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 183 | void BoundNetLog::BeginEvent( |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 184 | NetLog::EventType event_type, |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 185 | const scoped_refptr<NetLog::EventParameters>& params) const { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 186 | AddEntry(event_type, NetLog::PHASE_BEGIN, params); |
| 187 | } |
| 188 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 189 | void BoundNetLog::EndEvent( |
[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_END, params); |
| 193 | } |
| 194 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 195 | void BoundNetLog::AddEventWithNetErrorCode(NetLog::EventType event_type, |
| 196 | int net_error) const { |
| 197 | DCHECK_GT(0, net_error); |
| 198 | DCHECK_NE(ERR_IO_PENDING, net_error); |
| 199 | AddEvent( |
| 200 | event_type, |
| 201 | make_scoped_refptr(new NetLogIntegerParameter("net_error", net_error))); |
| 202 | } |
| 203 | |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 204 | void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type, |
| 205 | int net_error) const { |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 206 | DCHECK_NE(ERR_IO_PENDING, net_error); |
[email protected] | f6f1bebc | 2011-01-07 03:04:54 | [diff] [blame] | 207 | if (net_error >= 0) { |
| 208 | EndEvent(event_type, NULL); |
| 209 | } else { |
| 210 | EndEvent( |
| 211 | event_type, |
| 212 | make_scoped_refptr(new NetLogIntegerParameter("net_error", net_error))); |
| 213 | } |
| 214 | } |
| 215 | |
[email protected] | 267a0d66d | 2011-06-01 21:15:19 | [diff] [blame] | 216 | void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type, |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 217 | int byte_count, |
| 218 | const char* bytes) const { |
[email protected] | 267a0d66d | 2011-06-01 21:15:19 | [diff] [blame] | 219 | scoped_refptr<NetLog::EventParameters> params; |
| 220 | if (IsLoggingBytes()) { |
| 221 | params = new NetLogBytesTransferredParameter(byte_count, bytes); |
| 222 | } else { |
| 223 | params = new NetLogBytesTransferredParameter(byte_count, NULL); |
| 224 | } |
| 225 | AddEvent(event_type, params); |
| 226 | } |
| 227 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 228 | NetLog::LogLevel BoundNetLog::GetLogLevel() const { |
| 229 | if (net_log_) |
| 230 | return net_log_->GetLogLevel(); |
| 231 | return NetLog::LOG_BASIC; |
| 232 | } |
| 233 | |
| 234 | bool BoundNetLog::IsLoggingBytes() const { |
| 235 | return GetLogLevel() == NetLog::LOG_ALL; |
| 236 | } |
| 237 | |
| 238 | bool BoundNetLog::IsLoggingAllEvents() const { |
| 239 | return GetLogLevel() <= NetLog::LOG_ALL_BUT_BYTES; |
| 240 | } |
| 241 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 242 | // static |
| 243 | BoundNetLog BoundNetLog::Make(NetLog* net_log, |
| 244 | NetLog::SourceType source_type) { |
| 245 | if (!net_log) |
| 246 | return BoundNetLog(); |
| 247 | |
| 248 | NetLog::Source source(source_type, net_log->NextID()); |
| 249 | return BoundNetLog(source, net_log); |
| 250 | } |
| 251 | |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 252 | NetLogStringParameter::NetLogStringParameter(const char* name, |
| 253 | const std::string& value) |
| 254 | : name_(name), value_(value) { |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 257 | NetLogStringParameter::~NetLogStringParameter() { |
| 258 | } |
| 259 | |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 260 | Value* NetLogIntegerParameter::ToValue() const { |
| 261 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 262 | dict->SetInteger(name_, value_); |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 263 | return dict; |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 264 | } |
| 265 | |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 266 | Value* NetLogStringParameter::ToValue() const { |
| 267 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 268 | dict->SetString(name_, value_); |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 269 | return dict; |
[email protected] | 1f0e32b | 2010-04-09 04:34:47 | [diff] [blame] | 270 | } |
| 271 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 272 | Value* NetLogSourceParameter::ToValue() const { |
| 273 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | 68697a4 | 2011-12-19 21:47:55 | [diff] [blame^] | 274 | if (value_.is_valid()) |
| 275 | dict->Set(name_, value_.ToValue()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 276 | return dict; |
| 277 | } |
| 278 | |
[email protected] | e8188fe | 2010-12-09 17:33:21 | [diff] [blame] | 279 | ScopedNetLogEvent::ScopedNetLogEvent( |
| 280 | const BoundNetLog& net_log, |
| 281 | NetLog::EventType event_type, |
| 282 | const scoped_refptr<NetLog::EventParameters>& params) |
| 283 | : net_log_(net_log), |
| 284 | event_type_(event_type) { |
| 285 | net_log_.BeginEvent(event_type, params); |
| 286 | } |
| 287 | |
| 288 | ScopedNetLogEvent::~ScopedNetLogEvent() { |
| 289 | net_log_.EndEvent(event_type_, end_event_params_); |
| 290 | } |
| 291 | |
| 292 | void ScopedNetLogEvent::SetEndEventParameters( |
| 293 | const scoped_refptr<NetLog::EventParameters>& end_event_params) { |
| 294 | DCHECK(!end_event_params_.get()); |
| 295 | end_event_params_ = end_event_params; |
| 296 | } |
| 297 | |
| 298 | const BoundNetLog& ScopedNetLogEvent::net_log() const { |
| 299 | return net_log_; |
| 300 | } |
| 301 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 302 | } // namespace net |