Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use own Logger interface #45

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"bytes"
"encoding/binary"
"fmt"
"log"
"time"

"github.com/florianl/go-nflog/v2/internal/unix"

"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
Expand Down Expand Up @@ -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())
}
}

Expand All @@ -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])
Expand Down
24 changes: 12 additions & 12 deletions nflog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/binary"
"fmt"
"log"
"sync"
"time"
"unsafe"
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
})
}
Expand Down Expand Up @@ -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
}
}()
Expand All @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nflog

import (
"errors"
"log"
"time"
)

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
Loading