blob: c9e73196ce23685b14c536694ef490e3076734f8 [file] [log] [blame]
[email protected]9e743cd2010-03-16 07:03:531// 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]93190352010-08-13 13:55:106#include "base/logging.h"
7#include "base/string_number_conversions.h"
[email protected]5097dc82010-07-15 17:23:238#include "base/time.h"
[email protected]f1d81922010-07-31 17:47:099#include "base/utf_string_conversions.h"
[email protected]d13f51b2010-04-27 23:20:4510#include "base/values.h"
[email protected]9e743cd2010-03-16 07:03:5311
12namespace net {
13
[email protected]ee094b82010-08-24 15:55:5114Value* 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]9e743cd2010-03-16 07:03:5321// static
[email protected]1ce7b66b2010-10-12 20:32:4422std::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]9e743cd2010-03-16 07:03:5328const 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]fd9c0d92010-03-23 17:47:4937// static
38std::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]93190352010-08-13 13:55:1046// static
47const 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
58const 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
72Value* 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]1ce7b66b2010-10-12 20:32:4480 entry_dict->SetString("time", TickCountToString(time));
[email protected]93190352010-08-13 13:55:1081
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]ec11be62010-04-28 19:28:09109void BoundNetLog::AddEntry(
110 NetLog::EventType type,
111 NetLog::EventPhase phase,
112 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47113 if (net_log_) {
[email protected]ec11be62010-04-28 19:28:09114 net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase, params);
[email protected]1f0e32b2010-04-09 04:34:47115 }
116}
117
118void BoundNetLog::AddEntryWithTime(
119 NetLog::EventType type,
120 const base::TimeTicks& time,
121 NetLog::EventPhase phase,
[email protected]ec11be62010-04-28 19:28:09122 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47123 if (net_log_) {
[email protected]ec11be62010-04-28 19:28:09124 net_log_->AddEntry(type, time, source_, phase, params);
[email protected]1f0e32b2010-04-09 04:34:47125 }
[email protected]9e743cd2010-03-16 07:03:53126}
127
[email protected]095c7cf2010-08-31 21:07:33128NetLog::LogLevel BoundNetLog::GetLogLevel() const {
[email protected]9e743cd2010-03-16 07:03:53129 if (net_log_)
[email protected]095c7cf2010-08-31 21:07:33130 return net_log_->GetLogLevel();
131 return NetLog::LOG_BASIC;
132}
133
[email protected]465aeb942010-10-14 19:58:14134bool BoundNetLog::IsLoggingBytes() const {
[email protected]095c7cf2010-08-31 21:07:33135 return GetLogLevel() == NetLog::LOG_ALL;
[email protected]9e743cd2010-03-16 07:03:53136}
137
[email protected]465aeb942010-10-14 19:58:14138bool BoundNetLog::IsLoggingAllEvents() const {
139 return GetLogLevel() <= NetLog::LOG_ALL_BUT_BYTES;
140}
141
[email protected]ec11be62010-04-28 19:28:09142void BoundNetLog::AddEvent(
[email protected]1f0e32b2010-04-09 04:34:47143 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09144 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47145 AddEntry(event_type, NetLog::PHASE_NONE, params);
[email protected]9e743cd2010-03-16 07:03:53146}
147
[email protected]ec11be62010-04-28 19:28:09148void BoundNetLog::BeginEvent(
[email protected]1f0e32b2010-04-09 04:34:47149 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09150 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47151 AddEntry(event_type, NetLog::PHASE_BEGIN, params);
152}
153
[email protected]ec11be62010-04-28 19:28:09154void BoundNetLog::EndEvent(
[email protected]1f0e32b2010-04-09 04:34:47155 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09156 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47157 AddEntry(event_type, NetLog::PHASE_END, params);
158}
159
[email protected]9e743cd2010-03-16 07:03:53160// static
161BoundNetLog 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]d13f51b2010-04-27 23:20:45170NetLogStringParameter::NetLogStringParameter(const char* name,
171 const std::string& value)
172 : name_(name), value_(value) {
[email protected]1f0e32b2010-04-09 04:34:47173}
174
[email protected]9349cfb2010-08-31 18:00:53175NetLogStringParameter::~NetLogStringParameter() {
176}
177
[email protected]d13f51b2010-04-27 23:20:45178Value* NetLogIntegerParameter::ToValue() const {
179 DictionaryValue* dict = new DictionaryValue();
[email protected]ccaff652010-07-31 06:28:20180 dict->SetInteger(name_, value_);
[email protected]d13f51b2010-04-27 23:20:45181 return dict;
[email protected]1f0e32b2010-04-09 04:34:47182}
183
[email protected]d13f51b2010-04-27 23:20:45184Value* NetLogStringParameter::ToValue() const {
185 DictionaryValue* dict = new DictionaryValue();
[email protected]ccaff652010-07-31 06:28:20186 dict->SetString(name_, value_);
[email protected]d13f51b2010-04-27 23:20:45187 return dict;
[email protected]1f0e32b2010-04-09 04:34:47188}
189
[email protected]06650c52010-06-03 00:49:17190Value* NetLogSourceParameter::ToValue() const {
191 DictionaryValue* dict = new DictionaryValue();
192
[email protected]ee094b82010-08-24 15:55:51193 dict->Set(name_, value_.ToValue());
[email protected]06650c52010-06-03 00:49:17194 return dict;
195}
196
[email protected]e8188fe2010-12-09 17:33:21197ScopedNetLogEvent::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
206ScopedNetLogEvent::~ScopedNetLogEvent() {
207 net_log_.EndEvent(event_type_, end_event_params_);
208}
209
210void 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
216const BoundNetLog& ScopedNetLogEvent::net_log() const {
217 return net_log_;
218}
219
[email protected]9e743cd2010-03-16 07:03:53220} // namespace net