summaryrefslogtreecommitdiffstats
path: root/net/base/net_log.cc
blob: 6bb9297f69486ea033625a77cc89419fad43a66c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/net_log.h"
#include "base/logging.h"

namespace net {

// static
const char* NetLog::EventTypeToString(EventType event) {
  switch (event) {
#define EVENT_TYPE(label) case TYPE_ ## label: return #label;
#include "net/base/net_log_event_type_list.h"
#undef EVENT_TYPE
  }
  return NULL;
}

// static
std::vector<NetLog::EventType> NetLog::GetAllEventTypes() {
  std::vector<NetLog::EventType> types;
#define EVENT_TYPE(label) types.push_back(TYPE_ ## label);
#include "net/base/net_log_event_type_list.h"
#undef EVENT_TYPE
  return types;
}

void BoundNetLog::AddEntry(const NetLog::Entry& entry) const {
  if (net_log_)
    net_log_->AddEntry(entry);
}

bool BoundNetLog::HasListener() const {
  if (net_log_)
    return net_log_->HasListener();
  return false;
}

void BoundNetLog::AddEvent(NetLog::EventType event_type) const {
  if (net_log_) {
    NetLog::Entry entry;
    entry.source = source_;
    entry.type = NetLog::Entry::TYPE_EVENT;
    entry.time = base::TimeTicks::Now();
    entry.event = NetLog::Event(event_type, NetLog::PHASE_NONE);
    AddEntry(entry);
  }
}

void BoundNetLog::BeginEvent(NetLog::EventType event_type) const {
  if (net_log_) {
    NetLog::Entry entry;
    entry.source = source_;
    entry.type = NetLog::Entry::TYPE_EVENT;
    entry.time = base::TimeTicks::Now();
    entry.event = NetLog::Event(event_type, NetLog::PHASE_BEGIN);
    AddEntry(entry);
  }
}

void BoundNetLog::BeginEventWithString(NetLog::EventType event_type,
                                       const std::string& string) const {
  NetLog::Entry entry;
  entry.source = source_;
  entry.type = NetLog::Entry::TYPE_EVENT;
  entry.time = base::TimeTicks::Now();
  entry.event = NetLog::Event(event_type, NetLog::PHASE_BEGIN);
  entry.string = string;
  AddEntry(entry);
}

void BoundNetLog::AddEventWithInteger(NetLog::EventType event_type,
                                      int integer) const {
  NetLog::Entry entry;
  entry.source = source_;
  entry.type = NetLog::Entry::TYPE_EVENT;
  entry.time = base::TimeTicks::Now();
  entry.event = NetLog::Event(event_type, NetLog::PHASE_NONE);
  entry.error_code = integer;
  AddEntry(entry);
}

void BoundNetLog::EndEvent(NetLog::EventType event_type) const {
  if (net_log_) {
    NetLog::Entry entry;
    entry.source = source_;
    entry.type = NetLog::Entry::TYPE_EVENT;
    entry.time = base::TimeTicks::Now();
    entry.event = NetLog::Event(event_type, NetLog::PHASE_END);
    AddEntry(entry);
  }
}

void BoundNetLog::AddStringLiteral(const char* literal) const {
  if (net_log_) {
    NetLog::Entry entry;
    entry.source = source_;
    entry.type = NetLog::Entry::TYPE_STRING_LITERAL;
    entry.time = base::TimeTicks::Now();
    entry.literal = literal;
    AddEntry(entry);
  }
}

void BoundNetLog::AddString(const std::string& string) const {
  if (net_log_) {
    NetLog::Entry entry;
    entry.source = source_;
    entry.type = NetLog::Entry::TYPE_STRING;
    entry.time = base::TimeTicks::Now();
    entry.string = string;
    AddEntry(entry);
  }
}

void BoundNetLog::AddErrorCode(int error) const {
  if (net_log_) {
    NetLog::Entry entry;
    entry.source = source_;
    entry.type = NetLog::Entry::TYPE_ERROR_CODE;
    entry.time = base::TimeTicks::Now();
    entry.error_code = error;
    AddEntry(entry);
  }
}

// static
BoundNetLog BoundNetLog::Make(NetLog* net_log,
                              NetLog::SourceType source_type) {
  if (!net_log)
    return BoundNetLog();

  NetLog::Source source(source_type, net_log->NextID());
  return BoundNetLog(source, net_log);
}

void CapturingNetLog::AddEntry(const Entry& entry) {
  if (entries_.size() + 1 < max_num_entries_)
    entries_.push_back(entry);
}

int CapturingNetLog::NextID() {
  return next_id_++;
}

void CapturingNetLog::Clear() {
  entries_.clear();
}

void CapturingBoundNetLog::Clear() {
  capturing_net_log_->Clear();
}

void CapturingBoundNetLog::AppendTo(const BoundNetLog& net_log) const {
  for (size_t i = 0; i < entries().size(); ++i) {
    NetLog::Entry entry = entries()[i];
    entry.source = net_log.source();
    net_log.AddEntry(entry);
  }
}

}  // namespace net