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

Side by Side Diff: components/net_log/chrome_net_log.cc

Issue 2973673003: Use FileNetLogObserver for implementing --log-net-log. (Closed)
Patch Set: address comments Created 3 years, 5 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 | « components/net_log/chrome_net_log.h ('k') | components/net_log/net_export_file_writer.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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "components/net_log/chrome_net_log.h" 5 #include "components/net_log/chrome_net_log.h"
6 6
7 #include <stdio.h>
8 #include <utility> 7 #include <utility>
9 8
10 #include "base/command_line.h" 9 #include "base/command_line.h"
11 #include "base/files/scoped_file.h"
12 #include "base/logging.h"
13 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
14 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
15 #include "base/sys_info.h" 12 #include "base/sys_info.h"
16 #include "base/values.h" 13 #include "base/values.h"
17 #include "build/build_config.h" 14 #include "build/build_config.h"
18 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event _store.h" 15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event _store.h"
19 #include "components/net_log/net_export_file_writer.h" 16 #include "components/net_log/net_export_file_writer.h"
20 #include "components/version_info/version_info.h" 17 #include "components/version_info/version_info.h"
18 #include "net/log/file_net_log_observer.h"
21 #include "net/log/net_log_util.h" 19 #include "net/log/net_log_util.h"
22 #include "net/log/trace_net_log_observer.h" 20 #include "net/log/trace_net_log_observer.h"
23 #include "net/log/write_to_file_net_log_observer.h"
24 21
25 namespace net_log { 22 namespace net_log {
26 23
27 ChromeNetLog::ChromeNetLog() { 24 ChromeNetLog::ChromeNetLog() {
28 trace_net_log_observer_.reset(new net::TraceNetLogObserver()); 25 trace_net_log_observer_.reset(new net::TraceNetLogObserver());
29 trace_net_log_observer_->WatchForTraceStart(this); 26 trace_net_log_observer_->WatchForTraceStart(this);
30 } 27 }
31 28
32 ChromeNetLog::~ChromeNetLog() { 29 ChromeNetLog::~ChromeNetLog() {
33 net_export_file_writer_.reset(); 30 net_export_file_writer_.reset();
34 // Remove the observers we own before we're destroyed. 31 ClearFileNetLogObserver();
35 if (write_to_file_observer_) 32 trace_net_log_observer_->StopWatchForTraceStart();
36 write_to_file_observer_->StopObserving(nullptr);
37 if (trace_net_log_observer_)
38 trace_net_log_observer_->StopWatchForTraceStart();
39 } 33 }
40 34
41 void ChromeNetLog::StartWritingToFile( 35 void ChromeNetLog::StartWritingToFile(
42 const base::FilePath& log_file, 36 const base::FilePath& path,
43 net::NetLogCaptureMode log_file_mode, 37 net::NetLogCaptureMode capture_mode,
44 const base::CommandLine::StringType& command_line_string, 38 const base::CommandLine::StringType& command_line_string,
45 const std::string& channel_string) { 39 const std::string& channel_string) {
46 DCHECK(!log_file.empty()); 40 DCHECK(!path.empty());
47 41
48 // TODO(716570): Use common code to write NetLog to file. 42 // TODO(739485): The log file does not contain about:flags data.
43 file_net_log_observer_ = net::FileNetLogObserver::CreateUnbounded(
44 path, GetConstants(command_line_string, channel_string));
49 45
50 // Much like logging.h, bypass threading restrictions by using fopen 46 file_net_log_observer_->StartObserving(this, capture_mode);
51 // directly. Have to write on a thread that's shutdown to handle events on
52 // shutdown properly, and posting events to another thread as they occur
53 // would result in an unbounded buffer size, so not much can be gained by
54 // doing this on another thread. It's only used when debugging Chrome, so
55 // performance is not a big concern.
56 base::ScopedFILE file;
57 #if defined(OS_WIN)
58 file.reset(_wfopen(log_file.value().c_str(), L"w"));
59 #elif defined(OS_POSIX)
60 file.reset(fopen(log_file.value().c_str(), "w"));
61 #endif
62
63 if (!file) {
64 LOG(ERROR) << "Could not open file " << log_file.value()
65 << " for net logging";
66 } else {
67 std::unique_ptr<base::Value> constants(
68 GetConstants(command_line_string, channel_string));
69 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver());
70
71 write_to_file_observer_->set_capture_mode(log_file_mode);
72
73 write_to_file_observer_->StartObserving(this, std::move(file),
74 constants.get(), nullptr);
75 }
76 } 47 }
77 48
78 NetExportFileWriter* ChromeNetLog::net_export_file_writer() { 49 NetExportFileWriter* ChromeNetLog::net_export_file_writer() {
79 if (!net_export_file_writer_) 50 if (!net_export_file_writer_)
80 net_export_file_writer_ = base::WrapUnique(new NetExportFileWriter(this)); 51 net_export_file_writer_ = base::WrapUnique(new NetExportFileWriter(this));
81 return net_export_file_writer_.get(); 52 return net_export_file_writer_.get();
82 } 53 }
83 54
84 // static 55 // static
85 std::unique_ptr<base::Value> ChromeNetLog::GetConstants( 56 std::unique_ptr<base::Value> ChromeNetLog::GetConstants(
(...skipping 22 matching lines...) Expand all
108 dict->SetString("command_line", command_line_string); 79 dict->SetString("command_line", command_line_string);
109 80
110 constants_dict->Set("clientInfo", std::move(dict)); 81 constants_dict->Set("clientInfo", std::move(dict));
111 82
112 data_reduction_proxy::DataReductionProxyEventStore::AddConstants( 83 data_reduction_proxy::DataReductionProxyEventStore::AddConstants(
113 constants_dict.get()); 84 constants_dict.get());
114 85
115 return std::move(constants_dict); 86 return std::move(constants_dict);
116 } 87 }
117 88
89 void ChromeNetLog::ShutDownBeforeTaskScheduler() {
90 // TODO(eroman): Stop in-progress net_export_file_writer_ or delete its files?
91
92 ClearFileNetLogObserver();
93 }
94
95 void ChromeNetLog::ClearFileNetLogObserver() {
96 if (!file_net_log_observer_)
97 return;
98
99 // TODO(739487): The log file does not contain any polled data.
100 //
101 // TODO(eroman): FileNetLogObserver::StopObserving() posts to the file task
102 // runner to finish writing the log file. Despite that sequenced task runner
103 // being marked BLOCK_SHUTDOWN, those tasks are not actually running.
104 //
105 // This isn't a big deal when using the unbounded logger since the log
106 // loading code can handle such truncated logs. But this will need fixing
107 // if switching to log formats that are not so versatile (also if adding
108 // polled data).
109 file_net_log_observer_->StopObserving(nullptr /*polled_data*/,
110 base::Closure());
111 file_net_log_observer_.reset();
112 }
113
118 } // namespace net_log 114 } // namespace net_log
OLDNEW
« no previous file with comments | « components/net_log/chrome_net_log.h ('k') | components/net_log/net_export_file_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698