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 SequencedTaskRunner; |
19 } // namespace base | 19 } // namespace base |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 | 22 |
23 class NetLogCaptureMode; | 23 class NetLogCaptureMode; |
24 | 24 |
25 // FileNetLogObserver watches the NetLog event stream and sends all entries to | 25 // FileNetLogObserver watches the NetLog event stream and sends all entries to |
26 // 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 |
27 // (unbounded mode). | 27 // (unbounded mode). |
28 // | 28 // |
(...skipping 13 matching lines...) Expand all Loading... |
42 // 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. |
43 // | 43 // |
44 // The consumer must call StartObserving before calling StopObserving, and must | 44 // The consumer must call StartObserving before calling StopObserving, and must |
45 // call each method exactly once in the lifetime of the observer. StartObserving | 45 // call each method exactly once in the lifetime of the observer. StartObserving |
46 // and StopObserving must be called on the same thread, but there is no | 46 // and StopObserving must be called on the same thread, but there is no |
47 // restriction on which thread is used. | 47 // restriction on which thread is used. |
48 class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver { | 48 class NET_EXPORT FileNetLogObserver : public NetLog::ThreadSafeObserver { |
49 public: | 49 public: |
50 // Creates a FileNetLogObserver in bounded mode. | 50 // Creates a FileNetLogObserver in bounded mode. |
51 // | 51 // |
52 // |file_task_runner| indicates the task runner that should be used to post | 52 // |directory| is the directory where the log files will be written to. |
53 // tasks from the main thread to the file thread. | |
54 // | |
55 // |directory| is the directory where the log files will be. | |
56 // | 53 // |
57 // |max_total_size| is the approximate limit on the cumulative size of all | 54 // |max_total_size| is the approximate limit on the cumulative size of all |
58 // netlog files. | 55 // netlog files. |
59 // | 56 // |
60 // |total_num_files| sets the total number of event files that are used to | 57 // |total_num_files| sets the total number of event files that are used to |
61 // write the events. It must be greater than 0. | 58 // write the events. It must be greater than 0. |
62 // | 59 // |
63 // |constants| is an optional legend for decoding constant values used in | 60 // |constants| is an optional legend for decoding constant values used in |
64 // the log. It should generally be a modified version of GetNetConstants(). | 61 // the log. It should generally be a modified version of GetNetConstants(). |
65 // If not present, the output of GetNetConstants() will be used. | 62 // If not present, the output of GetNetConstants() will be used. |
66 static std::unique_ptr<FileNetLogObserver> CreateBounded( | 63 static std::unique_ptr<FileNetLogObserver> CreateBounded( |
67 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | |
68 const base::FilePath& directory, | 64 const base::FilePath& directory, |
69 size_t max_total_size, | 65 size_t max_total_size, |
70 size_t total_num_files, | 66 size_t total_num_files, |
71 std::unique_ptr<base::Value> constants); | 67 std::unique_ptr<base::Value> constants); |
72 | 68 |
73 // Creates a FileNetLogObserver in unbounded mode. | 69 // Creates a FileNetLogObserver in unbounded mode. |
74 // | 70 // |
75 // |file_task_runner| indicates the task runner that should be used to post | 71 // |log_path| is where the log file will be written to. |
76 // tasks from the main thread to the file thread. | |
77 // | |
78 // |log_path| is where the log file will be. | |
79 // | 72 // |
80 // |constants| is an optional legend for decoding constant values used in | 73 // |constants| is an optional legend for decoding constant values used in |
81 // the log. It should generally be a modified version of GetNetConstants(). | 74 // the log. It should generally be a modified version of GetNetConstants(). |
82 // If not present, the output of GetNetConstants() will be used. | 75 // If not present, the output of GetNetConstants() will be used. |
83 static std::unique_ptr<FileNetLogObserver> CreateUnbounded( | 76 static std::unique_ptr<FileNetLogObserver> CreateUnbounded( |
84 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | |
85 const base::FilePath& log_path, | 77 const base::FilePath& log_path, |
86 std::unique_ptr<base::Value> constants); | 78 std::unique_ptr<base::Value> constants); |
87 | 79 |
88 ~FileNetLogObserver() override; | 80 ~FileNetLogObserver() override; |
89 | 81 |
90 // Attaches this observer to |net_log| and begins observing events. | 82 // Attaches this observer to |net_log| and begins observing events. |
91 void StartObserving(NetLog* net_log, NetLogCaptureMode capture_mode); | 83 void StartObserving(NetLog* net_log, NetLogCaptureMode capture_mode); |
92 | 84 |
93 // Stops observing net_log() and closes the output file(s). Must be called | 85 // Stops observing net_log() and closes the output file(s). Must be called |
94 // after StartObserving. Should be called before destruction of the | 86 // after StartObserving. Should be called before destruction of the |
(...skipping 11 matching lines...) Expand all Loading... |
106 | 98 |
107 // NetLog::ThreadSafeObserver | 99 // NetLog::ThreadSafeObserver |
108 void OnAddEntry(const NetLogEntry& entry) override; | 100 void OnAddEntry(const NetLogEntry& entry) override; |
109 | 101 |
110 private: | 102 private: |
111 class WriteQueue; | 103 class WriteQueue; |
112 class FileWriter; | 104 class FileWriter; |
113 class BoundedFileWriter; | 105 class BoundedFileWriter; |
114 class UnboundedFileWriter; | 106 class UnboundedFileWriter; |
115 | 107 |
116 FileNetLogObserver( | 108 FileNetLogObserver(scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
117 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 109 std::unique_ptr<FileWriter> file_writer, |
118 std::unique_ptr<FileWriter> file_writer, | 110 scoped_refptr<WriteQueue> write_queue, |
119 scoped_refptr<WriteQueue> write_queue, | 111 std::unique_ptr<base::Value> constants); |
120 std::unique_ptr<base::Value> constants); | |
121 | 112 |
122 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 113 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
123 | 114 |
124 // The |write_queue_| object is shared between the file thread and the main | 115 // The |write_queue_| object is shared between the file task runner and the |
125 // thread, and should be alive for the entirety of the observer's lifetime. | 116 // main thread, and should be alive for the entirety of the observer's |
126 // It should be destroyed once both the observer has been destroyed and all | 117 // lifetime. It should be destroyed once both the observer has been destroyed |
127 // tasks posted to the file thread have completed. | 118 // and all tasks posted to the file task runner have completed. |
128 scoped_refptr<WriteQueue> write_queue_; | 119 scoped_refptr<WriteQueue> write_queue_; |
129 | 120 |
130 // This is the owning reference to a file thread object. The observer is | 121 // The FileNetLogObserver is shared between the main thread and |
131 // responsible for destroying the file thread object by posting a task from | 122 // |file_task_runner_|. |
132 // the main thread to the file thread to destroy the FileWriter when the | |
133 // observer is destroyed. | |
134 // | 123 // |
135 // The use of base::Unretained with |file_writer_| to post tasks to the file | 124 // Conceptually FileNetLogObserver owns it, however on destruction its |
136 // thread is safe because the FileWriter object will be alive until the | 125 // deletion is deferred until outstanding tasks on |file_task_runner_| have |
137 // observer's destruction. | 126 // finished (since it is posted using base::Unretained()). |
138 FileWriter* file_writer_; | 127 std::unique_ptr<FileWriter> file_writer_; |
139 | 128 |
140 DISALLOW_COPY_AND_ASSIGN(FileNetLogObserver); | 129 DISALLOW_COPY_AND_ASSIGN(FileNetLogObserver); |
141 }; | 130 }; |
142 | 131 |
143 } // namespace net | 132 } // namespace net |
144 | 133 |
145 #endif // NET_LOG_FILE_NET_LOG_OBSERVER_H_ | 134 #endif // NET_LOG_FILE_NET_LOG_OBSERVER_H_ |
OLD | NEW |