Chromecast Android buildfix: rework CommandLine initialization logic.
[chromium-blink-merge.git] / net / base / net_log_logger.cc
blob0c0f1c925b64f5e194cdf6e5167402808e48c534
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 {
22 struct StringToConstant {
23 const char* name;
24 const int constant;
27 const StringToConstant kCertStatusFlags[] = {
28 #define CERT_STATUS_FLAG(label, value) { #label, value },
29 #include "net/cert/cert_status_flags_list.h"
30 #undef CERT_STATUS_FLAG
33 const StringToConstant kLoadFlags[] = {
34 #define LOAD_FLAG(label, value) { #label, value },
35 #include "net/base/load_flags_list.h"
36 #undef LOAD_FLAG
39 const StringToConstant kLoadStateTable[] = {
40 #define LOAD_STATE(label) { # label, net::LOAD_STATE_ ## label },
41 #include "net/base/load_states_list.h"
42 #undef LOAD_STATE
45 const short kNetErrors[] = {
46 #define NET_ERROR(label, value) value,
47 #include "net/base/net_error_list.h"
48 #undef NET_ERROR
51 } // namespace
53 namespace net {
55 // This should be incremented when significant changes are made that will
56 // invalidate the old loading code.
57 static const int kLogFormatVersion = 1;
59 NetLogLogger::NetLogLogger(FILE* file, const base::Value& constants)
60 : file_(file),
61 log_level_(NetLog::LOG_STRIP_PRIVATE_DATA),
62 added_events_(false) {
63 DCHECK(file);
65 // Write constants to the output file. This allows loading files that have
66 // different source and event types, as they may be added and removed
67 // between Chrome versions.
68 std::string json;
69 base::JSONWriter::Write(&constants, &json);
70 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str());
71 fprintf(file_.get(), "\"events\": [\n");
74 NetLogLogger::~NetLogLogger() {
75 if (file_.get())
76 fprintf(file_.get(), "]}");
79 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level) {
80 DCHECK(!net_log());
81 log_level_ = log_level;
84 void NetLogLogger::StartObserving(net::NetLog* net_log) {
85 net_log->AddThreadSafeObserver(this, log_level_);
88 void NetLogLogger::StopObserving() {
89 net_log()->RemoveThreadSafeObserver(this);
92 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) {
93 // Add a comma and newline for every event but the first. Newlines are needed
94 // so can load partial log files by just ignoring the last line. For this to
95 // work, lines cannot be pretty printed.
96 scoped_ptr<base::Value> value(entry.ToValue());
97 std::string json;
98 base::JSONWriter::Write(value.get(), &json);
99 fprintf(file_.get(), "%s%s",
100 (added_events_ ? ",\n" : ""),
101 json.c_str());
102 added_events_ = true;
105 base::DictionaryValue* NetLogLogger::GetConstants() {
106 base::DictionaryValue* constants_dict = new base::DictionaryValue();
108 // Version of the file format.
109 constants_dict->SetInteger("logFormatVersion", kLogFormatVersion);
111 // Add a dictionary with information on the relationship between event type
112 // enums and their symbolic names.
113 constants_dict->Set("logEventTypes", net::NetLog::GetEventTypesAsValue());
115 // Add a dictionary with information about the relationship between CertStatus
116 // flags and their symbolic names.
118 base::DictionaryValue* dict = new base::DictionaryValue();
120 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kCertStatusFlags); i++)
121 dict->SetInteger(kCertStatusFlags[i].name, kCertStatusFlags[i].constant);
123 constants_dict->Set("certStatusFlag", dict);
126 // Add a dictionary with information about the relationship between load flag
127 // enums and their symbolic names.
129 base::DictionaryValue* dict = new base::DictionaryValue();
131 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kLoadFlags); i++)
132 dict->SetInteger(kLoadFlags[i].name, kLoadFlags[i].constant);
134 constants_dict->Set("loadFlag", dict);
137 // Add a dictionary with information about the relationship between load state
138 // enums and their symbolic names.
140 base::DictionaryValue* dict = new base::DictionaryValue();
142 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kLoadStateTable); i++)
143 dict->SetInteger(kLoadStateTable[i].name, kLoadStateTable[i].constant);
145 constants_dict->Set("loadState", dict);
148 // Add information on the relationship between net error codes and their
149 // symbolic names.
151 base::DictionaryValue* dict = new base::DictionaryValue();
153 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kNetErrors); i++)
154 dict->SetInteger(ErrorToShortString(kNetErrors[i]), kNetErrors[i]);
156 constants_dict->Set("netError", dict);
159 // Add information on the relationship between QUIC error codes and their
160 // symbolic names.
162 base::DictionaryValue* dict = new base::DictionaryValue();
164 for (net::QuicErrorCode error = net::QUIC_NO_ERROR;
165 error < net::QUIC_LAST_ERROR;
166 error = static_cast<net::QuicErrorCode>(error + 1)) {
167 dict->SetInteger(net::QuicUtils::ErrorToString(error),
168 static_cast<int>(error));
171 constants_dict->Set("quicError", dict);
174 // Add information on the relationship between QUIC RST_STREAM error codes
175 // and their symbolic names.
177 base::DictionaryValue* dict = new base::DictionaryValue();
179 for (net::QuicRstStreamErrorCode error = net::QUIC_STREAM_NO_ERROR;
180 error < net::QUIC_STREAM_LAST_ERROR;
181 error = static_cast<net::QuicRstStreamErrorCode>(error + 1)) {
182 dict->SetInteger(net::QuicUtils::StreamErrorToString(error),
183 static_cast<int>(error));
186 constants_dict->Set("quicRstStreamError", dict);
189 // Information about the relationship between event phase enums and their
190 // symbolic names.
192 base::DictionaryValue* dict = new base::DictionaryValue();
194 dict->SetInteger("PHASE_BEGIN", net::NetLog::PHASE_BEGIN);
195 dict->SetInteger("PHASE_END", net::NetLog::PHASE_END);
196 dict->SetInteger("PHASE_NONE", net::NetLog::PHASE_NONE);
198 constants_dict->Set("logEventPhase", dict);
201 // Information about the relationship between source type enums and
202 // their symbolic names.
203 constants_dict->Set("logSourceType", net::NetLog::GetSourceTypesAsValue());
205 // Information about the relationship between LogLevel enums and their
206 // symbolic names.
208 base::DictionaryValue* dict = new base::DictionaryValue();
210 dict->SetInteger("LOG_ALL", net::NetLog::LOG_ALL);
211 dict->SetInteger("LOG_ALL_BUT_BYTES", net::NetLog::LOG_ALL_BUT_BYTES);
212 dict->SetInteger("LOG_STRIP_PRIVATE_DATA",
213 net::NetLog::LOG_STRIP_PRIVATE_DATA);
215 constants_dict->Set("logLevelType", dict);
218 // Information about the relationship between address family enums and
219 // their symbolic names.
221 base::DictionaryValue* dict = new base::DictionaryValue();
223 dict->SetInteger("ADDRESS_FAMILY_UNSPECIFIED",
224 net::ADDRESS_FAMILY_UNSPECIFIED);
225 dict->SetInteger("ADDRESS_FAMILY_IPV4",
226 net::ADDRESS_FAMILY_IPV4);
227 dict->SetInteger("ADDRESS_FAMILY_IPV6",
228 net::ADDRESS_FAMILY_IPV6);
230 constants_dict->Set("addressFamily", dict);
233 // Information about how the "time ticks" values we have given it relate to
234 // actual system times. (We used time ticks throughout since they are stable
235 // across system clock changes).
237 int64 cur_time_ms = (base::Time::Now() - base::Time()).InMilliseconds();
239 int64 cur_time_ticks_ms =
240 (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds();
242 // If we add this number to a time tick value, it gives the timestamp.
243 int64 tick_to_time_ms = cur_time_ms - cur_time_ticks_ms;
245 // Chrome on all platforms stores times using the Windows epoch
246 // (Jan 1 1601), but the javascript wants a unix epoch.
247 // TODO(eroman): Getting the timestamp relative to the unix epoch should
248 // be part of the time library.
249 const int64 kUnixEpochMs = 11644473600000LL;
250 int64 tick_to_unix_time_ms = tick_to_time_ms - kUnixEpochMs;
252 // Pass it as a string, since it may be too large to fit in an integer.
253 constants_dict->SetString("timeTickOffset",
254 base::Int64ToString(tick_to_unix_time_ms));
257 // "clientInfo" key is required for some NetLogLogger log readers.
258 // Provide a default empty value for compatibility.
259 constants_dict->Set("clientInfo", new base::DictionaryValue());
261 return constants_dict;
264 } // namespace net