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 NET_BASE_NET_LOG_LOGGER_H_
6 #define NET_BASE_NET_LOG_LOGGER_H_
10 #include "base/files/scoped_file.h"
11 #include "base/macros.h"
12 #include "net/base/net_log.h"
15 class DictionaryValue
;
22 // NetLogLogger watches the NetLog event stream, and sends all entries to
23 // a file specified on creation.
25 // The text file will contain a single JSON object.
26 class NET_EXPORT NetLogLogger
: public NetLog::ThreadSafeObserver
{
28 // Takes ownership of |file| and will write network events to it once logging
29 // starts. |file| must be non-NULL handle and be open for writing.
30 // |constants| is a legend for decoding constant values used in the log.
31 NetLogLogger(FILE* file
, const base::Value
& constants
);
32 ~NetLogLogger() override
;
34 // Sets the log level to log at. Must be called before StartObserving.
35 void set_log_level(NetLog::LogLevel log_level
);
37 // Starts observing specified NetLog. Must not already be watching a NetLog.
38 // Separate from constructor to enforce thread safety.
39 void StartObserving(NetLog
* net_log
);
41 // Stops observing net_log(). Must already be watching.
44 // net::NetLog::ThreadSafeObserver implementation:
45 void OnAddEntry(const NetLog::Entry
& entry
) override
;
47 // Create a dictionary containing legend for net/ constants. Caller takes
48 // ownership of returned value.
49 // TODO(mmenke): Get rid of this, and have embedders use GetNetConstants
50 // directly. Also maybe call that function by default, so only embedders
51 // that need more constants need to worry about it.
52 static base::DictionaryValue
* GetConstants();
55 base::ScopedFILE file_
;
57 // The LogLevel to log at.
58 NetLog::LogLevel log_level_
;
60 // True if OnAddEntry() has been called at least once.
63 DISALLOW_COPY_AND_ASSIGN(NetLogLogger
);
68 #endif // NET_BASE_NET_LOG_LOGGER_H_