forked from watsonwanda/oracle-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_connection_logger.go
67 lines (56 loc) · 2.27 KB
/
options_connection_logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2021-2023 Chronicle Labs, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package internal
import (
"github.com/libp2p/go-libp2p/core/network"
"github.com/multiformats/go-multiaddr"
"github.com/orcfax/oracle-suite/pkg/log"
)
// ConnectionLogger logs connected and disconnected hosts,
func ConnectionLogger() Options {
return func(n *Node) error {
n.AddNotifee(&connectionLoggerNotifee{n: n})
return nil
}
}
type connectionLoggerNotifee struct {
n *Node
}
// Listen implements the network.Notifiee interface.
func (n *connectionLoggerNotifee) Listen(network.Network, multiaddr.Multiaddr) {}
// ListenClose implements the network.Notifiee interface.
func (n *connectionLoggerNotifee) ListenClose(network.Network, multiaddr.Multiaddr) {}
// Connected implements the network.Notifiee interface.
func (n *connectionLoggerNotifee) Connected(_ network.Network, conn network.Conn) {
n.n.tsLog.get().
WithFields(log.Fields{
"peerID": conn.RemotePeer().String(),
"addr": conn.RemoteMultiaddr().String(),
}).
Debug("Connected to a host")
}
// Disconnected implements the network.Notifiee interface.
func (n *connectionLoggerNotifee) Disconnected(_ network.Network, conn network.Conn) {
n.n.tsLog.get().
WithFields(log.Fields{
"peerID": conn.RemotePeer().String(),
"addr": conn.RemoteMultiaddr().String(),
}).
Debug("Disconnected from a host")
}
// OpenedStream implements the network.Notifiee interface.
func (n *connectionLoggerNotifee) OpenedStream(network.Network, network.Stream) {}
// ClosedStream implements the network.Notifiee interface.
func (n *connectionLoggerNotifee) ClosedStream(network.Network, network.Stream) {}