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
3// found in the LICENSE file.
4
5#ifndef NET_LOG_NET_LOG_WITH_SOURCE_H_
6#define NET_LOG_NET_LOG_WITH_SOURCE_H_
7
8#include "net/base/net_export.h"
9#include "net/log/net_log_event_type.h"
10#include "net/log/net_log_parameters_callback.h"
11#include "net/log/net_log_source.h"
12#include "net/log/net_log_source_type.h"
13
14namespace net {
15
16class NetLog;
17
18// Helper that binds a Source to a NetLog, and exposes convenience methods to
19// output log messages without needing to pass in the source.
20class NET_EXPORT NetLogWithSource {
21 public:
22 NetLogWithSource() : net_log_(nullptr) {}
23 ~NetLogWithSource();
24
25 // Add a log entry to the NetLog for the bound source.
26 void AddEntry(NetLogEventType type, NetLogEventPhase phase) const;
27 void AddEntry(NetLogEventType type,
28 NetLogEventPhase phase,
29 const NetLogParametersCallback& get_parameters) const;
30
31 // Convenience methods that call AddEntry with a fixed "capture phase"
32 // (begin, end, or none).
33 void BeginEvent(NetLogEventType type) const;
34 void BeginEvent(NetLogEventType type,
35 const NetLogParametersCallback& get_parameters) const;
36
37 void EndEvent(NetLogEventType type) const;
38 void EndEvent(NetLogEventType type,
39 const NetLogParametersCallback& get_parameters) const;
40
41 void AddEvent(NetLogEventType type) const;
42 void AddEvent(NetLogEventType type,
43 const NetLogParametersCallback& get_parameters) const;
44
45 // Just like AddEvent, except |net_error| is a net error code. A parameter
46 // called "net_error" with the indicated value will be recorded for the event.
47 // |net_error| must be negative, and not ERR_IO_PENDING, as it's not a true
48 // error.
49 void AddEventWithNetErrorCode(NetLogEventType event_type,
50 int net_error) const;
51
52 // Just like EndEvent, except |net_error| is a net error code. If it's
53 // negative, a parameter called "net_error" with a value of |net_error| is
54 // associated with the event. Otherwise, the end event has no parameters.
55 // |net_error| must not be ERR_IO_PENDING, as it's not a true error.
56 void EndEventWithNetErrorCode(NetLogEventType event_type,
57 int net_error) const;
58
59 // Logs a byte transfer event to the NetLog. Determines whether to log the
60 // received bytes or not based on the current logging level.
61 void AddByteTransferEvent(NetLogEventType event_type,
62 int byte_count,
63 const char* bytes) const;
64
65 bool IsCapturing() const;
66
67 // Helper to create a NetLogWithSource given a NetLog and a NetLogSourceType.
68 // Takes care of creating a unique source ID, and handles
69 // the case of NULL net_log.
70 static NetLogWithSource Make(NetLog* net_log, NetLogSourceType source_type);
71
72 // TODO(eroman): Temporary until crbug.com/467797 is solved.
73 void CrashIfInvalid() const;
74
75 const NetLogSource& source() const { return source_; }
76 NetLog* net_log() const { return net_log_; }
77
78 private:
79 // TODO(eroman): Temporary until crbug.com/467797 is solved.
80 enum Liveness {
81 ALIVE = 0xCA11AB13,
82 DEAD = 0xDEADBEEF,
83 };
84
85 NetLogWithSource(const NetLogSource& source, NetLog* net_log)
86 : source_(source), net_log_(net_log) {}
87
88 NetLogSource source_;
89 NetLog* net_log_;
90
91 // TODO(eroman): Temporary until crbug.com/467797 is solved.
92 Liveness liveness_ = ALIVE;
93};
94
95} // namespace net
96
97#endif // NET_LOG_NET_LOG_WITH_SOURCE_H_
98