summaryrefslogtreecommitdiffstats
path: root/net/base/net_log.cc
blob: 04496342c5866facbd6c140fb790a829a9eeb04d (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 2011 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"
#include "base/string_number_conversions.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "net/base/net_errors.h"

namespace net {

Value* NetLog::Source::ToValue() const {
  DictionaryValue* dict = new DictionaryValue();
  dict->SetInteger("type", static_cast<int>(type));
  dict->SetInteger("id", static_cast<int>(id));
  return dict;
}

// static
std::string NetLog::TickCountToString(const base::TimeTicks& time) {
  int64 delta_time = (time - base::TimeTicks()).InMilliseconds();
  return base::Int64ToString(delta_time);
}

// 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;
}

// static
const char* NetLog::SourceTypeToString(SourceType source) {
  switch (source) {
#define SOURCE_TYPE(label, id) case id: return #label;
#include "net/base/net_log_source_type_list.h"
#undef SOURCE_TYPE
  }
  NOTREACHED();
  return NULL;
}

// static
const char* NetLog::EventPhaseToString(EventPhase phase) {
  switch (phase) {
    case PHASE_BEGIN:
      return "PHASE_BEGIN";
    case PHASE_END:
      return "PHASE_END";
    case PHASE_NONE:
      return "PHASE_NONE";
  }
  NOTREACHED();
  return NULL;
}

// static
Value* NetLog::EntryToDictionaryValue(NetLog::EventType type,
                                      const base::TimeTicks& time,
                                      const NetLog::Source& source,
                                      NetLog::EventPhase phase,
                                      NetLog::EventParameters* params,
                                      bool use_strings) {
  DictionaryValue* entry_dict = new DictionaryValue();

  entry_dict->SetString("time", TickCountToString(time));

  // Set the entry source.
  DictionaryValue* source_dict = new DictionaryValue();
  source_dict->SetInteger("id", source.id);
  if (!use_strings) {
    source_dict->SetInteger("type", static_cast<int>(source.type));
  } else {
    source_dict->SetString("type",
                           NetLog::SourceTypeToString(source.type));
  }
  entry_dict->Set("source", source_dict);

  // Set the event info.
  if (!use_strings) {
    entry_dict->SetInteger("type", static_cast<int>(type));
    entry_dict->SetInteger("phase", static_cast<int>(phase));
  } else {
    entry_dict->SetString("type", NetLog::EventTypeToString(type));
    entry_dict->SetString("phase", NetLog::EventPhaseToString(phase));
  }

  // Set the event-specific parameters.
  if (params)
    entry_dict->Set("params", params->ToValue());

  return entry_dict;
}

void BoundNetLog::AddEntry(
    NetLog::EventType type,
    NetLog::EventPhase phase,
    const scoped_refptr<NetLog::EventParameters>& params) const {
  if (net_log_) {
    net_log_->AddEntry(type, base::TimeTicks::Now(), source_, phase, params);
  }
}

void BoundNetLog::AddEntryWithTime(
    NetLog::EventType type,
    const base::TimeTicks& time,
    NetLog::EventPhase phase,
    const scoped_refptr<NetLog::EventParameters>& params) const {
  if (net_log_) {
    net_log_->AddEntry(type, time, source_, phase, params);
  }
}

void BoundNetLog::AddEvent(
    NetLog::EventType event_type,
    const scoped_refptr<NetLog::EventParameters>& params) const {
  AddEntry(event_type, NetLog::PHASE_NONE, params);
}

void BoundNetLog::BeginEvent(
    NetLog::EventType event_type,
    const scoped_refptr<NetLog::EventParameters>& params) const {
  AddEntry(event_type, NetLog::PHASE_BEGIN, params);
}

void BoundNetLog::EndEvent(
    NetLog::EventType event_type,
    const scoped_refptr<NetLog::EventParameters>& params) const {
  AddEntry(event_type, NetLog::PHASE_END, params);
}

void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type,
                                           int net_error) const {
  DCHECK_NE(net_error, ERR_IO_PENDING);
  if (net_error >= 0) {
    EndEvent(event_type, NULL);
  } else {
    EndEvent(
        event_type,
        make_scoped_refptr(new NetLogIntegerParameter("net_error", net_error)));
  }
}

NetLog::LogLevel BoundNetLog::GetLogLevel() const {
  if (net_log_)
    return net_log_->GetLogLevel();
  return NetLog::LOG_BASIC;
}

bool BoundNetLog::IsLoggingBytes() const {
  return GetLogLevel() == NetLog::LOG_ALL;
}

bool BoundNetLog::IsLoggingAllEvents() const {
  return GetLogLevel() <= NetLog::LOG_ALL_BUT_BYTES;
}

// 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);
}

NetLogStringParameter::NetLogStringParameter(const char* name,
                                             const std::string& value)
    : name_(name), value_(value) {
}

NetLogStringParameter::~NetLogStringParameter() {
}

Value* NetLogIntegerParameter::ToValue() const {
  DictionaryValue* dict = new DictionaryValue();
  dict->SetInteger(name_, value_);
  return dict;
}

Value* NetLogStringParameter::ToValue() const {
  DictionaryValue* dict = new DictionaryValue();
  dict->SetString(name_, value_);
  return dict;
}

Value* NetLogSourceParameter::ToValue() const {
  DictionaryValue* dict = new DictionaryValue();

  dict->Set(name_, value_.ToValue());
  return dict;
}

ScopedNetLogEvent::ScopedNetLogEvent(
    const BoundNetLog& net_log,
    NetLog::EventType event_type,
    const scoped_refptr<NetLog::EventParameters>& params)
    : net_log_(net_log),
      event_type_(event_type) {
  net_log_.BeginEvent(event_type, params);
}

ScopedNetLogEvent::~ScopedNetLogEvent() {
  net_log_.EndEvent(event_type_, end_event_params_);
}

void ScopedNetLogEvent::SetEndEventParameters(
    const scoped_refptr<NetLog::EventParameters>& end_event_params) {
  DCHECK(!end_event_params_.get());
  end_event_params_ = end_event_params;
}

const BoundNetLog& ScopedNetLogEvent::net_log() const {
  return net_log_;
}

}  // namespace net