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