Web MIDI: send back error information to blink on starting sessions
[chromium-blink-merge.git] / net / base / net_log_logger.cc
blob9653e5988557540e55d20f0d6ddb4a36c87ce67f
1 // Copyright 2013 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_logger.h"
7 #include <stdio.h>
9 #include "base/json/json_writer.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/values.h"
14 #include "net/base/address_family.h"
15 #include "net/base/load_states.h"
16 #include "net/quic/quic_protocol.h"
17 #include "net/quic/quic_utils.h"
19 namespace net {
21 // This should be incremented when significant changes are made that will
22 // invalidate the old loading code.
23 static const int kLogFormatVersion = 1;
25 NetLogLogger::NetLogLogger(FILE* file, const base::Value& constants)
26 : file_(file), log_level_(NetLog::LOG_ALL_BUT_BYTES), added_events_(false) {
27 DCHECK(file);
29 // Write constants to the output file. This allows loading files that have
30 // different source and event types, as they may be added and removed
31 // between Chrome versions.
32 std::string json;
33 base::JSONWriter::Write(&constants, &json);
34 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str());
35 fprintf(file_.get(), "\"events\": [\n");
38 NetLogLogger::~NetLogLogger() {
39 if (file_.get())
40 fprintf(file_.get(), "]}");
43 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level) {
44 DCHECK(!net_log());
45 log_level_ = log_level;
48 void NetLogLogger::StartObserving(net::NetLog* net_log) {
49 net_log->AddThreadSafeObserver(this, log_level_);
52 void NetLogLogger::StopObserving() {
53 net_log()->RemoveThreadSafeObserver(this);
56 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) {
57 // Add a comma and newline for every event but the first. Newlines are needed
58 // so can load partial log files by just ignoring the last line. For this to
59 // work, lines cannot be pretty printed.
60 scoped_ptr<base::Value> value(entry.ToValue());
61 std::string json;
62 base::JSONWriter::Write(value.get(), &json);
63 fprintf(file_.get(), "%s%s",
64 (added_events_ ? ",\n" : ""),
65 json.c_str());
66 added_events_ = true;
69 base::DictionaryValue* NetLogLogger::GetConstants() {
70 base::DictionaryValue* constants_dict = new base::DictionaryValue();
72 // Version of the file format.
73 constants_dict->SetInteger("logFormatVersion", kLogFormatVersion);
75 // Add a dictionary with information on the relationship between event type
76 // enums and their symbolic names.
77 constants_dict->Set("logEventTypes", net::NetLog::GetEventTypesAsValue());
79 // Add a dictionary with information about the relationship between load flag
80 // enums and their symbolic names.
82 base::DictionaryValue* dict = new base::DictionaryValue();
84 #define LOAD_FLAG(label, value) \
85 dict->SetInteger(# label, static_cast<int>(value));
86 #include "net/base/load_flags_list.h"
87 #undef LOAD_FLAG
89 constants_dict->Set("loadFlag", dict);
92 // Add a dictionary with information about the relationship between load state
93 // enums and their symbolic names.
95 base::DictionaryValue* dict = new base::DictionaryValue();
97 #define LOAD_STATE(label) \
98 dict->SetInteger(# label, net::LOAD_STATE_ ## label);
99 #include "net/base/load_states_list.h"
100 #undef LOAD_STATE
102 constants_dict->Set("loadState", dict);
105 // Add information on the relationship between net error codes and their
106 // symbolic names.
108 base::DictionaryValue* dict = new base::DictionaryValue();
110 #define NET_ERROR(label, value) \
111 dict->SetInteger(# label, static_cast<int>(value));
112 #include "net/base/net_error_list.h"
113 #undef NET_ERROR
115 constants_dict->Set("netError", dict);
118 // Add information on the relationship between QUIC error codes and their
119 // symbolic names.
121 base::DictionaryValue* dict = new base::DictionaryValue();
123 for (net::QuicErrorCode error = net::QUIC_NO_ERROR;
124 error < net::QUIC_LAST_ERROR;
125 error = static_cast<net::QuicErrorCode>(error + 1)) {
126 dict->SetInteger(net::QuicUtils::ErrorToString(error),
127 static_cast<int>(error));
130 constants_dict->Set("quicError", dict);
133 // Add information on the relationship between QUIC RST_STREAM error codes
134 // and their symbolic names.
136 base::DictionaryValue* dict = new base::DictionaryValue();
138 for (net::QuicRstStreamErrorCode error = net::QUIC_STREAM_NO_ERROR;
139 error < net::QUIC_STREAM_LAST_ERROR;
140 error = static_cast<net::QuicRstStreamErrorCode>(error + 1)) {
141 dict->SetInteger(net::QuicUtils::StreamErrorToString(error),
142 static_cast<int>(error));
145 constants_dict->Set("quicRstStreamError", dict);
148 // Information about the relationship between event phase enums and their
149 // symbolic names.
151 base::DictionaryValue* dict = new base::DictionaryValue();
153 dict->SetInteger("PHASE_BEGIN", net::NetLog::PHASE_BEGIN);
154 dict->SetInteger("PHASE_END", net::NetLog::PHASE_END);
155 dict->SetInteger("PHASE_NONE", net::NetLog::PHASE_NONE);
157 constants_dict->Set("logEventPhase", dict);
160 // Information about the relationship between source type enums and
161 // their symbolic names.
162 constants_dict->Set("logSourceType", net::NetLog::GetSourceTypesAsValue());
164 // Information about the relationship between LogLevel enums and their
165 // symbolic names.
167 base::DictionaryValue* dict = new base::DictionaryValue();
169 dict->SetInteger("LOG_ALL", net::NetLog::LOG_ALL);
170 dict->SetInteger("LOG_ALL_BUT_BYTES", net::NetLog::LOG_ALL_BUT_BYTES);
171 dict->SetInteger("LOG_STRIP_PRIVATE_DATA",
172 net::NetLog::LOG_STRIP_PRIVATE_DATA);
174 constants_dict->Set("logLevelType", dict);
177 // Information about the relationship between address family enums and
178 // their symbolic names.
180 base::DictionaryValue* dict = new base::DictionaryValue();
182 dict->SetInteger("ADDRESS_FAMILY_UNSPECIFIED",
183 net::ADDRESS_FAMILY_UNSPECIFIED);
184 dict->SetInteger("ADDRESS_FAMILY_IPV4",
185 net::ADDRESS_FAMILY_IPV4);
186 dict->SetInteger("ADDRESS_FAMILY_IPV6",
187 net::ADDRESS_FAMILY_IPV6);
189 constants_dict->Set("addressFamily", dict);
192 // Information about how the "time ticks" values we have given it relate to
193 // actual system times. (We used time ticks throughout since they are stable
194 // across system clock changes).
196 int64 cur_time_ms = (base::Time::Now() - base::Time()).InMilliseconds();
198 int64 cur_time_ticks_ms =
199 (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds();
201 // If we add this number to a time tick value, it gives the timestamp.
202 int64 tick_to_time_ms = cur_time_ms - cur_time_ticks_ms;
204 // Chrome on all platforms stores times using the Windows epoch
205 // (Jan 1 1601), but the javascript wants a unix epoch.
206 // TODO(eroman): Getting the timestamp relative to the unix epoch should
207 // be part of the time library.
208 const int64 kUnixEpochMs = 11644473600000LL;
209 int64 tick_to_unix_time_ms = tick_to_time_ms - kUnixEpochMs;
211 // Pass it as a string, since it may be too large to fit in an integer.
212 constants_dict->SetString("timeTickOffset",
213 base::Int64ToString(tick_to_unix_time_ms));
216 // "clientInfo" key is required for some NetLogLogger log readers.
217 // Provide a default empty value for compatibility.
218 constants_dict->Set("clientInfo", new base::DictionaryValue());
220 return constants_dict;
223 } // namespace net