From 084f3bc1ee6c53687e2714499efbf9544cdfda5b Mon Sep 17 00:00:00 2001 From: Kleissner Date: Mon, 28 Jun 2021 02:37:31 +0200 Subject: [PATCH] Prepare log functions for filters. Init all filter functions by assigning empty stubs. #26 --- Config.go | 2 +- Connection.go | 4 +--- Filter.go | 18 ++++++++++++++++++ Peer ID.go | 8 ++------ Peernet.go | 1 + 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Config.go b/Config.go index 74bc000..774d4fc 100644 --- a/Config.go +++ b/Config.go @@ -16,7 +16,7 @@ import ( ) // Version is the current core library version -const Version = "Alpha 2/09.06.2021" +const Version = "Alpha 2/28.06.2021" var config struct { LogFile string `yaml:"LogFile"` // Log file diff --git a/Connection.go b/Connection.go index a4db005..d655bd2 100644 --- a/Connection.go +++ b/Connection.go @@ -175,9 +175,7 @@ func (peer *PeerInfo) registerConnection(incoming *Connection) (result *Connecti peer.connectionActive = append(peer.connectionActive, incoming) peer.setConnectionLatest(incoming) - if Filters.NewPeerConnection != nil { - Filters.NewPeerConnection(peer, incoming) - } + Filters.NewPeerConnection(peer, incoming) return incoming } diff --git a/Filter.go b/Filter.go index e5ed4d0..bfee8e7 100644 --- a/Filter.go +++ b/Filter.go @@ -8,6 +8,8 @@ Filters allow the caller to intercept events to log, modify, or prevent. package core +import "log" + // Filters contains all functions to install the hook. Use nil for unused. // The functions are called sequentially and block execution; if the filter takes a long time it should start a Go routine. var Filters struct { @@ -18,4 +20,20 @@ var Filters struct { // NewPeerConnection is called for each new established connection to a peer. Note that connections might be dropped and reconnected at anytime. NewPeerConnection func(peer *PeerInfo, connection *Connection) + + // LogError is called for any error. If this function is overwritten by the caller, the caller must write errors into the log file if desired, or call DefaultLogError. + LogError func(function, format string, v ...interface{}) +} + +func initFilters() { + // Set default filters to blank functions so they can be safely called without constant nil checks. + Filters.NewPeer = func(peer *PeerInfo, connection *Connection) {} + Filters.NewPeerConnection = func(peer *PeerInfo, connection *Connection) {} + + Filters.LogError = DefaultLogError +} + +// DefaultLogError is the default error logging function +func DefaultLogError(function, format string, v ...interface{}) { + log.Printf("["+function+"] "+format, v...) } diff --git a/Peer ID.go b/Peer ID.go index ac831af..47e3bf6 100644 --- a/Peer ID.go +++ b/Peer ID.go @@ -145,12 +145,8 @@ func PeerlistAdd(PublicKey *btcec.PublicKey, connections ...*Connection) (peer * } } - if Filters.NewPeer != nil { - Filters.NewPeer(peer, connections[0]) - } - if Filters.NewPeerConnection != nil { - Filters.NewPeerConnection(peer, connections[0]) - } + Filters.NewPeer(peer, connections[0]) + Filters.NewPeerConnection(peer, connections[0]) return peer, true } diff --git a/Peernet.go b/Peernet.go index aef0a2f..2f9a403 100644 --- a/Peernet.go +++ b/Peernet.go @@ -8,6 +8,7 @@ package core // Init initializes the client. The config must be loaded first! func Init() { + initFilters() initPeerID() initKademlia() initMessageSequence()