Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(336)

Side by Side Diff: net/base/net_log_logger.cc

Issue 976483002: Add ability for NetLogLogger to gather data from more than just NetLog (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/net_log_logger.h ('k') | net/base/net_log_logger_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <set>
10
9 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
10 #include "base/logging.h" 12 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h" 14 #include "base/values.h"
13 #include "net/base/net_log_util.h" 15 #include "net/base/net_log_util.h"
16 #include "net/url_request/url_request_context.h"
14 17
15 namespace net { 18 namespace net {
16 19
17 NetLogLogger::NetLogLogger(FILE* file, const base::Value& constants) 20 NetLogLogger::NetLogLogger()
18 : file_(file), 21 : log_level_(NetLog::LOG_STRIP_PRIVATE_DATA), added_events_(false) {
19 log_level_(NetLog::LOG_STRIP_PRIVATE_DATA),
20 added_events_(false) {
21 DCHECK(file);
22
23 // Write constants to the output file. This allows loading files that have
24 // different source and event types, as they may be added and removed
25 // between Chrome versions.
26 std::string json;
27 base::JSONWriter::Write(&constants, &json);
28 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str());
29 fprintf(file_.get(), "\"events\": [\n");
30 } 22 }
31 23
32 NetLogLogger::~NetLogLogger() { 24 NetLogLogger::~NetLogLogger() {
33 if (file_.get())
34 fprintf(file_.get(), "]}");
35 } 25 }
36 26
37 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level) { 27 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level) {
38 DCHECK(!net_log()); 28 DCHECK(!net_log());
39 log_level_ = log_level; 29 log_level_ = log_level;
40 } 30 }
41 31
42 void NetLogLogger::StartObserving(net::NetLog* net_log) { 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
43 net_log->AddThreadSafeObserver(this, log_level_); 64 net_log->AddThreadSafeObserver(this, log_level_);
44 } 65 }
45 66
46 void NetLogLogger::StopObserving() { 67 void NetLogLogger::StopObserving(net::URLRequestContext* url_request_context) {
47 net_log()->RemoveThreadSafeObserver(this); 68 net_log()->RemoveThreadSafeObserver(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();
48 } 86 }
49 87
50 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) { 88 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) {
51 // Add a comma and newline for every event but the first. Newlines are needed 89 // Add a comma and newline for every event but the first. Newlines are needed
52 // so can load partial log files by just ignoring the last line. For this to 90 // so can load partial log files by just ignoring the last line. For this to
53 // work, lines cannot be pretty printed. 91 // work, lines cannot be pretty printed.
54 scoped_ptr<base::Value> value(entry.ToValue()); 92 scoped_ptr<base::Value> value(entry.ToValue());
55 std::string json; 93 std::string json;
56 base::JSONWriter::Write(value.get(), &json); 94 base::JSONWriter::Write(value.get(), &json);
57 fprintf(file_.get(), "%s%s", 95 fprintf(file_.get(), "%s%s",
58 (added_events_ ? ",\n" : ""), 96 (added_events_ ? ",\n" : ""),
59 json.c_str()); 97 json.c_str());
60 added_events_ = true; 98 added_events_ = true;
61 } 99 }
62 100
63 // static
64 base::DictionaryValue* NetLogLogger::GetConstants() {
65 return GetNetConstants().release();
66 }
67
68 } // namespace net 101 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_log_logger.h ('k') | net/base/net_log_logger_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698