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

Side by Side Diff: net/log/file_net_log_observer.h

Issue 2698143004: Add ongoing events to net-export log when logging starts (Closed)
Patch Set: Forgot to update comment for StopNetLog() Created 3 years, 10 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #ifndef NET_LOG_FILE_NET_LOG_OBSERVER_H_ 5 #ifndef NET_LOG_FILE_NET_LOG_OBSERVER_H_
6 #define NET_LOG_FILE_NET_LOG_OBSERVER_H_ 6 #define NET_LOG_FILE_NET_LOG_OBSERVER_H_
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "net/base/net_export.h" 12 #include "net/base/net_export.h"
13 #include "net/log/net_log.h" 13 #include "net/log/net_log.h"
14 14
15 namespace base { 15 namespace base {
16 class Value; 16 class Value;
17 class FilePath; 17 class FilePath;
18 class SingleThreadTaskRunner; 18 class SingleThreadTaskRunner;
19 } // namespace base 19 } // namespace base
20 20
21 namespace net { 21 namespace net {
22 22
23 class NetLogCaptureMode; 23 class NetLogCaptureMode;
24 class URLRequestContext;
25 24
26 // FileNetLogObserver watches the NetLog event stream and sends all entries to 25 // FileNetLogObserver watches the NetLog event stream and sends all entries to
27 // either a group of files in a directory (bounded mode) or to a single file 26 // either a group of files in a directory (bounded mode) or to a single file
28 // (unbounded mode). 27 // (unbounded mode).
29 // 28 //
30 // Bounded mode: 29 // Bounded mode:
31 // The events are written to a single JSON object that is split across the 30 // The events are written to a single JSON object that is split across the
32 // files, and the files must be stitched together once the observation period 31 // files, and the files must be stitched together once the observation period
33 // is over. The first file is constants.json, followed by a consumer-specified 32 // is over. The first file is constants.json, followed by a consumer-specified
34 // number of event files named event_file_<index>.json, and the last file is 33 // number of event files named event_file_<index>.json, and the last file is
35 // end_netlog.json. 34 // end_netlog.json.
36 // 35 //
37 // The user is able to specify an approximate maximum cumulative size for the 36 // The user is able to specify an approximate maximum cumulative size for the
38 // netlog files and the observer overwrites old events when the maximum file 37 // netlog files and the observer overwrites old events when the maximum file
39 // size is reached. 38 // size is reached.
40 // 39 //
41 // Unbounded mode: 40 // Unbounded mode:
42 // The entire JSON object is put into one file. There is no size limit to how 41 // The entire JSON object is put into one file. There is no size limit to how
43 // large this file can grow; all events added will be written to the file. 42 // large this file can grow; all events added will be written to the file.
44 // 43 //
45 // The consumer must call StartObservingBounded/StartObservingUnbounded before 44 // The consumer must call StartObservingBounded/StartObservingUnbounded before
46 // calling StopObserving, and must call each method exactly once in the lifetime 45 // calling StopObserving, and must call each method exactly once in the lifetime
47 // of the observer. StartObservingBounded/StartObservingUnbounded and 46 // of the observer. StartObservingBounded/StartObservingUnbounded and
48 // StopObserving must be called on the same thread, but there is no restriction 47 // StopObserving must be called on the same thread, but there is no restriction
49 // on which thread is used. 48 // on which thread is used.
50 class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver { 49 class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver {
51 public: 50 public:
51 // Creates a FileNetLogObserver in bounded mode.
52 //
52 // |file_task_runner| indicates the task runner that should be used to post 53 // |file_task_runner| indicates the task runner that should be used to post
53 // tasks from the main thread to the file thread. 54 // tasks from the main thread to the file thread.
54 explicit FileNetLogObserver( 55 //
55 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); 56 // |directory| is the directory where the log files will be.
56
57 ~FileNetLogObserver() override;
58
59 // Starts observing |net_log| in bounded mode and writes output files to
60 // |directory|.
61 // May only be called once in the lifetime of the object.
62 // 57 //
63 // |max_total_size| is the approximate limit on the cumulative size of all 58 // |max_total_size| is the approximate limit on the cumulative size of all
64 // netlog files. 59 // netlog files.
65 // 60 //
66 // |total_num_files| sets the total number of event files that are used to 61 // |total_num_files| sets the total number of event files that are used to
67 // write the events. It must be greater than 0. 62 // write the events. It must be greater than 0.
68 // 63 //
69 // |constants| is an optional legend for decoding constant values used in 64 // |constants| is an optional legend for decoding constant values used in
70 // the log. It should generally be a modified version of GetNetConstants(). 65 // the log. It should generally be a modified version of GetNetConstants().
71 // If not present, the output of GetNetConstants() will be used. 66 // If not present, the output of GetNetConstants() will be used.
67 static std::unique_ptr<FileNetLogObserver> CreateBounded(
68 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
69 const base::FilePath& directory,
70 size_t max_total_size,
71 size_t total_num_files,
72 std::unique_ptr<base::Value> constants);
73
74 // Creates a FileNetLogObserver in unbounded mode.
72 // 75 //
73 // |url_request_context| is an optional URLRequestContext that will be used 76 // |file_task_runner| indicates the task runner that should be used to post
74 // to pre-populate the log with information about in-progress events. If the 77 // tasks from the main thread to the file thread.
75 // context is non-NULL, StartObservingBounded() must be called on the 78 //
76 // context's thread. 79 // |log_path| is where the log file will be.
77 void StartObservingBounded(NetLog* net_log,
78 NetLogCaptureMode capture_mode,
79 const base::FilePath& directory,
80 std::unique_ptr<base::Value> constants,
81 URLRequestContext* url_request_context,
82 size_t max_total_size,
83 size_t total_num_files);
84
85 // Starts observing |net_log| in unbounded mode and writes to the file at
86 // |filepath|.
87 // May only be called once in the lifetime of the object.
88 // 80 //
89 // |constants| is an optional legend for decoding constant values used in 81 // |constants| is an optional legend for decoding constant values used in
90 // the log. It should generally be a modified version of GetNetConstants(). 82 // the log. It should generally be a modified version of GetNetConstants().
91 // If not present, the output of GetNetConstants() will be used. 83 // If not present, the output of GetNetConstants() will be used.
92 // 84 static std::unique_ptr<FileNetLogObserver> CreateUnbounded(
93 // |url_request_context| is an optional URLRequestContext that will be used 85 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
94 // to pre-populate the log with information about in-progress events. If the 86 const base::FilePath& log_path,
95 // context is non-NULL, StartObservingUnbounded() must be called on 87 std::unique_ptr<base::Value> constants);
96 // the context's thread.
97 void StartObservingUnbounded(NetLog* net_log,
98 NetLogCaptureMode capture_mode,
99 const base::FilePath& filepath,
100 std::unique_ptr<base::Value> constants,
101 URLRequestContext* url_request_context);
102 88
103 // Stops observing net_log(). Must be called after StartObservingBounded/ 89 ~FileNetLogObserver() override;
104 // StartObservingUnbounded. Should be called before destruction of the 90
91 // Attach this observer to |net_log| and begin observing events.
eroman 2017/02/22 02:03:29 Attaches this observer to |net_log| and begins obs
wangyix1 2017/02/23 02:14:57 Done.
92 void StartObserving(NetLog* net_log, NetLogCaptureMode capture_mode);
93
94 // Stops observing net_log() and closes the output file(s). Must be called
95 // after StartObserving. Should be called before destruction of the
105 // FileNetLogObserver and the NetLog, or the NetLog files will be deleted when 96 // FileNetLogObserver and the NetLog, or the NetLog files will be deleted when
106 // the observer is destroyed. 97 // the observer is destroyed.
107 // 98 //
99 // |polled_data| is an optional argument used to add additional network stack
100 // state to the log.
101 //
108 // |callback| will be run on whichever thread StopObserving() was called on 102 // |callback| will be run on whichever thread StopObserving() was called on
109 // once all file writing is complete and the netlog files can be accessed 103 // once all file writing is complete and the netlog files can be accessed
110 // safely. 104 // safely.
111 //
112 // |polled_data| is an optional argument used to add additional network stack
113 // state to the log.
114 void StopObserving(std::unique_ptr<base::Value> polled_data, 105 void StopObserving(std::unique_ptr<base::Value> polled_data,
115 const base::Closure& callback); 106 const base::Closure& callback);
116 107
117 // NetLog::ThreadSafeObserver 108 // NetLog::ThreadSafeObserver
118 void OnAddEntry(const NetLogEntry& entry) override; 109 void OnAddEntry(const NetLogEntry& entry) override;
119 110
120 private: 111 private:
121 class WriteQueue; 112 class WriteQueue;
122 class FileWriter; 113 class FileWriter;
123 class BoundedFileWriter; 114 class BoundedFileWriter;
124 class UnboundedFileWriter; 115 class UnboundedFileWriter;
125 116
126 // Performs tasks common to both StartObservingBounded() and 117 // |file_writer| will be owned by the FileNetLogObserver instance.
127 // StartObservingUnbounded(). 118 FileNetLogObserver(
128 void StartObservingHelper(NetLog* net_log, 119 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
129 NetLogCaptureMode capture_mode, 120 FileWriter* file_writer,
eroman 2017/02/22 02:03:29 Can you change this parameter to std::unique_p
wangyix1 2017/02/23 02:14:57 Done.
130 std::unique_ptr<base::Value> constants, 121 size_t write_queue_memory_max,
eroman 2017/02/22 02:03:29 [optional] I suggest changing this parameter to be
wangyix1 2017/02/23 02:14:57 Done.
131 URLRequestContext* url_request_context); 122 std::unique_ptr<base::Value> constants);
132 123
133 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; 124 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
134 125
135 // The |write_queue_| object is shared between the file thread and the main 126 // The |write_queue_| object is shared between the file thread and the main
136 // thread, and should be alive for the entirety of the observer's lifetime. 127 // thread, and should be alive for the entirety of the observer's lifetime.
137 // It should be destroyed once both the observer has been destroyed and all 128 // It should be destroyed once both the observer has been destroyed and all
138 // tasks posted to the file thread have completed. 129 // tasks posted to the file thread have completed.
139 scoped_refptr<WriteQueue> write_queue_; 130 scoped_refptr<WriteQueue> write_queue_;
140 131
141 // This is the owning reference to a file thread object. The observer is 132 // This is the owning reference to a file thread object. The observer is
142 // responsible for destroying the file thread object by posting a task from 133 // responsible for destroying the file thread object by posting a task from
143 // the main thread to the file thread to destroy the FileWriter when the 134 // the main thread to the file thread to destroy the FileWriter when the
144 // observer is destroyed. 135 // observer is destroyed.
145 // 136 //
146 // The use of base::Unretained with |file_writer_| to post tasks to the file 137 // The use of base::Unretained with |file_writer_| to post tasks to the file
147 // thread is safe because the FileWriter object will be alive until the 138 // thread is safe because the FileWriter object will be alive until the
148 // observer's destruction. 139 // observer's destruction.
149 FileWriter* file_writer_; 140 FileWriter* file_writer_;
150 141
151 DISALLOW_COPY_AND_ASSIGN(FileNetLogObserver); 142 DISALLOW_COPY_AND_ASSIGN(FileNetLogObserver);
152 }; 143 };
153 144
154 } // namespace net 145 } // namespace net
155 146
156 #endif // NET_LOG_FILE_NET_LOG_OBSERVER_H_ 147 #endif // NET_LOG_FILE_NET_LOG_OBSERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698