• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
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 "base/memory/raw_ptr.h"
9 #include "net/base/net_export.h"
10 #include "net/log/net_log.h"
11 #include "net/log/net_log_event_type.h"
12 #include "net/log/net_log_source.h"
13 #include "net/log/net_log_source_type.h"
14 
15 namespace net {
16 class 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.
20 class NET_EXPORT NetLogWithSource {
21  public:
22   NetLogWithSource();
23 
24   // Adds a log entry to the NetLog for the bound source.
25   void AddEntry(NetLogEventType type, NetLogEventPhase phase) const;
26 
27   // See "Materializing parameters" in net_log.h for details on |get_params|.
28   template <typename ParametersCallback>
AddEntry(NetLogEventType type,NetLogEventPhase phase,const ParametersCallback & get_params)29   void AddEntry(NetLogEventType type,
30                 NetLogEventPhase phase,
31                 const ParametersCallback& get_params) const {
32     non_null_net_log_->AddEntry(type, source_, phase, get_params);
33   }
34 
35   // Convenience methods that call AddEntry with a fixed "capture phase"
36   // (begin, end, or none).
37   void BeginEvent(NetLogEventType type) const;
38 
39   // See "Materializing parameters" in net_log.h for details on |get_params|.
40   template <typename ParametersCallback>
BeginEvent(NetLogEventType type,const ParametersCallback & get_params)41   void BeginEvent(NetLogEventType type,
42                   const ParametersCallback& get_params) const {
43     AddEntry(type, NetLogEventPhase::BEGIN, get_params);
44   }
45 
46   void EndEvent(NetLogEventType type) const;
47 
48   // See "Materializing parameters" in net_log.h for details on |get_params|.
49   template <typename ParametersCallback>
EndEvent(NetLogEventType type,const ParametersCallback & get_params)50   void EndEvent(NetLogEventType type,
51                 const ParametersCallback& get_params) const {
52     AddEntry(type, NetLogEventPhase::END, get_params);
53   }
54 
55   void AddEvent(NetLogEventType type) const;
56 
57   // See "Materializing parameters" in net_log.h for details on |get_params|.
58   template <typename ParametersCallback>
AddEvent(NetLogEventType type,const ParametersCallback & get_params)59   void AddEvent(NetLogEventType type,
60                 const ParametersCallback& get_params) const {
61     AddEntry(type, NetLogEventPhase::NONE, get_params);
62   }
63 
64   void AddEventWithStringParams(NetLogEventType type,
65                                 std::string_view name,
66                                 std::string_view value) const;
67 
68   void AddEventWithIntParams(NetLogEventType type,
69                              std::string_view name,
70                              int value) const;
71 
72   void BeginEventWithIntParams(NetLogEventType type,
73                                std::string_view name,
74                                int value) const;
75 
76   void EndEventWithIntParams(NetLogEventType type,
77                              std::string_view name,
78                              int value) const;
79 
80   void AddEventWithInt64Params(NetLogEventType type,
81                                std::string_view name,
82                                int64_t value) const;
83 
84   void BeginEventWithStringParams(NetLogEventType type,
85                                   std::string_view name,
86                                   std::string_view value) const;
87 
88   void AddEventReferencingSource(NetLogEventType type,
89                                  const NetLogSource& source) const;
90 
91   void BeginEventReferencingSource(NetLogEventType type,
92                                    const NetLogSource& source) const;
93 
94   // Just like AddEvent, except |net_error| is a net error code.  A parameter
95   // called "net_error" with the indicated value will be recorded for the event.
96   // |net_error| must be negative, and not ERR_IO_PENDING, as it's not a true
97   // error.
98   void AddEventWithNetErrorCode(NetLogEventType event_type,
99                                 int net_error) const;
100 
101   // Just like EndEvent, except |net_error| is a net error code.  If it's
102   // negative, a parameter called "net_error" with a value of |net_error| is
103   // associated with the event.  Otherwise, the end event has no parameters.
104   // |net_error| must not be ERR_IO_PENDING, as it's not a true error.
105   void EndEventWithNetErrorCode(NetLogEventType event_type,
106                                 int net_error) const;
107 
108   void AddEntryWithBoolParams(NetLogEventType type,
109                               NetLogEventPhase phase,
110                               std::string_view name,
111                               bool value) const;
112 
113   // Logs a byte transfer event to the NetLog.  Determines whether to log the
114   // received bytes or not based on the current logging level.
115   void AddByteTransferEvent(NetLogEventType event_type,
116                             int byte_count,
117                             const char* bytes) const;
118 
IsCapturing()119   bool IsCapturing() const { return non_null_net_log_->IsCapturing(); }
120 
121   // Helper to create a NetLogWithSource given a NetLog and a NetLogSourceType.
122   // Takes care of creating a unique source ID, and handles the case of NULL
123   // net_log.
124   static NetLogWithSource Make(NetLog* net_log, NetLogSourceType source_type);
125 
126   // Helper to create a NetLogWithSource given a NetLogSourceType.
127   // Equivalent to calling Make(NetLog*, NetLogSourceType) with NetLog::Get()
128   static NetLogWithSource Make(NetLogSourceType source_type);
129 
130   // Creates a NetLogWithSource with an already initialized NetLogSource. If
131   // |net_log| is null or |source| is not valid, creates an unbound
132   // NetLogWithSource.
133   static NetLogWithSource Make(NetLog* net_log, const NetLogSource& source);
134 
135   // Creates a NetLogWithSource with an already initialized NetLogSource.
136   // Equivalent to calling Make(NetLog*, NetLogSource&) with NetLog::Get().
137   // If |source| is not valid, creates an unbound NetLogWithSource.
138   static NetLogWithSource Make(const NetLogSource& source);
139 
source()140   const NetLogSource& source() const { return source_; }
141 
142   // Returns the bound NetLog*, or nullptr.
143   NetLog* net_log() const;
144 
145  private:
NetLogWithSource(const NetLogSource & source,NetLog * non_null_net_log)146   NetLogWithSource(const NetLogSource& source, NetLog* non_null_net_log)
147       : source_(source), non_null_net_log_(non_null_net_log) {}
148 
149   NetLogSource source_;
150 
151   // There are two types of NetLogWithSource:
152   //
153   // (a) An ordinary NetLogWithSource for which |source().IsValid()| and
154   //     |net_log() != nullptr|
155   //
156   // (b) A default constructed NetLogWithSource for which
157   //     |!source().IsValid()| and |net_log() == nullptr|.
158   //
159   // As an optimization, both types internally store a non-null NetLog*. This
160   // way no null checks are needed before dispatching to the (possibly dummy)
161   // NetLog
162   raw_ptr<NetLog> non_null_net_log_;
163 };
164 
165 }  // namespace net
166 
167 #endif  // NET_LOG_NET_LOG_WITH_SOURCE_H_
168