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.
4
5#ifndef NET_BASE_NET_LOG_LOGGER_H_
6#define NET_BASE_NET_LOG_LOGGER_H_
7
8#include <stdio.h>
9
10#include "base/files/scoped_file.h"
11#include "base/macros.h"
12#include "net/base/net_log.h"
13
14namespace base {
15class FilePath;
16class Value;
17}
18
19namespace net {
20
21// NetLogLogger watches the NetLog event stream, and sends all entries to
22// a file specified on creation.
23//
24// The text file will contain a single JSON object.
25class NET_EXPORT NetLogLogger : public NetLog::ThreadSafeObserver {
26 public:
27  // Takes ownership of |file| and will write network events to it once logging
28  // starts.  |file| must be non-NULL handle and be open for writing.
29  // |constants| is a legend for decoding constant values used in the log.
30  NetLogLogger(FILE* file, const base::Value& constants);
31  virtual ~NetLogLogger();
32
33  // Sets the log level to log at. Must be called before StartObserving.
34  void set_log_level(NetLog::LogLevel log_level);
35
36  // Starts observing specified NetLog.  Must not already be watching a NetLog.
37  // Separate from constructor to enforce thread safety.
38  void StartObserving(NetLog* net_log);
39
40  // Stops observing net_log().  Must already be watching.
41  void StopObserving();
42
43  // net::NetLog::ThreadSafeObserver implementation:
44  virtual void OnAddEntry(const NetLog::Entry& entry) OVERRIDE;
45
46  // Create a dictionary containing legend for net/ constants.  Caller takes
47  // ownership of returned value.
48  static base::DictionaryValue* GetConstants();
49
50 private:
51  base::ScopedFILE file_;
52
53  // The LogLevel to log at.
54  NetLog::LogLevel log_level_;
55
56  // True if OnAddEntry() has been called at least once.
57  bool added_events_;
58
59  DISALLOW_COPY_AND_ASSIGN(NetLogLogger);
60};
61
62}  // namespace net
63
64#endif  // NET_BASE_NET_LOG_LOGGER_H_
65