Valgrind Mac: Remove many obsolete suppressions.
[chromium-blink-merge.git] / net / base / net_log.cc
blob36c94ca9441734cc887c5085f48357066cf31a59
1 // Copyright (c) 2012 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.
5 #include "net/base/net_log.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "net/base/net_errors.h"
15 namespace net {
17 namespace {
19 // Returns parameters for logging data transferred events. Includes number of
20 // bytes transferred and, if the log level indicates bytes should be logged and
21 // |byte_count| > 0, the bytes themselves. The bytes are hex-encoded, since
22 // base::StringValue only supports UTF-8.
23 base::Value* BytesTransferredCallback(int byte_count,
24 const char* bytes,
25 NetLog::LogLevel log_level) {
26 base::DictionaryValue* dict = new base::DictionaryValue();
27 dict->SetInteger("byte_count", byte_count);
28 if (NetLog::IsLoggingBytes(log_level) && byte_count > 0)
29 dict->SetString("hex_encoded_bytes", base::HexEncode(bytes, byte_count));
30 return dict;
33 base::Value* SourceEventParametersCallback(const NetLog::Source source,
34 NetLog::LogLevel /* log_level */) {
35 if (!source.IsValid())
36 return NULL;
37 base::DictionaryValue* event_params = new base::DictionaryValue();
38 source.AddToEventParameters(event_params);
39 return event_params;
42 base::Value* NetLogIntegerCallback(const char* name,
43 int value,
44 NetLog::LogLevel /* log_level */) {
45 base::DictionaryValue* event_params = new base::DictionaryValue();
46 event_params->SetInteger(name, value);
47 return event_params;
50 base::Value* NetLogInt64Callback(const char* name,
51 int64 value,
52 NetLog::LogLevel /* log_level */) {
53 base::DictionaryValue* event_params = new base::DictionaryValue();
54 event_params->SetString(name, base::Int64ToString(value));
55 return event_params;
58 base::Value* NetLogStringCallback(const char* name,
59 const std::string* value,
60 NetLog::LogLevel /* log_level */) {
61 base::DictionaryValue* event_params = new base::DictionaryValue();
62 event_params->SetString(name, *value);
63 return event_params;
66 base::Value* NetLogString16Callback(const char* name,
67 const base::string16* value,
68 NetLog::LogLevel /* log_level */) {
69 base::DictionaryValue* event_params = new base::DictionaryValue();
70 event_params->SetString(name, *value);
71 return event_params;
74 } // namespace
76 // LoadTimingInfo requires this be 0.
77 const uint32 NetLog::Source::kInvalidId = 0;
79 NetLog::Source::Source() : type(SOURCE_NONE), id(kInvalidId) {
82 NetLog::Source::Source(SourceType type, uint32 id) : type(type), id(id) {
85 bool NetLog::Source::IsValid() const {
86 return id != kInvalidId;
89 void NetLog::Source::AddToEventParameters(
90 base::DictionaryValue* event_params) const {
91 base::DictionaryValue* dict = new base::DictionaryValue();
92 dict->SetInteger("type", static_cast<int>(type));
93 dict->SetInteger("id", static_cast<int>(id));
94 event_params->Set("source_dependency", dict);
97 NetLog::ParametersCallback NetLog::Source::ToEventParametersCallback() const {
98 return base::Bind(&SourceEventParametersCallback, *this);
101 // static
102 bool NetLog::Source::FromEventParameters(base::Value* event_params,
103 Source* source) {
104 base::DictionaryValue* dict = NULL;
105 base::DictionaryValue* source_dict = NULL;
106 int source_id = -1;
107 int source_type = NetLog::SOURCE_COUNT;
108 if (!event_params ||
109 !event_params->GetAsDictionary(&dict) ||
110 !dict->GetDictionary("source_dependency", &source_dict) ||
111 !source_dict->GetInteger("id", &source_id) ||
112 !source_dict->GetInteger("type", &source_type)) {
113 *source = Source();
114 return false;
117 DCHECK_GE(source_id, 0);
118 DCHECK_LT(source_type, NetLog::SOURCE_COUNT);
119 *source = Source(static_cast<SourceType>(source_type), source_id);
120 return true;
123 base::Value* NetLog::Entry::ToValue() const {
124 base::DictionaryValue* entry_dict(new base::DictionaryValue());
126 entry_dict->SetString("time", TickCountToString(data_->time));
128 // Set the entry source.
129 base::DictionaryValue* source_dict = new base::DictionaryValue();
130 source_dict->SetInteger("id", data_->source.id);
131 source_dict->SetInteger("type", static_cast<int>(data_->source.type));
132 entry_dict->Set("source", source_dict);
134 // Set the event info.
135 entry_dict->SetInteger("type", static_cast<int>(data_->type));
136 entry_dict->SetInteger("phase", static_cast<int>(data_->phase));
138 // Set the event-specific parameters.
139 if (data_->parameters_callback) {
140 base::Value* value = data_->parameters_callback->Run(log_level_);
141 if (value)
142 entry_dict->Set("params", value);
145 return entry_dict;
148 base::Value* NetLog::Entry::ParametersToValue() const {
149 if (data_->parameters_callback)
150 return data_->parameters_callback->Run(log_level_);
151 return NULL;
154 NetLog::EntryData::EntryData(
155 EventType type,
156 Source source,
157 EventPhase phase,
158 base::TimeTicks time,
159 const ParametersCallback* parameters_callback)
160 : type(type),
161 source(source),
162 phase(phase),
163 time(time),
164 parameters_callback(parameters_callback) {
167 NetLog::EntryData::~EntryData() {
170 NetLog::Entry::Entry(const EntryData* data, LogLevel log_level)
171 : data_(data), log_level_(log_level) {
174 NetLog::Entry::~Entry() {
177 NetLog::ThreadSafeObserver::ThreadSafeObserver() : log_level_(LOG_NONE),
178 net_log_(NULL) {
181 NetLog::ThreadSafeObserver::~ThreadSafeObserver() {
182 // Make sure we aren't watching a NetLog on destruction. Because the NetLog
183 // may pass events to each observer on multiple threads, we cannot safely
184 // stop watching a NetLog automatically from a parent class.
185 DCHECK(!net_log_);
188 NetLog::LogLevel NetLog::ThreadSafeObserver::log_level() const {
189 DCHECK(net_log_);
190 return log_level_;
193 NetLog* NetLog::ThreadSafeObserver::net_log() const {
194 return net_log_;
197 void NetLog::ThreadSafeObserver::OnAddEntryData(const EntryData& entry_data) {
198 OnAddEntry(Entry(&entry_data, log_level()));
201 NetLog::NetLog()
202 : last_id_(0),
203 base_log_level_(LOG_NONE),
204 effective_log_level_(LOG_NONE) {
207 NetLog::~NetLog() {
210 void NetLog::AddGlobalEntry(EventType type) {
211 AddEntry(type,
212 Source(net::NetLog::SOURCE_NONE, NextID()),
213 net::NetLog::PHASE_NONE,
214 NULL);
217 void NetLog::AddGlobalEntry(
218 EventType type,
219 const NetLog::ParametersCallback& parameters_callback) {
220 AddEntry(type,
221 Source(net::NetLog::SOURCE_NONE, NextID()),
222 net::NetLog::PHASE_NONE,
223 &parameters_callback);
226 uint32 NetLog::NextID() {
227 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1);
230 void NetLog::SetBaseLogLevel(LogLevel log_level) {
231 base::AutoLock lock(lock_);
232 base_log_level_ = log_level;
234 UpdateLogLevel();
237 NetLog::LogLevel NetLog::GetLogLevel() const {
238 base::subtle::Atomic32 log_level =
239 base::subtle::NoBarrier_Load(&effective_log_level_);
240 return static_cast<net::NetLog::LogLevel>(log_level);
243 void NetLog::AddThreadSafeObserver(
244 net::NetLog::ThreadSafeObserver* observer,
245 LogLevel log_level) {
246 DCHECK_NE(LOG_NONE, log_level);
247 base::AutoLock lock(lock_);
249 DCHECK(!observer->net_log_);
250 DCHECK_EQ(LOG_NONE, observer->log_level_);
251 observers_.AddObserver(observer);
252 observer->net_log_ = this;
253 observer->log_level_ = log_level;
254 UpdateLogLevel();
257 void NetLog::SetObserverLogLevel(
258 net::NetLog::ThreadSafeObserver* observer,
259 LogLevel log_level) {
260 DCHECK_NE(LOG_NONE, log_level);
261 base::AutoLock lock(lock_);
263 DCHECK(observers_.HasObserver(observer));
264 DCHECK_EQ(this, observer->net_log_);
265 DCHECK_NE(LOG_NONE, observer->log_level_);
266 observer->log_level_ = log_level;
267 UpdateLogLevel();
270 void NetLog::RemoveThreadSafeObserver(
271 net::NetLog::ThreadSafeObserver* observer) {
272 base::AutoLock lock(lock_);
274 DCHECK(observers_.HasObserver(observer));
275 DCHECK_EQ(this, observer->net_log_);
276 DCHECK_NE(LOG_NONE, observer->log_level_);
277 observers_.RemoveObserver(observer);
278 observer->net_log_ = NULL;
279 observer->log_level_ = LOG_NONE;
280 UpdateLogLevel();
283 void NetLog::UpdateLogLevel() {
284 lock_.AssertAcquired();
286 // Look through all the observers and find the finest granularity
287 // log level (higher values of the enum imply *lower* log levels).
288 LogLevel new_effective_log_level = base_log_level_;
289 ObserverListBase<ThreadSafeObserver>::Iterator it(observers_);
290 ThreadSafeObserver* observer;
291 while ((observer = it.GetNext()) != NULL) {
292 new_effective_log_level =
293 std::min(new_effective_log_level, observer->log_level());
295 base::subtle::NoBarrier_Store(&effective_log_level_,
296 new_effective_log_level);
299 // static
300 std::string NetLog::TickCountToString(const base::TimeTicks& time) {
301 int64 delta_time = (time - base::TimeTicks()).InMilliseconds();
302 return base::Int64ToString(delta_time);
305 // static
306 const char* NetLog::EventTypeToString(EventType event) {
307 switch (event) {
308 #define EVENT_TYPE(label) case TYPE_ ## label: return #label;
309 #include "net/base/net_log_event_type_list.h"
310 #undef EVENT_TYPE
311 default:
312 NOTREACHED();
313 return NULL;
317 // static
318 base::Value* NetLog::GetEventTypesAsValue() {
319 base::DictionaryValue* dict = new base::DictionaryValue();
320 for (int i = 0; i < EVENT_COUNT; ++i) {
321 dict->SetInteger(EventTypeToString(static_cast<EventType>(i)), i);
323 return dict;
326 // static
327 const char* NetLog::SourceTypeToString(SourceType source) {
328 switch (source) {
329 #define SOURCE_TYPE(label) case SOURCE_ ## label: return #label;
330 #include "net/base/net_log_source_type_list.h"
331 #undef SOURCE_TYPE
332 default:
333 NOTREACHED();
334 return NULL;
338 // static
339 base::Value* NetLog::GetSourceTypesAsValue() {
340 base::DictionaryValue* dict = new base::DictionaryValue();
341 for (int i = 0; i < SOURCE_COUNT; ++i) {
342 dict->SetInteger(SourceTypeToString(static_cast<SourceType>(i)), i);
344 return dict;
347 // static
348 const char* NetLog::EventPhaseToString(EventPhase phase) {
349 switch (phase) {
350 case PHASE_BEGIN:
351 return "PHASE_BEGIN";
352 case PHASE_END:
353 return "PHASE_END";
354 case PHASE_NONE:
355 return "PHASE_NONE";
357 NOTREACHED();
358 return NULL;
361 // static
362 bool NetLog::IsLoggingBytes(LogLevel log_level) {
363 return log_level == NetLog::LOG_ALL;
366 // static
367 bool NetLog::IsLogging(LogLevel log_level) {
368 return log_level < NetLog::LOG_NONE;
371 // static
372 NetLog::ParametersCallback NetLog::IntegerCallback(const char* name,
373 int value) {
374 return base::Bind(&NetLogIntegerCallback, name, value);
377 // static
378 NetLog::ParametersCallback NetLog::Int64Callback(const char* name,
379 int64 value) {
380 return base::Bind(&NetLogInt64Callback, name, value);
383 // static
384 NetLog::ParametersCallback NetLog::StringCallback(const char* name,
385 const std::string* value) {
386 DCHECK(value);
387 return base::Bind(&NetLogStringCallback, name, value);
390 // static
391 NetLog::ParametersCallback NetLog::StringCallback(const char* name,
392 const base::string16* value) {
393 DCHECK(value);
394 return base::Bind(&NetLogString16Callback, name, value);
397 void NetLog::AddEntry(EventType type,
398 const Source& source,
399 EventPhase phase,
400 const NetLog::ParametersCallback* parameters_callback) {
401 if (GetLogLevel() == LOG_NONE)
402 return;
403 EntryData entry_data(type, source, phase, base::TimeTicks::Now(),
404 parameters_callback);
406 // Notify all of the log observers.
407 base::AutoLock lock(lock_);
408 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntryData(entry_data));
411 void BoundNetLog::AddEntry(NetLog::EventType type,
412 NetLog::EventPhase phase) const {
413 if (!net_log_)
414 return;
415 net_log_->AddEntry(type, source_, phase, NULL);
418 void BoundNetLog::AddEntry(
419 NetLog::EventType type,
420 NetLog::EventPhase phase,
421 const NetLog::ParametersCallback& get_parameters) const {
422 if (!net_log_)
423 return;
424 net_log_->AddEntry(type, source_, phase, &get_parameters);
427 void BoundNetLog::AddEvent(NetLog::EventType type) const {
428 AddEntry(type, NetLog::PHASE_NONE);
431 void BoundNetLog::AddEvent(
432 NetLog::EventType type,
433 const NetLog::ParametersCallback& get_parameters) const {
434 AddEntry(type, NetLog::PHASE_NONE, get_parameters);
437 void BoundNetLog::BeginEvent(NetLog::EventType type) const {
438 AddEntry(type, NetLog::PHASE_BEGIN);
441 void BoundNetLog::BeginEvent(
442 NetLog::EventType type,
443 const NetLog::ParametersCallback& get_parameters) const {
444 AddEntry(type, NetLog::PHASE_BEGIN, get_parameters);
447 void BoundNetLog::EndEvent(NetLog::EventType type) const {
448 AddEntry(type, NetLog::PHASE_END);
451 void BoundNetLog::EndEvent(
452 NetLog::EventType type,
453 const NetLog::ParametersCallback& get_parameters) const {
454 AddEntry(type, NetLog::PHASE_END, get_parameters);
457 void BoundNetLog::AddEventWithNetErrorCode(NetLog::EventType event_type,
458 int net_error) const {
459 DCHECK_NE(ERR_IO_PENDING, net_error);
460 if (net_error >= 0) {
461 AddEvent(event_type);
462 } else {
463 AddEvent(event_type, NetLog::IntegerCallback("net_error", net_error));
467 void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type,
468 int net_error) const {
469 DCHECK_NE(ERR_IO_PENDING, net_error);
470 if (net_error >= 0) {
471 EndEvent(event_type);
472 } else {
473 EndEvent(event_type, NetLog::IntegerCallback("net_error", net_error));
477 void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type,
478 int byte_count,
479 const char* bytes) const {
480 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes));
483 NetLog::LogLevel BoundNetLog::GetLogLevel() const {
484 if (net_log_)
485 return net_log_->GetLogLevel();
486 return NetLog::LOG_NONE;
489 bool BoundNetLog::IsLoggingBytes() const {
490 return NetLog::IsLoggingBytes(GetLogLevel());
493 bool BoundNetLog::IsLogging() const {
494 return NetLog::IsLogging(GetLogLevel());
497 // static
498 BoundNetLog BoundNetLog::Make(NetLog* net_log,
499 NetLog::SourceType source_type) {
500 if (!net_log)
501 return BoundNetLog();
503 NetLog::Source source(source_type, net_log->NextID());
504 return BoundNetLog(source, net_log);
507 } // namespace net