OLD | NEW |
---|---|
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 // Constructs a FileNetLogObserver in bounded mode. | |
eroman
2017/02/21 22:14:23
I generally like the split of file allocation vs s
wangyix1
2017/02/22 01:28:18
Done, I went with factory methods.
| |
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 FileNetLogObserver( | |
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 // Constructs 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 FileNetLogObserver( |
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. | 88 |
97 void StartObservingUnbounded(NetLog* net_log, | 89 ~FileNetLogObserver() override; |
98 NetLogCaptureMode capture_mode, | 90 |
99 const base::FilePath& filepath, | 91 // Attach this observer to |net_log| and begin observing events. |
100 std::unique_ptr<base::Value> constants, | 92 void StartObserving(NetLog* net_log, NetLogCaptureMode capture_mode); |
101 URLRequestContext* url_request_context); | |
102 | 93 |
103 // Stops observing net_log(). Must be called after StartObservingBounded/ | 94 // Stops observing net_log(). Must be called after StartObservingBounded/ |
eroman
2017/02/21 22:14:23
(a) StartObservingBounded/ StartObservingUnbounded
wangyix1
2017/02/22 01:28:18
Done.
| |
104 // StartObservingUnbounded. Should be called before destruction of the | 95 // StartObservingUnbounded. 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 void InitializeFileWriter(std::unique_ptr<base::Value> constants); |
127 // StartObservingUnbounded(). | |
128 void StartObservingHelper(NetLog* net_log, | |
129 NetLogCaptureMode capture_mode, | |
130 std::unique_ptr<base::Value> constants, | |
131 URLRequestContext* url_request_context); | |
132 | 118 |
133 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 119 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
134 | 120 |
135 // The |write_queue_| object is shared between the file thread and the main | 121 // 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. | 122 // 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 | 123 // It should be destroyed once both the observer has been destroyed and all |
138 // tasks posted to the file thread have completed. | 124 // tasks posted to the file thread have completed. |
139 scoped_refptr<WriteQueue> write_queue_; | 125 scoped_refptr<WriteQueue> write_queue_; |
140 | 126 |
141 // This is the owning reference to a file thread object. The observer is | 127 // 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 | 128 // 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 | 129 // the main thread to the file thread to destroy the FileWriter when the |
144 // observer is destroyed. | 130 // observer is destroyed. |
145 // | 131 // |
146 // The use of base::Unretained with |file_writer_| to post tasks to the file | 132 // 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 | 133 // thread is safe because the FileWriter object will be alive until the |
148 // observer's destruction. | 134 // observer's destruction. |
149 FileWriter* file_writer_; | 135 FileWriter* file_writer_; |
150 | 136 |
151 DISALLOW_COPY_AND_ASSIGN(FileNetLogObserver); | 137 DISALLOW_COPY_AND_ASSIGN(FileNetLogObserver); |
152 }; | 138 }; |
153 | 139 |
154 } // namespace net | 140 } // namespace net |
155 | 141 |
156 #endif // NET_LOG_FILE_NET_LOG_OBSERVER_H_ | 142 #endif // NET_LOG_FILE_NET_LOG_OBSERVER_H_ |
OLD | NEW |