|
|
|
@ -6,52 +6,62 @@ import ( |
|
|
|
|
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) { |
|
|
|
|
log.Debug("[CONNECTIONS] Listener starting", "net", net, "addr", ma) |
|
|
|
|
func (cl ConnLogger) Listen(net net.Network, ma ma.Multiaddr) { |
|
|
|
|
cl.l.Debug("[CONNECTIONS] Listener starting", "net", net, "addr", ma) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (connLogger) ListenClose(net net.Network, ma ma.Multiaddr) { |
|
|
|
|
log.Debug("[CONNECTIONS] Listener closing", "net", net, "addr", ma) |
|
|
|
|
func (cl ConnLogger) ListenClose(net net.Network, ma ma.Multiaddr) { |
|
|
|
|
cl.l.Debug("[CONNECTIONS] Listener closing", "net", net, "addr", ma) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (connLogger) Connected(net net.Network, conn net.Conn) { |
|
|
|
|
log.Debug("[CONNECTIONS] Connected", "net", net, |
|
|
|
|
func (cl ConnLogger) Connected(net net.Network, conn net.Conn) { |
|
|
|
|
cl.l.Debug("[CONNECTIONS] Connected", "net", net, |
|
|
|
|
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
|
|
|
|
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (connLogger) Disconnected(net net.Network, conn net.Conn) { |
|
|
|
|
log.Debug("[CONNECTIONS] Disconnected", "net", net, |
|
|
|
|
func (cl ConnLogger) Disconnected(net net.Network, conn net.Conn) { |
|
|
|
|
cl.l.Debug("[CONNECTIONS] Disconnected", "net", net, |
|
|
|
|
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
|
|
|
|
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (connLogger) OpenedStream(net net.Network, stream net.Stream) { |
|
|
|
|
func (cl ConnLogger) OpenedStream(net net.Network, stream net.Stream) { |
|
|
|
|
conn := stream.Conn() |
|
|
|
|
log.Debug("[CONNECTIONS] Stream opened", "net", net, |
|
|
|
|
cl.l.Debug("[CONNECTIONS] Stream opened", "net", net, |
|
|
|
|
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
|
|
|
|
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
|
|
|
|
"protocol", stream.Protocol(), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (connLogger) ClosedStream(net net.Network, stream net.Stream) { |
|
|
|
|
func (cl ConnLogger) ClosedStream(net net.Network, stream net.Stream) { |
|
|
|
|
conn := stream.Conn() |
|
|
|
|
log.Debug("[CONNECTIONS] Stream closed", "net", net, |
|
|
|
|
cl.l.Debug("[CONNECTIONS] Stream closed", "net", net, |
|
|
|
|
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
|
|
|
|
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
|
|
|
|
"protocol", stream.Protocol(), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ConnLogger is a LibP2P connection logger.
|
|
|
|
|
// Add on a LibP2P host by calling:
|
|
|
|
|
//
|
|
|
|
|
// host.Network().Notify(utils.ConnLogger)
|
|
|
|
|
//
|
|
|
|
|
// It logs all listener/connection/stream open/close activities at debug level.
|
|
|
|
|
var ConnLogger connLogger |
|
|
|
|
// 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()) |
|
|
|
|