OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/log/net_log_logger.h" | |
6 | |
7 #include <stdio.h> | |
8 | |
9 #include <set> | |
10 | |
11 #include "base/json/json_writer.h" | |
12 #include "base/logging.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/values.h" | |
15 #include "net/log/net_log_util.h" | |
16 #include "net/url_request/url_request_context.h" | |
17 | |
18 namespace net { | |
19 | |
20 NetLogLogger::NetLogLogger() | |
21 : log_level_(NetLog::LOG_STRIP_PRIVATE_DATA), added_events_(false) { | |
22 } | |
23 | |
24 NetLogLogger::~NetLogLogger() { | |
25 } | |
26 | |
27 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level) { | |
28 DCHECK(!net_log()); | |
29 log_level_ = log_level; | |
30 } | |
31 | |
32 void NetLogLogger::StartObserving(net::NetLog* net_log, | |
33 base::ScopedFILE file, | |
34 base::Value* constants, | |
35 net::URLRequestContext* url_request_context) { | |
36 DCHECK(file.get()); | |
37 file_ = file.Pass(); | |
38 added_events_ = false; | |
39 | |
40 // Write constants to the output file. This allows loading files that have | |
41 // different source and event types, as they may be added and removed | |
42 // between Chrome versions. | |
43 std::string json; | |
44 if (constants) { | |
45 base::JSONWriter::Write(constants, &json); | |
46 } else { | |
47 scoped_ptr<base::DictionaryValue> scoped_constants(GetNetConstants()); | |
48 base::JSONWriter::Write(scoped_constants.get(), &json); | |
49 } | |
50 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str()); | |
51 | |
52 // Start events array. It's closed in StopObserving(). | |
53 fprintf(file_.get(), "\"events\": [\n"); | |
54 | |
55 // Add events for in progress requests if a context is given. | |
56 if (url_request_context) { | |
57 DCHECK(url_request_context->CalledOnValidThread()); | |
58 | |
59 std::set<URLRequestContext*> contexts; | |
60 contexts.insert(url_request_context); | |
61 CreateNetLogEntriesForActiveObjects(contexts, this); | |
62 } | |
63 | |
64 net_log->DeprecatedAddObserver(this, log_level_); | |
65 } | |
66 | |
67 void NetLogLogger::StopObserving(net::URLRequestContext* url_request_context) { | |
68 net_log()->DeprecatedRemoveObserver(this); | |
69 | |
70 // End events array. | |
71 fprintf(file_.get(), "]"); | |
72 | |
73 // Write state of the URLRequestContext when logging stopped. | |
74 if (url_request_context) { | |
75 DCHECK(url_request_context->CalledOnValidThread()); | |
76 | |
77 std::string json; | |
78 scoped_ptr<base::DictionaryValue> net_info = | |
79 GetNetInfo(url_request_context, NET_INFO_ALL_SOURCES); | |
80 base::JSONWriter::Write(net_info.get(), &json); | |
81 fprintf(file_.get(), ",\"tabInfo\": %s\n", json.c_str()); | |
82 } | |
83 fprintf(file_.get(), "}"); | |
84 | |
85 file_.reset(); | |
86 } | |
87 | |
88 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) { | |
89 // Add a comma and newline for every event but the first. Newlines are needed | |
90 // so can load partial log files by just ignoring the last line. For this to | |
91 // work, lines cannot be pretty printed. | |
92 scoped_ptr<base::Value> value(entry.ToValue()); | |
93 std::string json; | |
94 base::JSONWriter::Write(value.get(), &json); | |
95 fprintf(file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str()); | |
96 added_events_ = true; | |
97 } | |
98 | |
99 } // namespace net | |
OLD | NEW |