bittorrent: add String() and LogFields()

This commit is contained in:
Jimmy Zelinskie 2017-10-17 22:02:06 -04:00
parent ce43a09956
commit 66e12c6684

View file

@ -93,6 +93,24 @@ type AnnounceRequest struct {
Params Params
} }
// LogFields renders the current response as a set of log fields.
func (r AnnounceRequest) LogFields() log.Fields {
return log.Fields{
"event": r.Event,
"infoHash": r.InfoHash,
"compact": r.Compact,
"eventProvided": r.EventProvided,
"numWantProvided": r.NumWantProvided,
"ipProvided": r.IPProvided,
"numWant": r.NumWant,
"left": r.Left,
"downloaded": r.Downloaded,
"uploaded": r.Uploaded,
"peer": r.Peer,
"params": r.Params,
}
}
// AnnounceResponse represents the parameters used to create an announce // AnnounceResponse represents the parameters used to create an announce
// response. // response.
type AnnounceResponse struct { type AnnounceResponse struct {
@ -105,15 +123,15 @@ type AnnounceResponse struct {
IPv6Peers []Peer IPv6Peers []Peer
} }
// LogFields renders the current response as a set of Logrus fields. // LogFields renders the current response as a set of log fields.
func (ar AnnounceResponse) LogFields() log.Fields { func (r AnnounceResponse) LogFields() log.Fields {
return log.Fields{ return log.Fields{
"compact": ar.Compact, "compact": r.Compact,
"complete": ar.Complete, "complete": r.Complete,
"interval": ar.Interval, "interval": r.Interval,
"minInterval": ar.MinInterval, "minInterval": r.MinInterval,
"ipv4Peers": ar.IPv4Peers, "ipv4Peers": r.IPv4Peers,
"ipv6Peers": ar.IPv6Peers, "ipv6Peers": r.IPv6Peers,
} }
} }
@ -124,6 +142,15 @@ type ScrapeRequest struct {
Params Params Params Params
} }
// LogFields renders the current response as a set of log fields.
func (r ScrapeRequest) LogFields() log.Fields {
return log.Fields{
"addressFamily": r.AddressFamily,
"infoHashes": r.InfoHashes,
"params": r.Params,
}
}
// ScrapeResponse represents the parameters used to create a scrape response. // ScrapeResponse represents the parameters used to create a scrape response.
// //
// The Scrapes must be in the same order as the InfoHashes in the corresponding // The Scrapes must be in the same order as the InfoHashes in the corresponding
@ -150,6 +177,17 @@ type Scrape struct {
// AddressFamily is the address family of an IP address. // AddressFamily is the address family of an IP address.
type AddressFamily uint8 type AddressFamily uint8
func (af AddressFamily) String() string {
switch af {
case IPv4:
return "IPv4"
case IPv6:
return "IPv6"
default:
panic("tried to print unknown AddressFamily")
}
}
// AddressFamily constants. // AddressFamily constants.
const ( const (
IPv4 AddressFamily = iota IPv4 AddressFamily = iota
@ -162,6 +200,10 @@ type IP struct {
AddressFamily AddressFamily
} }
func (ip IP) String() string {
return ip.IP.String()
}
// Peer represents the connection details of a peer that is returned in an // Peer represents the connection details of a peer that is returned in an
// announce response. // announce response.
type Peer struct { type Peer struct {