blob: 1b1661edca32e6c99ec1ca3d71f041ea2f5f0266 [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"
6#include "base/logging.h"
7
8namespace net {
9
10// static
11const char* NetLog::EventTypeToString(EventType event) {
12 switch (event) {
13#define EVENT_TYPE(label) case TYPE_ ## label: return #label;
14#include "net/base/net_log_event_type_list.h"
15#undef EVENT_TYPE
16 }
17 return NULL;
18}
19
20void BoundNetLog::AddEntry(const NetLog::Entry& entry) const {
21 if (net_log_)
22 net_log_->AddEntry(entry);
23}
24
25bool BoundNetLog::HasListener() const {
26 if (net_log_)
27 return net_log_->HasListener();
28 return false;
29}
30
31void BoundNetLog::AddEvent(NetLog::EventType event_type) const {
32 if (net_log_) {
33 NetLog::Entry entry;
34 entry.source = source_;
35 entry.type = NetLog::Entry::TYPE_EVENT;
36 entry.time = base::TimeTicks::Now();
37 entry.event = NetLog::Event(event_type, NetLog::PHASE_NONE);
38 AddEntry(entry);
39 }
40}
41
42void BoundNetLog::BeginEvent(NetLog::EventType event_type) const {
43 if (net_log_) {
44 NetLog::Entry entry;
45 entry.source = source_;
46 entry.type = NetLog::Entry::TYPE_EVENT;
47 entry.time = base::TimeTicks::Now();
48 entry.event = NetLog::Event(event_type, NetLog::PHASE_BEGIN);
49 AddEntry(entry);
50 }
51}
52
53void BoundNetLog::BeginEventWithString(NetLog::EventType event_type,
54 const std::string& string) const {
55 NetLog::Entry entry;
56 entry.source = source_;
57 entry.type = NetLog::Entry::TYPE_EVENT;
58 entry.time = base::TimeTicks::Now();
59 entry.event = NetLog::Event(event_type, NetLog::PHASE_BEGIN);
60 entry.string = string;
61 AddEntry(entry);
62}
63
64void BoundNetLog::AddEventWithInteger(NetLog::EventType event_type,
65 int integer) const {
66 NetLog::Entry entry;
67 entry.source = source_;
68 entry.type = NetLog::Entry::TYPE_EVENT;
69 entry.time = base::TimeTicks::Now();
70 entry.event = NetLog::Event(event_type, NetLog::PHASE_NONE);
71 entry.error_code = integer;
72 AddEntry(entry);
73}
74
75void BoundNetLog::EndEvent(NetLog::EventType event_type) const {
76 if (net_log_) {
77 NetLog::Entry entry;
78 entry.source = source_;
79 entry.type = NetLog::Entry::TYPE_EVENT;
80 entry.time = base::TimeTicks::Now();
81 entry.event = NetLog::Event(event_type, NetLog::PHASE_END);
82 AddEntry(entry);
83 }
84}
85
86void BoundNetLog::AddStringLiteral(const char* literal) const {
87 if (net_log_) {
88 NetLog::Entry entry;
89 entry.source = source_;
90 entry.type = NetLog::Entry::TYPE_STRING_LITERAL;
91 entry.time = base::TimeTicks::Now();
92 entry.literal = literal;
93 AddEntry(entry);
94 }
95}
96
97void BoundNetLog::AddString(const std::string& string) const {
98 if (net_log_) {
99 NetLog::Entry entry;
100 entry.source = source_;
101 entry.type = NetLog::Entry::TYPE_STRING;
102 entry.time = base::TimeTicks::Now();
103 entry.string = string;
104 AddEntry(entry);
105 }
106}
107
108void BoundNetLog::AddErrorCode(int error) const {
109 if (net_log_) {
110 NetLog::Entry entry;
111 entry.source = source_;
112 entry.type = NetLog::Entry::TYPE_ERROR_CODE;
113 entry.time = base::TimeTicks::Now();
114 entry.error_code = error;
115 AddEntry(entry);
116 }
117}
118
119// static
120BoundNetLog BoundNetLog::Make(NetLog* net_log,
121 NetLog::SourceType source_type) {
122 if (!net_log)
123 return BoundNetLog();
124
125 NetLog::Source source(source_type, net_log->NextID());
126 return BoundNetLog(source, net_log);
127}
128
129void CapturingNetLog::AddEntry(const Entry& entry) {
130 if (entries_.size() + 1 < max_num_entries_)
131 entries_.push_back(entry);
132}
133
134int CapturingNetLog::NextID() {
135 return next_id_++;
136}
137
138void CapturingNetLog::Clear() {
139 entries_.clear();
140}
141
142void CapturingBoundNetLog::Clear() {
143 capturing_net_log_->Clear();
144}
145
146void CapturingBoundNetLog::AppendTo(const BoundNetLog& net_log) const {
147 for (size_t i = 0; i < entries().size(); ++i) {
148 NetLog::Entry entry = entries()[i];
149 entry.source = net_log.source();
150 net_log.AddEntry(entry);
151 }
152}
153
154} // namespace net