You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.3 KiB
71 lines
2.3 KiB
package utils
|
|
|
|
import (
|
|
"github.com/ethereum/go-ethereum/log"
|
|
net "github.com/libp2p/go-libp2p-net"
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
)
|
|
|
|
// 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 netLogger(n net.Network, l log.Logger) log.Logger {
|
|
return l.New("net", n)
|
|
}
|
|
|
|
func (cl ConnLogger) Listen(n net.Network, ma ma.Multiaddr) {
|
|
WithCaller(netLogger(n, cl.l)).Debug("listener starting", "addr", ma)
|
|
}
|
|
|
|
func (cl ConnLogger) ListenClose(n net.Network, ma ma.Multiaddr) {
|
|
WithCaller(netLogger(n, cl.l)).Debug("listener closing", "addr", ma)
|
|
}
|
|
|
|
func (cl ConnLogger) Connected(n net.Network, c net.Conn) {
|
|
WithCaller(netLogger(n, cl.l)).Debug("connected",
|
|
"localPeer", c.LocalPeer(), "localAddr", c.LocalMultiaddr(),
|
|
"remotePeer", c.RemotePeer(), "remoteAddr", c.RemoteMultiaddr(),
|
|
)
|
|
}
|
|
|
|
func (cl ConnLogger) Disconnected(n net.Network, c net.Conn) {
|
|
WithCaller(netLogger(n, cl.l)).Debug("disconnected",
|
|
"localPeer", c.LocalPeer(), "localAddr", c.LocalMultiaddr(),
|
|
"remotePeer", c.RemotePeer(), "remoteAddr", c.RemoteMultiaddr(),
|
|
)
|
|
}
|
|
|
|
func (cl ConnLogger) OpenedStream(n net.Network, s net.Stream) {
|
|
conn := s.Conn()
|
|
WithCaller(netLogger(n, cl.l)).Debug("stream opened",
|
|
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(),
|
|
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(),
|
|
"protocol", s.Protocol(),
|
|
)
|
|
}
|
|
|
|
func (cl ConnLogger) ClosedStream(n net.Network, s net.Stream) {
|
|
conn := s.Conn()
|
|
WithCaller(netLogger(n, cl.l)).Debug("stream closed",
|
|
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(),
|
|
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(),
|
|
"protocol", s.Protocol(),
|
|
)
|
|
}
|
|
|
|
// 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())
|
|
|