Skip to content

Commit aae957a

Browse files
authored
Merge pull request #6887 from ipfs/peerlog-plugin
feat: add peerlog plugin
2 parents 3433076 + 15085fd commit aae957a

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

docs/plugins.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ To preload a go-ipfs plugin:
136136
go-ipfs$ make build
137137
```
138138

139+
You can also preload an in-tree but disabled-by-default plugin by adding it to
140+
the IPFS_PLUGINS variable. For example, to enable plugins foo, bar, and baz:
141+
142+
```bash
143+
go-ipfs$ make build IPFS_PLUGINS="foo bar baz"
144+
```
145+
139146
## Creating A Plugin
140147

141148
To create your own out-of-tree plugin, use the [example

plugin/loader/Rules.mk

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
include mk/header.mk
22

3+
IPFS_PLUGINS ?=
4+
export IPFS_PLUGINS
5+
36
$(d)/preload.go: d:=$(d)
4-
$(d)/preload.go: $(d)/preload_list $(d)/preload.sh
7+
$(d)/preload.go: $(d)/preload_list $(d)/preload.sh ALWAYS
58
$(d)/preload.sh > $@
69
go fmt $@ >/dev/null
710

811
DEPS_GO += $(d)/preload.go
9-
12+
1013
include mk/footer.mk

plugin/loader/preload.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
44

55
to_preload() {
66
awk 'NF' "$DIR/preload_list" | sed '/^#/d'
7+
if [[ -n "$IPFS_PLUGINS" ]]; then
8+
for plugin in $IPFS_PLUGINS; do
9+
echo "$plugin github.com/ipfs/go-ipfs/plugin/plugins/$plugin *"
10+
done
11+
fi
712
}
813

914
cat <<EOL

plugin/plugins/peerlog/peerlog.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package peerlog
2+
3+
import (
4+
"fmt"
5+
6+
core "github.com/ipfs/go-ipfs/core"
7+
plugin "github.com/ipfs/go-ipfs/plugin"
8+
logging "github.com/ipfs/go-log"
9+
network "github.com/libp2p/go-libp2p-core/network"
10+
)
11+
12+
var log = logging.Logger("plugin/peerlog")
13+
14+
// Log all the PeerIDs we see
15+
//
16+
// Usage:
17+
// GOLOG_FILE=~/peer.log IPFS_LOGGING_FMT=json ipfs daemon
18+
// Output:
19+
// {"level":"info","ts":"2020-02-10T13:54:26.639Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:51","msg":"connected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"}
20+
// {"level":"info","ts":"2020-02-10T13:54:59.095Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:56","msg":"disconnected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"}
21+
//
22+
type peerLogPlugin struct{}
23+
24+
var _ plugin.PluginDaemonInternal = (*peerLogPlugin)(nil)
25+
26+
// Plugins is exported list of plugins that will be loaded
27+
var Plugins = []plugin.Plugin{
28+
&peerLogPlugin{},
29+
}
30+
31+
// Name returns the plugin's name, satisfying the plugin.Plugin interface.
32+
func (*peerLogPlugin) Name() string {
33+
return "peerlog"
34+
}
35+
36+
// Version returns the plugin's version, satisfying the plugin.Plugin interface.
37+
func (*peerLogPlugin) Version() string {
38+
return "0.1.0"
39+
}
40+
41+
// Init initializes plugin
42+
func (*peerLogPlugin) Init(*plugin.Environment) error {
43+
fmt.Println("peerLogPlugin enabled - PeerIDs will be logged")
44+
return nil
45+
}
46+
47+
func (*peerLogPlugin) Start(node *core.IpfsNode) error {
48+
// Ensure logs from this plugin get printed regardless of global IPFS_LOGGING value
49+
if err := logging.SetLogLevel("plugin/peerlog", "info"); err != nil {
50+
return fmt.Errorf("failed to set log level: %w", err)
51+
}
52+
var notifee network.NotifyBundle
53+
notifee.ConnectedF = func(net network.Network, conn network.Conn) {
54+
log.Infow("connected",
55+
"peer", conn.RemotePeer().Pretty(),
56+
)
57+
}
58+
notifee.DisconnectedF = func(net network.Network, conn network.Conn) {
59+
log.Infow("disconnected",
60+
"peer", conn.RemotePeer().Pretty(),
61+
)
62+
}
63+
node.PeerHost.Network().Notify(&notifee)
64+
return nil
65+
}
66+
67+
func (*peerLogPlugin) Close() error {
68+
return nil
69+
}

0 commit comments

Comments
 (0)