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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_LOG_NET_LOG_WITH_SOURCE_H_
#define NET_LOG_NET_LOG_WITH_SOURCE_H_
#include "base/memory/raw_ptr.h"
#include "net/base/net_export.h"
#include "net/log/net_log.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_source.h"
#include "net/log/net_log_source_type.h"
namespace net {
class NetLog;
// Helper that binds a Source to a NetLog, and exposes convenience methods to
// output log messages without needing to pass in the source.
class NET_EXPORT NetLogWithSource {
public:
NetLogWithSource();
// Adds a log entry to the NetLog for the bound source.
void AddEntry(NetLogEventType type, NetLogEventPhase phase) const;
// See "Materializing parameters" in net_log.h for details on |get_params|.
template <typename ParametersCallback>
void AddEntry(NetLogEventType type,
NetLogEventPhase phase,
const ParametersCallback& get_params) const {
non_null_net_log_->AddEntry(type, source_, phase, get_params);
}
// Convenience methods that call AddEntry with a fixed "capture phase"
// (begin, end, or none).
void BeginEvent(NetLogEventType type) const;
// See "Materializing parameters" in net_log.h for details on |get_params|.
template <typename ParametersCallback>
void BeginEvent(NetLogEventType type,
const ParametersCallback& get_params) const {
AddEntry(type, NetLogEventPhase::BEGIN, get_params);
}
void EndEvent(NetLogEventType type) const;
// See "Materializing parameters" in net_log.h for details on |get_params|.
template <typename ParametersCallback>
void EndEvent(NetLogEventType type,
const ParametersCallback& get_params) const {
AddEntry(type, NetLogEventPhase::END, get_params);
}
void AddEvent(NetLogEventType type) const;
// See "Materializing parameters" in net_log.h for details on |get_params|.
template <typename ParametersCallback>
void AddEvent(NetLogEventType type,
const ParametersCallback& get_params) const {
AddEntry(type, NetLogEventPhase::NONE, get_params);
}
void AddEventWithStringParams(NetLogEventType type,
std::string_view name,
std::string_view value) const;
void AddEventWithIntParams(NetLogEventType type,
std::string_view name,
int value) const;
void BeginEventWithIntParams(NetLogEventType type,
std::string_view name,
int value) const;
void EndEventWithIntParams(NetLogEventType type,
std::string_view name,
int value) const;
void AddEventWithInt64Params(NetLogEventType type,
std::string_view name,
int64_t value) const;
void BeginEventWithStringParams(NetLogEventType type,
std::string_view name,
std::string_view value) const;
void AddEventReferencingSource(NetLogEventType type,
const NetLogSource& source) const;
void BeginEventReferencingSource(NetLogEventType type,
const NetLogSource& source) const;
// Just like AddEvent, except |net_error| is a net error code. A parameter
// called "net_error" with the indicated value will be recorded for the event.
// |net_error| must be negative, and not ERR_IO_PENDING, as it's not a true
// error.
void AddEventWithNetErrorCode(NetLogEventType event_type,
int net_error) const;
// Just like EndEvent, except |net_error| is a net error code. If it's
// negative, a parameter called "net_error" with a value of |net_error| is
// associated with the event. Otherwise, the end event has no parameters.
// |net_error| must not be ERR_IO_PENDING, as it's not a true error.
void EndEventWithNetErrorCode(NetLogEventType event_type,
int net_error) const;
void AddEntryWithBoolParams(NetLogEventType type,
NetLogEventPhase phase,
std::string_view name,
bool value) const;
// Logs a byte transfer event to the NetLog. Determines whether to log the
// received bytes or not based on the current logging level.
void AddByteTransferEvent(NetLogEventType event_type,
base::span<const uint8_t> bytes) const;
// DEPRECATED: Use the above `base::span` variant to avoid unsafe buffer
// usage.
// TODO(https://crbug.com/40284755): Remove this once the callers are gone.
void AddByteTransferEvent(NetLogEventType event_type,
int byte_count,
const char* bytes) const;
bool IsCapturing() const { return non_null_net_log_->IsCapturing(); }
// Helper to create a NetLogWithSource given a NetLog and a NetLogSourceType.
// Takes care of creating a unique source ID, and handles the case of NULL
// net_log.
static NetLogWithSource Make(NetLog* net_log, NetLogSourceType source_type);
// Helper to create a NetLogWithSource given a NetLogSourceType.
// Equivalent to calling Make(NetLog*, NetLogSourceType) with NetLog::Get()
static NetLogWithSource Make(NetLogSourceType source_type);
// Creates a NetLogWithSource with an already initialized NetLogSource. If
// |net_log| is null or |source| is not valid, creates an unbound
// NetLogWithSource.
static NetLogWithSource Make(NetLog* net_log, const NetLogSource& source);
// Creates a NetLogWithSource with an already initialized NetLogSource.
// Equivalent to calling Make(NetLog*, NetLogSource&) with NetLog::Get().
// If |source| is not valid, creates an unbound NetLogWithSource.
static NetLogWithSource Make(const NetLogSource& source);
const NetLogSource& source() const { return source_; }
// Returns the bound NetLog*, or nullptr.
NetLog* net_log() const;
private:
NetLogWithSource(const NetLogSource& source, NetLog* non_null_net_log)
: source_(source), non_null_net_log_(non_null_net_log) {}
NetLogSource source_;
// There are two types of NetLogWithSource:
//
// (a) An ordinary NetLogWithSource for which |source().IsValid()| and
// |net_log() != nullptr|
//
// (b) A default constructed NetLogWithSource for which
// |!source().IsValid()| and |net_log() == nullptr|.
//
// As an optimization, both types internally store a non-null NetLog*. This
// way no null checks are needed before dispatching to the (possibly dummy)
// NetLog
raw_ptr<NetLog> non_null_net_log_;
};
} // namespace net
#endif // NET_LOG_NET_LOG_WITH_SOURCE_H_
|