Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / base / net_log_logger.cc
blobf6ac5dd7188c0c8e49f0eb105baa666c92264dec
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/base/net_errors.h"
17 #include "net/quic/quic_protocol.h"
18 #include "net/quic/quic_utils.h"
20 namespace net {
22 // This should be incremented when significant changes are made that will
23 // invalidate the old loading code.
24 static const int kLogFormatVersion = 1;
26 NetLogLogger::NetLogLogger(FILE* file, const base::Value& constants)
27 : file_(file), log_level_(NetLog::LOG_ALL_BUT_BYTES), added_events_(false) {
28 DCHECK(file);
30 // Write constants to the output file. This allows loading files that have
31 // different source and event types, as they may be added and removed
32 // between Chrome versions.
33 std::string json;
34 base::JSONWriter::Write(&constants, &json);
35 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str());
36 fprintf(file_.get(), "\"events\": [\n");
39 NetLogLogger::~NetLogLogger() {
40 if (file_.get())
41 fprintf(file_.get(), "]}");
44 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level) {
45 DCHECK(!net_log());
46 log_level_ = log_level;
49 void NetLogLogger::StartObserving(net::NetLog* net_log) {
50 net_log->AddThreadSafeObserver(this, log_level_);
53 void NetLogLogger::StopObserving() {
54 net_log()->RemoveThreadSafeObserver(this);
57 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) {
58 // Add a comma and newline for every event but the first. Newlines are needed
59 // so can load partial log files by just ignoring the last line. For this to
60 // work, lines cannot be pretty printed.
61 scoped_ptr<base::Value> value(entry.ToValue());
62 std::string json;
63 base::JSONWriter::Write(value.get(), &json);
64 fprintf(file_.get(), "%s%s",
65 (added_events_ ? ",\n" : ""),
66 json.c_str());
67 added_events_ = true;
70 base::DictionaryValue* NetLogLogger::GetConstants() {
71 base::DictionaryValue* constants_dict = new base::DictionaryValue();
73 // Version of the file format.
74 constants_dict->SetInteger("logFormatVersion", kLogFormatVersion);
76 // Add a dictionary with information on the relationship between event type
77 // enums and their symbolic names.
78 constants_dict->Set("logEventTypes", net::NetLog::GetEventTypesAsValue());
80 // Add a dictionary with information about the relationship between CertStatus
81 // flags and their symbolic names.
83 base::DictionaryValue* dict = new base::DictionaryValue();
85 #define CERT_STATUS_FLAG(label, value) dict->SetInteger(#label, value);
86 #include "net/cert/cert_status_flags_list.h"
87 #undef CERT_STATUS_FLAG
89 constants_dict->Set("certStatusFlag", dict);
92 // Add a dictionary with information about the relationship between load flag
93 // enums and their symbolic names.
95 base::DictionaryValue* dict = new base::DictionaryValue();
97 #define LOAD_FLAG(label, value) \
98 dict->SetInteger(# label, static_cast<int>(value));
99 #include "net/base/load_flags_list.h"
100 #undef LOAD_FLAG
102 constants_dict->Set("loadFlag", dict);
105 // Add a dictionary with information about the relationship between load state
106 // enums and their symbolic names.
108 base::DictionaryValue* dict = new base::DictionaryValue();
110 #define LOAD_STATE(label) \
111 dict->SetInteger(# label, net::LOAD_STATE_ ## label);
112 #include "net/base/load_states_list.h"
113 #undef LOAD_STATE
115 constants_dict->Set("loadState", dict);
118 // Add information on the relationship between net error codes and their
119 // symbolic names.
121 base::DictionaryValue* dict = new base::DictionaryValue();
123 #define NET_ERROR(label, value) \
124 dict->SetInteger(ErrorToShortString(value), static_cast<int>(value));
125 #include "net/base/net_error_list.h"
126 #undef NET_ERROR
128 constants_dict->Set("netError", dict);
131 // Add information on the relationship between QUIC error codes and their
132 // symbolic names.
134 base::DictionaryValue* dict = new base::DictionaryValue();
136 for (net::QuicErrorCode error = net::QUIC_NO_ERROR;
137 error < net::QUIC_LAST_ERROR;
138 error = static_cast<net::QuicErrorCode>(error + 1)) {
139 dict->SetInteger(net::QuicUtils::ErrorToString(error),
140 static_cast<int>(error));
143 constants_dict->Set("quicError", dict);
146 // Add information on the relationship between QUIC RST_STREAM error codes
147 // and their symbolic names.
149 base::DictionaryValue* dict = new base::DictionaryValue();
151 for (net::QuicRstStreamErrorCode error = net::QUIC_STREAM_NO_ERROR;
152 error < net::QUIC_STREAM_LAST_ERROR;
153 error = static_cast<net::QuicRstStreamErrorCode>(error + 1)) {
154 dict->SetInteger(net::QuicUtils::StreamErrorToString(error),
155 static_cast<int>(error));
158 constants_dict->Set("quicRstStreamError", dict);
161 // Information about the relationship between event phase enums and their
162 // symbolic names.
164 base::DictionaryValue* dict = new base::DictionaryValue();
166 dict->SetInteger("PHASE_BEGIN", net::NetLog::PHASE_BEGIN);
167 dict->SetInteger("PHASE_END", net::NetLog::PHASE_END);
168 dict->SetInteger("PHASE_NONE", net::NetLog::PHASE_NONE);
170 constants_dict->Set("logEventPhase", dict);
173 // Information about the relationship between source type enums and
174 // their symbolic names.
175 constants_dict->Set("logSourceType", net::NetLog::GetSourceTypesAsValue());
177 // Information about the relationship between LogLevel enums and their
178 // symbolic names.
180 base::DictionaryValue* dict = new base::DictionaryValue();
182 dict->SetInteger("LOG_ALL", net::NetLog::LOG_ALL);
183 dict->SetInteger("LOG_ALL_BUT_BYTES", net::NetLog::LOG_ALL_BUT_BYTES);
184 dict->SetInteger("LOG_STRIP_PRIVATE_DATA",
185 net::NetLog::LOG_STRIP_PRIVATE_DATA);
187 constants_dict->Set("logLevelType", dict);
190 // Information about the relationship between address family enums and
191 // their symbolic names.
193 base::DictionaryValue* dict = new base::DictionaryValue();
195 dict->SetInteger("ADDRESS_FAMILY_UNSPECIFIED",
196 net::ADDRESS_FAMILY_UNSPECIFIED);
197 dict->SetInteger("ADDRESS_FAMILY_IPV4",
198 net::ADDRESS_FAMILY_IPV4);
199 dict->SetInteger("ADDRESS_FAMILY_IPV6",
200 net::ADDRESS_FAMILY_IPV6);
202 constants_dict->Set("addressFamily", dict);
205 // Information about how the "time ticks" values we have given it relate to
206 // actual system times. (We used time ticks throughout since they are stable
207 // across system clock changes).
209 int64 cur_time_ms = (base::Time::Now() - base::Time()).InMilliseconds();
211 int64 cur_time_ticks_ms =
212 (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds();
214 // If we add this number to a time tick value, it gives the timestamp.
215 int64 tick_to_time_ms = cur_time_ms - cur_time_ticks_ms;
217 // Chrome on all platforms stores times using the Windows epoch
218 // (Jan 1 1601), but the javascript wants a unix epoch.
219 // TODO(eroman): Getting the timestamp relative to the unix epoch should
220 // be part of the time library.
221 const int64 kUnixEpochMs = 11644473600000LL;
222 int64 tick_to_unix_time_ms = tick_to_time_ms - kUnixEpochMs;
224 // Pass it as a string, since it may be too large to fit in an integer.
225 constants_dict->SetString("timeTickOffset",
226 base::Int64ToString(tick_to_unix_time_ms));
229 // "clientInfo" key is required for some NetLogLogger log readers.
230 // Provide a default empty value for compatibility.
231 constants_dict->Set("clientInfo", new base::DictionaryValue());
233 return constants_dict;
236 } // namespace net