OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/net_log_logger.h" | 5 #include "net/base/net_log_logger.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/metrics/field_trial.h" | |
13 #include "base/strings/string_number_conversions.h" | |
14 #include "base/values.h" | 12 #include "base/values.h" |
15 #include "net/base/address_family.h" | 13 #include "net/base/net_log_util.h" |
16 #include "net/base/load_states.h" | |
17 #include "net/base/net_errors.h" | |
18 #include "net/quic/quic_protocol.h" | |
19 #include "net/quic/quic_utils.h" | |
20 | |
21 namespace { | |
22 | |
23 struct StringToConstant { | |
24 const char* name; | |
25 const int constant; | |
26 }; | |
27 | |
28 const StringToConstant kCertStatusFlags[] = { | |
29 #define CERT_STATUS_FLAG(label, value) { #label, value }, | |
30 #include "net/cert/cert_status_flags_list.h" | |
31 #undef CERT_STATUS_FLAG | |
32 }; | |
33 | |
34 const StringToConstant kLoadFlags[] = { | |
35 #define LOAD_FLAG(label, value) { #label, value }, | |
36 #include "net/base/load_flags_list.h" | |
37 #undef LOAD_FLAG | |
38 }; | |
39 | |
40 const StringToConstant kLoadStateTable[] = { | |
41 #define LOAD_STATE(label) { # label, net::LOAD_STATE_ ## label }, | |
42 #include "net/base/load_states_list.h" | |
43 #undef LOAD_STATE | |
44 }; | |
45 | |
46 const short kNetErrors[] = { | |
47 #define NET_ERROR(label, value) value, | |
48 #include "net/base/net_error_list.h" | |
49 #undef NET_ERROR | |
50 }; | |
51 | |
52 } // namespace | |
53 | 14 |
54 namespace net { | 15 namespace net { |
55 | 16 |
56 // This should be incremented when significant changes are made that will | |
57 // invalidate the old loading code. | |
58 static const int kLogFormatVersion = 1; | |
59 | |
60 NetLogLogger::NetLogLogger(FILE* file, const base::Value& constants) | 17 NetLogLogger::NetLogLogger(FILE* file, const base::Value& constants) |
61 : file_(file), | 18 : file_(file), |
62 log_level_(NetLog::LOG_STRIP_PRIVATE_DATA), | 19 log_level_(NetLog::LOG_STRIP_PRIVATE_DATA), |
63 added_events_(false) { | 20 added_events_(false) { |
64 DCHECK(file); | 21 DCHECK(file); |
65 | 22 |
66 // Write constants to the output file. This allows loading files that have | 23 // Write constants to the output file. This allows loading files that have |
67 // different source and event types, as they may be added and removed | 24 // different source and event types, as they may be added and removed |
68 // between Chrome versions. | 25 // between Chrome versions. |
69 std::string json; | 26 std::string json; |
(...skipping 26 matching lines...) Expand all Loading... |
96 // work, lines cannot be pretty printed. | 53 // work, lines cannot be pretty printed. |
97 scoped_ptr<base::Value> value(entry.ToValue()); | 54 scoped_ptr<base::Value> value(entry.ToValue()); |
98 std::string json; | 55 std::string json; |
99 base::JSONWriter::Write(value.get(), &json); | 56 base::JSONWriter::Write(value.get(), &json); |
100 fprintf(file_.get(), "%s%s", | 57 fprintf(file_.get(), "%s%s", |
101 (added_events_ ? ",\n" : ""), | 58 (added_events_ ? ",\n" : ""), |
102 json.c_str()); | 59 json.c_str()); |
103 added_events_ = true; | 60 added_events_ = true; |
104 } | 61 } |
105 | 62 |
| 63 // static |
106 base::DictionaryValue* NetLogLogger::GetConstants() { | 64 base::DictionaryValue* NetLogLogger::GetConstants() { |
107 base::DictionaryValue* constants_dict = new base::DictionaryValue(); | 65 return GetNetConstants().release(); |
108 | |
109 // Version of the file format. | |
110 constants_dict->SetInteger("logFormatVersion", kLogFormatVersion); | |
111 | |
112 // Add a dictionary with information on the relationship between event type | |
113 // enums and their symbolic names. | |
114 constants_dict->Set("logEventTypes", net::NetLog::GetEventTypesAsValue()); | |
115 | |
116 // Add a dictionary with information about the relationship between CertStatus | |
117 // flags and their symbolic names. | |
118 { | |
119 base::DictionaryValue* dict = new base::DictionaryValue(); | |
120 | |
121 for (size_t i = 0; i < arraysize(kCertStatusFlags); i++) | |
122 dict->SetInteger(kCertStatusFlags[i].name, kCertStatusFlags[i].constant); | |
123 | |
124 constants_dict->Set("certStatusFlag", dict); | |
125 } | |
126 | |
127 // Add a dictionary with information about the relationship between load flag | |
128 // enums and their symbolic names. | |
129 { | |
130 base::DictionaryValue* dict = new base::DictionaryValue(); | |
131 | |
132 for (size_t i = 0; i < arraysize(kLoadFlags); i++) | |
133 dict->SetInteger(kLoadFlags[i].name, kLoadFlags[i].constant); | |
134 | |
135 constants_dict->Set("loadFlag", dict); | |
136 } | |
137 | |
138 // Add a dictionary with information about the relationship between load state | |
139 // enums and their symbolic names. | |
140 { | |
141 base::DictionaryValue* dict = new base::DictionaryValue(); | |
142 | |
143 for (size_t i = 0; i < arraysize(kLoadStateTable); i++) | |
144 dict->SetInteger(kLoadStateTable[i].name, kLoadStateTable[i].constant); | |
145 | |
146 constants_dict->Set("loadState", dict); | |
147 } | |
148 | |
149 // Add information on the relationship between net error codes and their | |
150 // symbolic names. | |
151 { | |
152 base::DictionaryValue* dict = new base::DictionaryValue(); | |
153 | |
154 for (size_t i = 0; i < arraysize(kNetErrors); i++) | |
155 dict->SetInteger(ErrorToShortString(kNetErrors[i]), kNetErrors[i]); | |
156 | |
157 constants_dict->Set("netError", dict); | |
158 } | |
159 | |
160 // Add information on the relationship between QUIC error codes and their | |
161 // symbolic names. | |
162 { | |
163 base::DictionaryValue* dict = new base::DictionaryValue(); | |
164 | |
165 for (net::QuicErrorCode error = net::QUIC_NO_ERROR; | |
166 error < net::QUIC_LAST_ERROR; | |
167 error = static_cast<net::QuicErrorCode>(error + 1)) { | |
168 dict->SetInteger(net::QuicUtils::ErrorToString(error), | |
169 static_cast<int>(error)); | |
170 } | |
171 | |
172 constants_dict->Set("quicError", dict); | |
173 } | |
174 | |
175 // Add information on the relationship between QUIC RST_STREAM error codes | |
176 // and their symbolic names. | |
177 { | |
178 base::DictionaryValue* dict = new base::DictionaryValue(); | |
179 | |
180 for (net::QuicRstStreamErrorCode error = net::QUIC_STREAM_NO_ERROR; | |
181 error < net::QUIC_STREAM_LAST_ERROR; | |
182 error = static_cast<net::QuicRstStreamErrorCode>(error + 1)) { | |
183 dict->SetInteger(net::QuicUtils::StreamErrorToString(error), | |
184 static_cast<int>(error)); | |
185 } | |
186 | |
187 constants_dict->Set("quicRstStreamError", dict); | |
188 } | |
189 | |
190 // Information about the relationship between event phase enums and their | |
191 // symbolic names. | |
192 { | |
193 base::DictionaryValue* dict = new base::DictionaryValue(); | |
194 | |
195 dict->SetInteger("PHASE_BEGIN", net::NetLog::PHASE_BEGIN); | |
196 dict->SetInteger("PHASE_END", net::NetLog::PHASE_END); | |
197 dict->SetInteger("PHASE_NONE", net::NetLog::PHASE_NONE); | |
198 | |
199 constants_dict->Set("logEventPhase", dict); | |
200 } | |
201 | |
202 // Information about the relationship between source type enums and | |
203 // their symbolic names. | |
204 constants_dict->Set("logSourceType", net::NetLog::GetSourceTypesAsValue()); | |
205 | |
206 // Information about the relationship between LogLevel enums and their | |
207 // symbolic names. | |
208 { | |
209 base::DictionaryValue* dict = new base::DictionaryValue(); | |
210 | |
211 dict->SetInteger("LOG_ALL", net::NetLog::LOG_ALL); | |
212 dict->SetInteger("LOG_ALL_BUT_BYTES", net::NetLog::LOG_ALL_BUT_BYTES); | |
213 dict->SetInteger("LOG_STRIP_PRIVATE_DATA", | |
214 net::NetLog::LOG_STRIP_PRIVATE_DATA); | |
215 | |
216 constants_dict->Set("logLevelType", dict); | |
217 } | |
218 | |
219 // Information about the relationship between address family enums and | |
220 // their symbolic names. | |
221 { | |
222 base::DictionaryValue* dict = new base::DictionaryValue(); | |
223 | |
224 dict->SetInteger("ADDRESS_FAMILY_UNSPECIFIED", | |
225 net::ADDRESS_FAMILY_UNSPECIFIED); | |
226 dict->SetInteger("ADDRESS_FAMILY_IPV4", | |
227 net::ADDRESS_FAMILY_IPV4); | |
228 dict->SetInteger("ADDRESS_FAMILY_IPV6", | |
229 net::ADDRESS_FAMILY_IPV6); | |
230 | |
231 constants_dict->Set("addressFamily", dict); | |
232 } | |
233 | |
234 // Information about how the "time ticks" values we have given it relate to | |
235 // actual system times. (We used time ticks throughout since they are stable | |
236 // across system clock changes). | |
237 { | |
238 int64 cur_time_ms = (base::Time::Now() - base::Time()).InMilliseconds(); | |
239 | |
240 int64 cur_time_ticks_ms = | |
241 (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds(); | |
242 | |
243 // If we add this number to a time tick value, it gives the timestamp. | |
244 int64 tick_to_time_ms = cur_time_ms - cur_time_ticks_ms; | |
245 | |
246 // Chrome on all platforms stores times using the Windows epoch | |
247 // (Jan 1 1601), but the javascript wants a unix epoch. | |
248 // TODO(eroman): Getting the timestamp relative to the unix epoch should | |
249 // be part of the time library. | |
250 const int64 kUnixEpochMs = 11644473600000LL; | |
251 int64 tick_to_unix_time_ms = tick_to_time_ms - kUnixEpochMs; | |
252 | |
253 // Pass it as a string, since it may be too large to fit in an integer. | |
254 constants_dict->SetString("timeTickOffset", | |
255 base::Int64ToString(tick_to_unix_time_ms)); | |
256 } | |
257 | |
258 // "clientInfo" key is required for some NetLogLogger log readers. | |
259 // Provide a default empty value for compatibility. | |
260 constants_dict->Set("clientInfo", new base::DictionaryValue()); | |
261 | |
262 // Add a list of active field experiments. | |
263 { | |
264 base::FieldTrial::ActiveGroups active_groups; | |
265 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | |
266 base::ListValue* field_trial_groups = new base::ListValue(); | |
267 for (base::FieldTrial::ActiveGroups::const_iterator it = | |
268 active_groups.begin(); | |
269 it != active_groups.end(); ++it) { | |
270 field_trial_groups->AppendString(it->trial_name + ":" + | |
271 it->group_name); | |
272 } | |
273 constants_dict->Set("activeFieldTrialGroups", field_trial_groups); | |
274 } | |
275 | |
276 return constants_dict; | |
277 } | 66 } |
278 | 67 |
279 } // namespace net | 68 } // namespace net |
OLD | NEW |