From 02abf88079bfe86abebe80c6e31a679905115db0 Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Tue, 12 Feb 2019 11:10:26 -0800 Subject: [PATCH] Move connection logger to internal/utils Suggested-by: Leo Chen --- cmd/harmony.go | 47 +---------------------------- internal/utils/connlogger.go | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 46 deletions(-) create mode 100644 internal/utils/connlogger.go diff --git a/cmd/harmony.go b/cmd/harmony.go index ddfc7e868..b5c0c9753 100644 --- a/cmd/harmony.go +++ b/cmd/harmony.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" - net "github.com/libp2p/go-libp2p-net" peerstore "github.com/libp2p/go-libp2p-peerstore" multiaddr "github.com/multiformats/go-multiaddr" @@ -79,50 +78,6 @@ func loggingInit(logFolder, role, ip, port string, onlyLogTps bool) { log.Root().SetHandler(h) } -type connLogger struct{} - -func (connLogger) Listen(net net.Network, ma multiaddr.Multiaddr) { - log.Debug("[CONNECTIONS] Listener starting", "net", net, "addr", ma) -} - -func (connLogger) ListenClose(net net.Network, ma multiaddr.Multiaddr) { - log.Debug("[CONNECTIONS] Listener closing", "net", net, "addr", ma) -} - -func (connLogger) Connected(net net.Network, conn net.Conn) { - log.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, - "localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), - "remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), - ) -} - -func (connLogger) OpenedStream(net net.Network, stream net.Stream) { - conn := stream.Conn() - log.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) { - conn := stream.Conn() - log.Debug("[CONNECTIONS] Stream closed", "net", net, - "localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), - "remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), - "protocol", stream.Protocol(), - ) -} - -var theConnLogger connLogger - func main() { // TODO: use http://getmyipaddress.org/ or http://www.get-myip.com/ to retrieve my IP address ip := flag.String("ip", "127.0.0.1", "IP of the node") @@ -261,7 +216,7 @@ func main() { host, err := p2pimpl.NewHost(&selfPeer, nodePriKey) if *logConn { - host.GetP2PHost().Network().Notify(theConnLogger) + host.GetP2PHost().Network().Notify(utils.ConnLogger) } if err != nil { panic("unable to new host in harmony") diff --git a/internal/utils/connlogger.go b/internal/utils/connlogger.go new file mode 100644 index 000000000..d283867a3 --- /dev/null +++ b/internal/utils/connlogger.go @@ -0,0 +1,57 @@ +package utils + +import ( + "github.com/ethereum/go-ethereum/log" + net "github.com/libp2p/go-libp2p-net" + ma "github.com/multiformats/go-multiaddr" +) + +type connLogger struct{} + +func (connLogger) Listen(net net.Network, ma ma.Multiaddr) { + log.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 (connLogger) Connected(net net.Network, conn net.Conn) { + log.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, + "localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), + "remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), + ) +} + +func (connLogger) OpenedStream(net net.Network, stream net.Stream) { + conn := stream.Conn() + log.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) { + conn := stream.Conn() + log.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