Prepare log functions for filters. Init all filter functions by assigning empty stubs. #26

This commit is contained in:
Kleissner
2021-06-28 02:37:31 +02:00
parent f5ce15d33a
commit 084f3bc1ee
5 changed files with 23 additions and 10 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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...)
}

View File

@@ -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
}

View File

@@ -8,6 +8,7 @@ package core
// Init initializes the client. The config must be loaded first!
func Init() {
initFilters()
initPeerID()
initKademlia()
initMessageSequence()