blob: 9460f747e1cf114b4ee72a894441432b81f304a4 [file] [log] [blame]
[email protected]8effd3f62011-03-25 16:29:071// Copyright (c) 2011 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
82 }
83 return NULL;
84}
85
[email protected]fd9c0d92010-03-23 17:47:4986// static
87std::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]93190352010-08-13 13:55:1095// static
96const 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
107const 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]8effd3f62011-03-25 16:29:07121Value* NetLog::EntryToDictionaryValue(NetLog::EventType type,
[email protected]93190352010-08-13 13:55:10122 const base::TimeTicks& time,
[email protected]8effd3f62011-03-25 16:29:07123 const NetLog::Source& source,
124 NetLog::EventPhase phase,
125 NetLog::EventParameters* params,
[email protected]93190352010-08-13 13:55:10126 bool use_strings) {
127 DictionaryValue* entry_dict = new DictionaryValue();
128
[email protected]1ce7b66b2010-10-12 20:32:44129 entry_dict->SetString("time", TickCountToString(time));
[email protected]93190352010-08-13 13:55:10130
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]8effd3f62011-03-25 16:29:07138 NetLog::SourceTypeToString(source.type));
[email protected]93190352010-08-13 13:55:10139 }
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]8effd3f62011-03-25 16:29:07147 entry_dict->SetString("type", NetLog::EventTypeToString(type));
148 entry_dict->SetString("phase", NetLog::EventPhaseToString(phase));
[email protected]93190352010-08-13 13:55:10149 }
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]ec11be62010-04-28 19:28:09158void BoundNetLog::AddEntry(
159 NetLog::EventType type,
160 NetLog::EventPhase phase,
161 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47162 if (net_log_) {
[email protected]ec11be62010-04-28 19:28:09163 net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase, params);
[email protected]1f0e32b2010-04-09 04:34:47164 }
165}
166
167void BoundNetLog::AddEntryWithTime(
168 NetLog::EventType type,
169 const base::TimeTicks& time,
170 NetLog::EventPhase phase,
[email protected]ec11be62010-04-28 19:28:09171 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47172 if (net_log_) {
[email protected]ec11be62010-04-28 19:28:09173 net_log_->AddEntry(type, time, source_, phase, params);
[email protected]1f0e32b2010-04-09 04:34:47174 }
[email protected]9e743cd2010-03-16 07:03:53175}
176
[email protected]ec11be62010-04-28 19:28:09177void BoundNetLog::AddEvent(
[email protected]1f0e32b2010-04-09 04:34:47178 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09179 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47180 AddEntry(event_type, NetLog::PHASE_NONE, params);
[email protected]9e743cd2010-03-16 07:03:53181}
182
[email protected]ec11be62010-04-28 19:28:09183void BoundNetLog::BeginEvent(
[email protected]1f0e32b2010-04-09 04:34:47184 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09185 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47186 AddEntry(event_type, NetLog::PHASE_BEGIN, params);
187}
188
[email protected]ec11be62010-04-28 19:28:09189void BoundNetLog::EndEvent(
[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_END, params);
193}
194
[email protected]8866f622011-10-18 20:08:10195void 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]f6f1bebc2011-01-07 03:04:54204void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type,
205 int net_error) const {
[email protected]8866f622011-10-18 20:08:10206 DCHECK_NE(ERR_IO_PENDING, net_error);
[email protected]f6f1bebc2011-01-07 03:04:54207 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]267a0d66d2011-06-01 21:15:19216void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type,
[email protected]8866f622011-10-18 20:08:10217 int byte_count,
218 const char* bytes) const {
[email protected]267a0d66d2011-06-01 21:15:19219 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]be1a48b2011-01-20 00:12:13228NetLog::LogLevel BoundNetLog::GetLogLevel() const {
229 if (net_log_)
230 return net_log_->GetLogLevel();
231 return NetLog::LOG_BASIC;
232}
233
234bool BoundNetLog::IsLoggingBytes() const {
235 return GetLogLevel() == NetLog::LOG_ALL;
236}
237
238bool BoundNetLog::IsLoggingAllEvents() const {
239 return GetLogLevel() <= NetLog::LOG_ALL_BUT_BYTES;
240}
241
[email protected]9e743cd2010-03-16 07:03:53242// static
243BoundNetLog 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]d13f51b2010-04-27 23:20:45252NetLogStringParameter::NetLogStringParameter(const char* name,
253 const std::string& value)
254 : name_(name), value_(value) {
[email protected]1f0e32b2010-04-09 04:34:47255}
256
[email protected]9349cfb2010-08-31 18:00:53257NetLogStringParameter::~NetLogStringParameter() {
258}
259
[email protected]d13f51b2010-04-27 23:20:45260Value* NetLogIntegerParameter::ToValue() const {
261 DictionaryValue* dict = new DictionaryValue();
[email protected]ccaff652010-07-31 06:28:20262 dict->SetInteger(name_, value_);
[email protected]d13f51b2010-04-27 23:20:45263 return dict;
[email protected]1f0e32b2010-04-09 04:34:47264}
265
[email protected]d13f51b2010-04-27 23:20:45266Value* NetLogStringParameter::ToValue() const {
267 DictionaryValue* dict = new DictionaryValue();
[email protected]ccaff652010-07-31 06:28:20268 dict->SetString(name_, value_);
[email protected]d13f51b2010-04-27 23:20:45269 return dict;
[email protected]1f0e32b2010-04-09 04:34:47270}
271
[email protected]06650c52010-06-03 00:49:17272Value* NetLogSourceParameter::ToValue() const {
273 DictionaryValue* dict = new DictionaryValue();
[email protected]68697a42011-12-19 21:47:55274 if (value_.is_valid())
275 dict->Set(name_, value_.ToValue());
[email protected]06650c52010-06-03 00:49:17276 return dict;
277}
278
[email protected]e8188fe2010-12-09 17:33:21279ScopedNetLogEvent::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
288ScopedNetLogEvent::~ScopedNetLogEvent() {
289 net_log_.EndEvent(event_type_, end_event_params_);
290}
291
292void 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
298const BoundNetLog& ScopedNetLogEvent::net_log() const {
299 return net_log_;
300}
301
[email protected]9e743cd2010-03-16 07:03:53302} // namespace net