From 6427f2bf37b0eac5caeac3b95f6409cd2d39842a Mon Sep 17 00:00:00 2001 From: Florian Lehner Date: Mon, 20 May 2024 09:24:54 +0200 Subject: [PATCH] use own Logger interface Signed-off-by: Florian Lehner --- attribute.go | 7 +++---- nflog.go | 24 ++++++++++++------------ types.go | 9 +++++++-- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/attribute.go b/attribute.go index f666d3c..1e11cf9 100644 --- a/attribute.go +++ b/attribute.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/binary" "fmt" - "log" "time" "github.com/florianl/go-nflog/v2/internal/unix" @@ -12,7 +11,7 @@ import ( "github.com/mdlayher/netlink" ) -func extractAttribute(a *Attribute, logger *log.Logger, data []byte) error { +func extractAttribute(a *Attribute, logger Logger, data []byte) error { ad, err := netlink.NewAttributeDecoder(data) if err != nil { return err @@ -129,7 +128,7 @@ func extractAttribute(a *Attribute, logger *log.Logger, data []byte) error { a.VLAN = info ad.ByteOrder = nativeEndian default: - logger.Printf("Unknown attribute: %d %v\n", ad.Type(), ad.Bytes()) + logger.Debugf("Unknown attribute: %d %v\n", ad.Type(), ad.Bytes()) } } @@ -143,7 +142,7 @@ func checkHeader(data []byte) int { return 0 } -func extractAttributes(logger *log.Logger, msg []byte) (Attribute, error) { +func extractAttributes(logger Logger, msg []byte) (Attribute, error) { attrs := Attribute{} offset := checkHeader(msg[:2]) diff --git a/nflog.go b/nflog.go index 3175ba3..849d9e8 100644 --- a/nflog.go +++ b/nflog.go @@ -4,7 +4,6 @@ import ( "context" "encoding/binary" "fmt" - "log" "sync" "time" "unsafe" @@ -19,7 +18,7 @@ type Nflog struct { // Con is the pure representation of a netlink socket Con *netlink.Conn - logger *log.Logger + logger Logger wg sync.WaitGroup @@ -32,12 +31,13 @@ type Nflog struct { settings uint16 } -// devNull satisfies io.Writer, in case *log.Logger is not provided +var _ Logger = (*devNull)(nil) + +// devNull satisfies the Logger interface. type devNull struct{} -func (devNull) Write(p []byte) (int, error) { - return 0, nil -} +func (dn *devNull) Debugf(format string, args ...interface{}) {} +func (dn *devNull) Errorf(format string, args ...interface{}) {} // for detailes see https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/tensor.go#L488-L505 var nativeEndian binary.ByteOrder @@ -79,7 +79,7 @@ func Open(config *Config) (*Nflog, error) { nflog.Con = con if config.Logger == nil { - nflog.logger = log.New(new(devNull), "", 0) + nflog.logger = new(devNull) } else { nflog.logger = config.Logger } @@ -133,7 +133,7 @@ func (nflog *Nflog) Register(ctx context.Context, fn HookFunc) error { return 0 } } - nflog.logger.Printf("Could not receive message: %v\n", err) + nflog.logger.Errorf("Could not receive message: %v\n", err) return 1 }) } @@ -218,7 +218,7 @@ func (nflog *Nflog) RegisterWithErrorFunc(ctx context.Context, fn HookFunc, errf {Type: nfUlACfgCmd, Data: []byte{nfUlnlCfgCmdUnbind}}, }) if err != nil { - nflog.logger.Printf("Could not unbind socket from configuration: %v", err) + nflog.logger.Errorf("Could not unbind socket from configuration: %v", err) return } }() @@ -234,7 +234,7 @@ func (nflog *Nflog) RegisterWithErrorFunc(ctx context.Context, fn HookFunc, errf }() for { if err := ctx.Err(); err != nil { - nflog.logger.Printf("Stop receiving nflog messages: %v", err) + nflog.logger.Errorf("Error and stopping receiving nflog messages: %v", err) return } reply, err := nflog.Con.Receive() @@ -253,7 +253,7 @@ func (nflog *Nflog) RegisterWithErrorFunc(ctx context.Context, fn HookFunc, errf } attrs, err := parseMsg(nflog.logger, msg) if err != nil { - nflog.logger.Printf("Could not parse message: %v", err) + nflog.logger.Debugf("Could not parse message: %v", err) continue } if ret := fn(attrs); ret != 0 { @@ -317,7 +317,7 @@ func (nflog *Nflog) execute(req netlink.Message) (uint32, error) { return seq, nil } -func parseMsg(logger *log.Logger, msg netlink.Message) (Attribute, error) { +func parseMsg(logger Logger, msg netlink.Message) (Attribute, error) { a, err := extractAttributes(logger, msg.Data) if err != nil { return Attribute{}, err diff --git a/types.go b/types.go index 368a5fc..e4cc4eb 100644 --- a/types.go +++ b/types.go @@ -2,7 +2,6 @@ package nflog import ( "errors" - "log" "time" ) @@ -120,6 +119,12 @@ type VLAN struct { TCI uint16 } +// Logger provides logging functionality. +type Logger interface { + Debugf(format string, args ...interface{}) + Errorf(format string, args ...interface{}) +} + // Config contains options for a Conn. type Config struct { // Network namespace the Nflog needs to operate in. If set to 0 (default), @@ -159,7 +164,7 @@ type Config struct { ReadTimeout time.Duration // Interface to log internals. - Logger *log.Logger + Logger Logger } // ErrorFunc is a function that receives all errors that happen while reading