blob: 5b2742a897a910dcb83d4b7525c6db7f69031ec8 [file] [log] [blame]
// Copyright 2016 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.
#include "net/log/net_log_with_source.h"
#include <memory>
#include <utility>
#include "base/logging.h"
#include "base/values.h"
#include "net/base/net_errors.h"
#include "net/log/net_log.h"
#include "net/log/net_log_capture_mode.h"
#include "net/log/net_log_values.h"
namespace net {
namespace {
// Returns parameters for logging data transferred events. At a minimum includes
// the number of bytes transferred. If the capture mode allows logging byte
// contents and |byte_count| > 0, then will include the actual bytes.
base::Value BytesTransferredParams(int byte_count,
const char* bytes,
NetLogCaptureMode capture_mode) {
base::DictionaryValue dict;
dict.SetInteger("byte_count", byte_count);
if (NetLogCaptureIncludesSocketBytes(capture_mode) && byte_count > 0)
dict.SetKey("bytes", NetLogBinaryValue(bytes, byte_count));
return std::move(dict);
}
} // namespace
NetLogWithSource::~NetLogWithSource() {}
void NetLogWithSource::AddEntry(NetLogEventType type,
NetLogEventPhase phase) const {
if (!net_log_)
return;
net_log_->AddEntry(type, source_, phase);
}
void NetLogWithSource::AddEvent(NetLogEventType type) const {
AddEntry(type, NetLogEventPhase::NONE);
}
void NetLogWithSource::AddEventWithStringParams(NetLogEventType type,
base::StringPiece name,
base::StringPiece value) const {
AddEvent(type, [&] { return NetLogParamsWithString(name, value); });
}
void NetLogWithSource::AddEventWithIntParams(NetLogEventType type,
base::StringPiece name,
int value) const {
AddEvent(type, [&] { return NetLogParamsWithInt(name, value); });
}
void NetLogWithSource::BeginEventWithIntParams(NetLogEventType type,
base::StringPiece name,
int value) const {
BeginEvent(type, [&] { return NetLogParamsWithInt(name, value); });
}
void NetLogWithSource::EndEventWithIntParams(NetLogEventType type,
base::StringPiece name,
int value) const {
EndEvent(type, [&] { return NetLogParamsWithInt(name, value); });
}
void NetLogWithSource::AddEventWithInt64Params(NetLogEventType type,
base::StringPiece name,
int64_t value) const {
AddEvent(type, [&] { return NetLogParamsWithInt64(name, value); });
}
void NetLogWithSource::BeginEventWithStringParams(
NetLogEventType type,
base::StringPiece name,
base::StringPiece value) const {
BeginEvent(type, [&] { return NetLogParamsWithString(name, value); });
}
void NetLogWithSource::AddEventReferencingSource(
NetLogEventType type,
const NetLogSource& source) const {
AddEvent(type, [&] { return source.ToEventParameters(); });
}
void NetLogWithSource::BeginEventReferencingSource(
NetLogEventType type,
const NetLogSource& source) const {
BeginEvent(type, [&] { return source.ToEventParameters(); });
}
void NetLogWithSource::BeginEvent(NetLogEventType type) const {
AddEntry(type, NetLogEventPhase::BEGIN);
}
void NetLogWithSource::EndEvent(NetLogEventType type) const {
AddEntry(type, NetLogEventPhase::END);
}
void NetLogWithSource::AddEventWithNetErrorCode(NetLogEventType event_type,
int net_error) const {
DCHECK_NE(ERR_IO_PENDING, net_error);
if (net_error >= 0) {
AddEvent(event_type);
} else {
AddEventWithIntParams(event_type, "net_error", net_error);
}
}
void NetLogWithSource::EndEventWithNetErrorCode(NetLogEventType event_type,
int net_error) const {
DCHECK_NE(ERR_IO_PENDING, net_error);
if (net_error >= 0) {
EndEvent(event_type);
} else {
EndEventWithIntParams(event_type, "net_error", net_error);
}
}
void NetLogWithSource::AddEntryWithBoolParams(NetLogEventType type,
NetLogEventPhase phase,
base::StringPiece name,
bool value) const {
AddEntry(type, phase, [&] { return NetLogParamsWithBool(name, value); });
}
void NetLogWithSource::AddByteTransferEvent(NetLogEventType event_type,
int byte_count,
const char* bytes) const {
AddEvent(event_type, [&](NetLogCaptureMode capture_mode) {
return BytesTransferredParams(byte_count, bytes, capture_mode);
});
}
bool NetLogWithSource::IsCapturing() const {
return net_log_ && net_log_->IsCapturing();
}
// static
NetLogWithSource NetLogWithSource::Make(NetLog* net_log,
NetLogSourceType source_type) {
if (!net_log)
return NetLogWithSource();
NetLogSource source(source_type, net_log->NextID());
return NetLogWithSource(source, net_log);
}
} // namespace net