|
|
@ -6,52 +6,72 @@ import ( |
|
|
|
ma "github.com/multiformats/go-multiaddr" |
|
|
|
ma "github.com/multiformats/go-multiaddr" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type connLogger struct{} |
|
|
|
// ConnLogger is a LibP2P connection logger that logs to an Ethereum logger.
|
|
|
|
|
|
|
|
// It logs all listener/connection/stream open/close activities at debug level.
|
|
|
|
|
|
|
|
// To use one, add it on a LibP2P host swarm as a notifier, ex:
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// connLogger := utils.NewConnLogger(
|
|
|
|
|
|
|
|
// host.Network().Notify(connLogger)
|
|
|
|
|
|
|
|
type ConnLogger struct { |
|
|
|
|
|
|
|
l log.Logger |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (connLogger) Listen(net net.Network, ma ma.Multiaddr) { |
|
|
|
func netLogger(n net.Network, l log.Logger) log.Logger { |
|
|
|
log.Debug("[CONNECTIONS] Listener starting", "net", net, "addr", ma) |
|
|
|
return l.New("net", |
|
|
|
|
|
|
|
"netLocalPeer", n.LocalPeer(), |
|
|
|
|
|
|
|
"netListenAddresses", n.ListenAddresses()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (connLogger) ListenClose(net net.Network, ma ma.Multiaddr) { |
|
|
|
func connLogger(c net.Conn, l log.Logger) log.Logger { |
|
|
|
log.Debug("[CONNECTIONS] Listener closing", "net", net, "addr", ma) |
|
|
|
return l.New( |
|
|
|
|
|
|
|
"connLocalPeer", c.LocalPeer(), |
|
|
|
|
|
|
|
"connLocalAddr", c.LocalMultiaddr(), |
|
|
|
|
|
|
|
"connRemotePeer", c.RemotePeer(), |
|
|
|
|
|
|
|
"connRemoteAddr", c.RemoteMultiaddr()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (connLogger) Connected(net net.Network, conn net.Conn) { |
|
|
|
func streamLogger(s net.Stream, l log.Logger) log.Logger { |
|
|
|
log.Debug("[CONNECTIONS] Connected", "net", net, |
|
|
|
return connLogger(s.Conn(), l).New("streamProtocolID", s.Protocol()) |
|
|
|
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
|
|
|
|
|
|
|
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (connLogger) Disconnected(net net.Network, conn net.Conn) { |
|
|
|
// Listen logs a listener starting listening on an address.
|
|
|
|
log.Debug("[CONNECTIONS] Disconnected", "net", net, |
|
|
|
func (cl ConnLogger) Listen(n net.Network, ma ma.Multiaddr) { |
|
|
|
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
|
|
|
WithCaller(netLogger(n, cl.l)). |
|
|
|
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
|
|
|
Debug("listener starting", "listenAddress", ma) |
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (connLogger) OpenedStream(net net.Network, stream net.Stream) { |
|
|
|
// ListenClose logs a listener stopping listening on an address.
|
|
|
|
conn := stream.Conn() |
|
|
|
func (cl ConnLogger) ListenClose(n net.Network, ma ma.Multiaddr) { |
|
|
|
log.Debug("[CONNECTIONS] Stream opened", "net", net, |
|
|
|
WithCaller(netLogger(n, cl.l)). |
|
|
|
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
|
|
|
Debug("listener closing", "listenAddress", ma) |
|
|
|
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
|
|
|
|
|
|
|
"protocol", stream.Protocol(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (connLogger) ClosedStream(net net.Network, stream net.Stream) { |
|
|
|
// Connected logs a connection getting opened.
|
|
|
|
conn := stream.Conn() |
|
|
|
func (cl ConnLogger) Connected(n net.Network, c net.Conn) { |
|
|
|
log.Debug("[CONNECTIONS] Stream closed", "net", net, |
|
|
|
WithCaller(connLogger(c, netLogger(n, cl.l))).Debug("connected") |
|
|
|
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
|
|
|
|
|
|
|
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
|
|
|
|
|
|
|
"protocol", stream.Protocol(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ConnLogger is a LibP2P connection logger.
|
|
|
|
// Disconnected logs a connection getting closed.
|
|
|
|
// Add on a LibP2P host by calling:
|
|
|
|
func (cl ConnLogger) Disconnected(n net.Network, c net.Conn) { |
|
|
|
//
|
|
|
|
WithCaller(connLogger(c, netLogger(n, cl.l))).Debug("disconnected") |
|
|
|
// host.Network().Notify(utils.ConnLogger)
|
|
|
|
} |
|
|
|
//
|
|
|
|
|
|
|
|
// It logs all listener/connection/stream open/close activities at debug level.
|
|
|
|
// OpenedStream logs a new stream getting opened.
|
|
|
|
var ConnLogger connLogger |
|
|
|
func (cl ConnLogger) OpenedStream(n net.Network, s net.Stream) { |
|
|
|
|
|
|
|
WithCaller(streamLogger(s, netLogger(n, cl.l))).Debug("stream opened") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ClosedStream logs a stream getting closed.
|
|
|
|
|
|
|
|
func (cl ConnLogger) ClosedStream(n net.Network, s net.Stream) { |
|
|
|
|
|
|
|
WithCaller(streamLogger(s, netLogger(n, cl.l))).Debug("stream closed") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// NewConnLogger returns a new connection logger that uses the given
|
|
|
|
|
|
|
|
// Ethereum logger. See ConnLogger for usage.
|
|
|
|
|
|
|
|
func NewConnLogger(l log.Logger) *ConnLogger { |
|
|
|
|
|
|
|
return &ConnLogger{l: l} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// RootConnLogger is a LibP2P connection logger that logs to Ethereum root
|
|
|
|
|
|
|
|
// logger. See ConnLogger for usage.
|
|
|
|
|
|
|
|
var RootConnLogger = NewConnLogger(log.Root()) |
|
|
|