blob: 26ecdeeac7c0981a60ef2a1f68c336a3918eb7e9 [file] [log] [blame]
[email protected]988394e2012-02-22 16:37:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]9e743cd2010-03-16 07:03:532// 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]f6f1bebc2011-01-07 03:04:546
[email protected]93190352010-08-13 13:55:107#include "base/logging.h"
8#include "base/string_number_conversions.h"
[email protected]5097dc82010-07-15 17:23:239#include "base/time.h"
[email protected]f1d81922010-07-31 17:47:0910#include "base/utf_string_conversions.h"
[email protected]d13f51b2010-04-27 23:20:4511#include "base/values.h"
[email protected]f6f1bebc2011-01-07 03:04:5412#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5313
14namespace net {
15
[email protected]267a0d66d2011-06-01 21:15:1916namespace {
17
18// Parameters for logging data transferred events. Includes bytes transferred
19// and, if |bytes| is not NULL, the bytes themselves.
20class 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
32NetLogBytesTransferredParameter::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
42Value* 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]ee094b82010-08-24 15:55:5152Value* 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]ae6e9912011-07-27 01:18:2859NetLog::ThreadSafeObserver::ThreadSafeObserver(LogLevel log_level)
60 : log_level_(log_level) {
61}
62
63NetLog::ThreadSafeObserver::~ThreadSafeObserver() {
64}
65
66NetLog::LogLevel NetLog::ThreadSafeObserver::log_level() const {
67 return log_level_;
68}
69
[email protected]9e743cd2010-03-16 07:03:5370// static
[email protected]1ce7b66b2010-10-12 20:32:4471std::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]9e743cd2010-03-16 07:03:5377const 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]988394e2012-02-22 16:37:5582 default:
83 NOTREACHED();
84 return NULL;
[email protected]9e743cd2010-03-16 07:03:5385 }
[email protected]9e743cd2010-03-16 07:03:5386}
87
[email protected]fd9c0d92010-03-23 17:47:4988// static
[email protected]988394e2012-02-22 16:37:5589base::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]fd9c0d92010-03-23 17:47:4995}
96
[email protected]93190352010-08-13 13:55:1097// static
98const char* NetLog::SourceTypeToString(SourceType source) {
99 switch (source) {
[email protected]988394e2012-02-22 16:37:55100#define SOURCE_TYPE(label) case SOURCE_ ## label: return #label;
[email protected]93190352010-08-13 13:55:10101#include "net/base/net_log_source_type_list.h"
102#undef SOURCE_TYPE
[email protected]988394e2012-02-22 16:37:55103 default:
104 NOTREACHED();
105 return NULL;
[email protected]93190352010-08-13 13:55:10106 }
[email protected]988394e2012-02-22 16:37:55107}
108
109// static
110base::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]93190352010-08-13 13:55:10116}
117
118// static
119const 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]8effd3f62011-03-25 16:29:07133Value* NetLog::EntryToDictionaryValue(NetLog::EventType type,
[email protected]93190352010-08-13 13:55:10134 const base::TimeTicks& time,
[email protected]8effd3f62011-03-25 16:29:07135 const NetLog::Source& source,
136 NetLog::EventPhase phase,
137 NetLog::EventParameters* params,
[email protected]93190352010-08-13 13:55:10138 bool use_strings) {
139 DictionaryValue* entry_dict = new DictionaryValue();
140
[email protected]1ce7b66b2010-10-12 20:32:44141 entry_dict->SetString("time", TickCountToString(time));
[email protected]93190352010-08-13 13:55:10142
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]8effd3f62011-03-25 16:29:07150 NetLog::SourceTypeToString(source.type));
[email protected]93190352010-08-13 13:55:10151 }
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]8effd3f62011-03-25 16:29:07159 entry_dict->SetString("type", NetLog::EventTypeToString(type));
160 entry_dict->SetString("phase", NetLog::EventPhaseToString(phase));
[email protected]93190352010-08-13 13:55:10161 }
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]ec11be62010-04-28 19:28:09170void BoundNetLog::AddEntry(
171 NetLog::EventType type,
172 NetLog::EventPhase phase,
173 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47174 if (net_log_) {
[email protected]ec11be62010-04-28 19:28:09175 net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase, params);
[email protected]1f0e32b2010-04-09 04:34:47176 }
177}
178
179void BoundNetLog::AddEntryWithTime(
180 NetLog::EventType type,
181 const base::TimeTicks& time,
182 NetLog::EventPhase phase,
[email protected]ec11be62010-04-28 19:28:09183 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47184 if (net_log_) {
[email protected]ec11be62010-04-28 19:28:09185 net_log_->AddEntry(type, time, source_, phase, params);
[email protected]1f0e32b2010-04-09 04:34:47186 }
[email protected]9e743cd2010-03-16 07:03:53187}
188
[email protected]ec11be62010-04-28 19:28:09189void BoundNetLog::AddEvent(
[email protected]1f0e32b2010-04-09 04:34:47190 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09191 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47192 AddEntry(event_type, NetLog::PHASE_NONE, params);
[email protected]9e743cd2010-03-16 07:03:53193}
194
[email protected]ec11be62010-04-28 19:28:09195void BoundNetLog::BeginEvent(
[email protected]1f0e32b2010-04-09 04:34:47196 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09197 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47198 AddEntry(event_type, NetLog::PHASE_BEGIN, params);
199}
200
[email protected]ec11be62010-04-28 19:28:09201void BoundNetLog::EndEvent(
[email protected]1f0e32b2010-04-09 04:34:47202 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09203 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47204 AddEntry(event_type, NetLog::PHASE_END, params);
205}
206
[email protected]8866f622011-10-18 20:08:10207void 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]f6f1bebc2011-01-07 03:04:54216void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type,
217 int net_error) const {
[email protected]8866f622011-10-18 20:08:10218 DCHECK_NE(ERR_IO_PENDING, net_error);
[email protected]f6f1bebc2011-01-07 03:04:54219 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]267a0d66d2011-06-01 21:15:19228void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type,
[email protected]8866f622011-10-18 20:08:10229 int byte_count,
230 const char* bytes) const {
[email protected]267a0d66d2011-06-01 21:15:19231 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]be1a48b2011-01-20 00:12:13240NetLog::LogLevel BoundNetLog::GetLogLevel() const {
241 if (net_log_)
242 return net_log_->GetLogLevel();
243 return NetLog::LOG_BASIC;
244}
245
246bool BoundNetLog::IsLoggingBytes() const {
247 return GetLogLevel() == NetLog::LOG_ALL;
248}
249
250bool BoundNetLog::IsLoggingAllEvents() const {
251 return GetLogLevel() <= NetLog::LOG_ALL_BUT_BYTES;
252}
253
[email protected]9e743cd2010-03-16 07:03:53254// static
255BoundNetLog 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]d13f51b2010-04-27 23:20:45264NetLogStringParameter::NetLogStringParameter(const char* name,
265 const std::string& value)
266 : name_(name), value_(value) {
[email protected]1f0e32b2010-04-09 04:34:47267}
268
[email protected]9349cfb2010-08-31 18:00:53269NetLogStringParameter::~NetLogStringParameter() {
270}
271
[email protected]d13f51b2010-04-27 23:20:45272Value* NetLogIntegerParameter::ToValue() const {
273 DictionaryValue* dict = new DictionaryValue();
[email protected]ccaff652010-07-31 06:28:20274 dict->SetInteger(name_, value_);
[email protected]d13f51b2010-04-27 23:20:45275 return dict;
[email protected]1f0e32b2010-04-09 04:34:47276}
277
[email protected]d13f51b2010-04-27 23:20:45278Value* NetLogStringParameter::ToValue() const {
279 DictionaryValue* dict = new DictionaryValue();
[email protected]ccaff652010-07-31 06:28:20280 dict->SetString(name_, value_);
[email protected]d13f51b2010-04-27 23:20:45281 return dict;
[email protected]1f0e32b2010-04-09 04:34:47282}
283
[email protected]06650c52010-06-03 00:49:17284Value* NetLogSourceParameter::ToValue() const {
285 DictionaryValue* dict = new DictionaryValue();
[email protected]68697a42011-12-19 21:47:55286 if (value_.is_valid())
287 dict->Set(name_, value_.ToValue());
[email protected]06650c52010-06-03 00:49:17288 return dict;
289}
290
[email protected]e8188fe2010-12-09 17:33:21291ScopedNetLogEvent::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
300ScopedNetLogEvent::~ScopedNetLogEvent() {
301 net_log_.EndEvent(event_type_, end_event_params_);
302}
303
304void 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
310const BoundNetLog& ScopedNetLogEvent::net_log() const {
311 return net_log_;
312}
313
[email protected]9e743cd2010-03-16 07:03:53314} // namespace net