Move AsyncPolicyProvider, etc. to components/policy/.
[chromium-blink-merge.git] / net / base / net_log_logger.cc
blobbd68bd55a3f8a87a7f59bc3f84c51e23139ef37f
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), 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::StartObserving(net::NetLog* net_log) {
44 net_log->AddThreadSafeObserver(this, net::NetLog::LOG_ALL_BUT_BYTES);
47 void NetLogLogger::StopObserving() {
48 net_log()->RemoveThreadSafeObserver(this);
51 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) {
52 // Add a comma and newline for every event but the first. Newlines are needed
53 // so can load partial log files by just ignoring the last line. For this to
54 // work, lines cannot be pretty printed.
55 scoped_ptr<Value> value(entry.ToValue());
56 std::string json;
57 base::JSONWriter::Write(value.get(), &json);
58 fprintf(file_.get(), "%s%s",
59 (added_events_ ? ",\n" : ""),
60 json.c_str());
61 added_events_ = true;
64 base::DictionaryValue* NetLogLogger::GetConstants() {
65 DictionaryValue* constants_dict = new DictionaryValue();
67 // Version of the file format.
68 constants_dict->SetInteger("logFormatVersion", kLogFormatVersion);
70 // Add a dictionary with information on the relationship between event type
71 // enums and their symbolic names.
72 constants_dict->Set("logEventTypes", net::NetLog::GetEventTypesAsValue());
74 // Add a dictionary with information about the relationship between load flag
75 // enums and their symbolic names.
77 DictionaryValue* dict = new DictionaryValue();
79 #define LOAD_FLAG(label, value) \
80 dict->SetInteger(# label, static_cast<int>(value));
81 #include "net/base/load_flags_list.h"
82 #undef LOAD_FLAG
84 constants_dict->Set("loadFlag", dict);
87 // Add a dictionary with information about the relationship between load state
88 // enums and their symbolic names.
90 DictionaryValue* dict = new DictionaryValue();
92 #define LOAD_STATE(label) \
93 dict->SetInteger(# label, net::LOAD_STATE_ ## label);
94 #include "net/base/load_states_list.h"
95 #undef LOAD_STATE
97 constants_dict->Set("loadState", dict);
100 // Add information on the relationship between net error codes and their
101 // symbolic names.
103 DictionaryValue* dict = new DictionaryValue();
105 #define NET_ERROR(label, value) \
106 dict->SetInteger(# label, static_cast<int>(value));
107 #include "net/base/net_error_list.h"
108 #undef NET_ERROR
110 constants_dict->Set("netError", dict);
113 // Add information on the relationship between QUIC error codes and their
114 // symbolic names.
116 DictionaryValue* dict = new DictionaryValue();
118 for (net::QuicErrorCode error = net::QUIC_NO_ERROR;
119 error < net::QUIC_LAST_ERROR;
120 error = static_cast<net::QuicErrorCode>(error + 1)) {
121 dict->SetInteger(net::QuicUtils::ErrorToString(error),
122 static_cast<int>(error));
125 constants_dict->Set("quicError", dict);
128 // Add information on the relationship between QUIC RST_STREAM error codes
129 // and their symbolic names.
131 DictionaryValue* dict = new DictionaryValue();
133 for (net::QuicRstStreamErrorCode error = net::QUIC_STREAM_NO_ERROR;
134 error < net::QUIC_STREAM_LAST_ERROR;
135 error = static_cast<net::QuicRstStreamErrorCode>(error + 1)) {
136 dict->SetInteger(net::QuicUtils::StreamErrorToString(error),
137 static_cast<int>(error));
140 constants_dict->Set("quicRstStreamError", dict);
143 // Information about the relationship between event phase enums and their
144 // symbolic names.
146 DictionaryValue* dict = new DictionaryValue();
148 dict->SetInteger("PHASE_BEGIN", net::NetLog::PHASE_BEGIN);
149 dict->SetInteger("PHASE_END", net::NetLog::PHASE_END);
150 dict->SetInteger("PHASE_NONE", net::NetLog::PHASE_NONE);
152 constants_dict->Set("logEventPhase", dict);
155 // Information about the relationship between source type enums and
156 // their symbolic names.
157 constants_dict->Set("logSourceType", net::NetLog::GetSourceTypesAsValue());
159 // Information about the relationship between LogLevel enums and their
160 // symbolic names.
162 DictionaryValue* dict = new DictionaryValue();
164 dict->SetInteger("LOG_ALL", net::NetLog::LOG_ALL);
165 dict->SetInteger("LOG_ALL_BUT_BYTES", net::NetLog::LOG_ALL_BUT_BYTES);
166 dict->SetInteger("LOG_BASIC", net::NetLog::LOG_BASIC);
168 constants_dict->Set("logLevelType", dict);
171 // Information about the relationship between address family enums and
172 // their symbolic names.
174 DictionaryValue* dict = new DictionaryValue();
176 dict->SetInteger("ADDRESS_FAMILY_UNSPECIFIED",
177 net::ADDRESS_FAMILY_UNSPECIFIED);
178 dict->SetInteger("ADDRESS_FAMILY_IPV4",
179 net::ADDRESS_FAMILY_IPV4);
180 dict->SetInteger("ADDRESS_FAMILY_IPV6",
181 net::ADDRESS_FAMILY_IPV6);
183 constants_dict->Set("addressFamily", dict);
186 // Information about how the "time ticks" values we have given it relate to
187 // actual system times. (We used time ticks throughout since they are stable
188 // across system clock changes).
190 int64 cur_time_ms = (base::Time::Now() - base::Time()).InMilliseconds();
192 int64 cur_time_ticks_ms =
193 (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds();
195 // If we add this number to a time tick value, it gives the timestamp.
196 int64 tick_to_time_ms = cur_time_ms - cur_time_ticks_ms;
198 // Chrome on all platforms stores times using the Windows epoch
199 // (Jan 1 1601), but the javascript wants a unix epoch.
200 // TODO(eroman): Getting the timestamp relative to the unix epoch should
201 // be part of the time library.
202 const int64 kUnixEpochMs = 11644473600000LL;
203 int64 tick_to_unix_time_ms = tick_to_time_ms - kUnixEpochMs;
205 // Pass it as a string, since it may be too large to fit in an integer.
206 constants_dict->SetString("timeTickOffset",
207 base::Int64ToString(tick_to_unix_time_ms));
210 // "clientInfo" key is required for some NetLogLogger log readers.
211 // Provide a default empty value for compatibility.
212 constants_dict->Set("clientInfo", new DictionaryValue());
214 return constants_dict;
217 } // namespace net