summaryrefslogtreecommitdiffstats
path: root/net/base/net_log_logger.h
blob: ede3ca443db32839305e97af91699de692930baf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_BASE_NET_LOG_LOGGER_H_
#define NET_BASE_NET_LOG_LOGGER_H_

#include <stdio.h>

#include "base/files/scoped_file.h"
#include "base/macros.h"
#include "net/base/net_log.h"

namespace base {
class DictionaryValue;
class FilePath;
class Value;
}

namespace net {

// NetLogLogger watches the NetLog event stream, and sends all entries to
// a file specified on creation.
//
// The text file will contain a single JSON object.
class NET_EXPORT NetLogLogger : public NetLog::ThreadSafeObserver {
 public:
  // Takes ownership of |file| and will write network events to it once logging
  // starts.  |file| must be non-NULL handle and be open for writing.
  // |constants| is a legend for decoding constant values used in the log.
  NetLogLogger(FILE* file, const base::Value& constants);
  ~NetLogLogger() override;

  // Sets the log level to log at. Must be called before StartObserving.
  void set_log_level(NetLog::LogLevel log_level);

  // Starts observing specified NetLog.  Must not already be watching a NetLog.
  // Separate from constructor to enforce thread safety.
  void StartObserving(NetLog* net_log);

  // Stops observing net_log().  Must already be watching.
  void StopObserving();

  // net::NetLog::ThreadSafeObserver implementation:
  void OnAddEntry(const NetLog::Entry& entry) override;

  // Create a dictionary containing legend for net/ constants.  Caller takes
  // ownership of returned value.
  // TODO(mmenke):  Get rid of this, and have embedders use GetNetConstants
  // directly.  Also maybe call that function by default, so only embedders
  // that need more constants need to worry about it.
  static base::DictionaryValue* GetConstants();

 private:
  base::ScopedFILE file_;

  // The LogLevel to log at.
  NetLog::LogLevel log_level_;

  // True if OnAddEntry() has been called at least once.
  bool added_events_;

  DISALLOW_COPY_AND_ASSIGN(NetLogLogger);
};

}  // namespace net

#endif  // NET_BASE_NET_LOG_LOGGER_H_