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
3 // found in the LICENSE file.
5 #ifndef WRITE_TO_FILE_NET_LOG_OBSERVER_H_
6 #define WRITE_TO_FILE_NET_LOG_OBSERVER_H_
10 #include "base/files/scoped_file.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "net/log/net_log.h"
16 class DictionaryValue
;
23 class URLRequestContext
;
25 // WriteToFileNetLogObserver watches the NetLog event stream, and sends all
26 // entries to a file specified on creation.
28 // The text file will contain a single JSON object.
29 class NET_EXPORT WriteToFileNetLogObserver
: public NetLog::ThreadSafeObserver
{
31 WriteToFileNetLogObserver();
32 ~WriteToFileNetLogObserver() override
;
34 // Sets the capture mode to log at. Must be called before StartObserving.
35 void set_capture_mode(NetLogCaptureMode capture_mode
);
37 // Starts observing |net_log| and writes output to |file|. Must not already
38 // be watching a NetLog. Separate from constructor to enforce thread safety.
40 // |file| must be a non-NULL empty file that's open for writing.
42 // |constants| is an optional legend for decoding constant values used in the
43 // log. It should generally be a modified version of GetNetConstants(). If
44 // not present, the output of GetNetConstants() will be used.
46 // |url_request_context| is an optional URLRequestContext that will be used to
47 // pre-populate the log with information about in-progress events.
48 // If the context is non-NULL, this must be called on the context's thread.
49 void StartObserving(NetLog
* net_log
,
50 base::ScopedFILE file
,
51 base::Value
* constants
,
52 URLRequestContext
* url_request_context
);
54 // Stops observing net_log(). Must already be watching. Must be called
55 // before destruction of the WriteToFileNetLogObserver and the NetLog.
57 // |url_request_context| is an optional argument used to added additional
58 // network stack state to the log. If the context is non-NULL, this must be
59 // called on the context's thread.
60 void StopObserving(URLRequestContext
* url_request_context
);
62 // net::NetLog::ThreadSafeObserver implementation:
63 void OnAddEntry(const NetLog::Entry
& entry
) override
;
66 base::ScopedFILE file_
;
68 // The capture mode to log at.
69 NetLogCaptureMode capture_mode_
;
71 // True if OnAddEntry() has been called at least once.
74 DISALLOW_COPY_AND_ASSIGN(WriteToFileNetLogObserver
);
79 #endif // WRITE_TO_FILE_NET_LOG_OBSERVER_H_