Skip to content

Commit

Permalink
make logger local to library, fixes #58
Browse files Browse the repository at this point in the history
  • Loading branch information
spali committed Apr 25, 2024
1 parent 3108b23 commit 627889b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
6 changes: 3 additions & 3 deletions cmd/e3dc/e3dc.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ func main() {
os.Exit(1)
}
if conf.debug > 0 {
logrus.SetLevel(logrus.Level(conf.debug))
logrus.SetOutput(os.Stderr)
rscp.Log.SetLevel(logrus.Level(conf.debug))
rscp.Log.SetOutput(os.Stderr)
} else {
logrus.SetLevel(logrus.PanicLevel)
rscp.Log.SetLevel(logrus.PanicLevel)
}
var (
rb []byte
Expand Down
4 changes: 2 additions & 2 deletions cmd/example/e3dc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func run() int {
fmt.Fprintf(os.Stderr, "Connection error: %s\n", err)
return 1
}
logrus.SetLevel(logrus.WarnLevel)
logrus.SetOutput(os.Stderr)
rscp.Log.SetLevel(logrus.WarnLevel)
rscp.Log.SetOutput(os.Stderr)
defer func() { _ = c.Disconnect() }() // make sure to disconnect at the end
{
//
Expand Down
26 changes: 14 additions & 12 deletions rscp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"errors"

"github.com/azihsoyn/rijndael256"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

var Log = logrus.New()

// Client for rscp protocol
type Client struct {
config ClientConfig
Expand Down Expand Up @@ -102,7 +104,7 @@ func (c *Client) receive() ([]Message, error) {

// connect create connection
func (c *Client) connect() error {
log.Infof("Connecting to %s", c.connectionString)
Log.Infof("Connecting to %s", c.connectionString)
var (
conn net.Conn
err error
Expand All @@ -113,39 +115,39 @@ func (c *Client) connect() error {
}
c.conn = conn
c.isConnected = true
log.Infof("successfully connected to %s", c.conn.RemoteAddr())
Log.Infof("successfully connected to %s", c.conn.RemoteAddr())
return nil
}

// authenticate authenticates the connection
func (c *Client) authenticate() error {
orgLogLevel := log.GetLevel()
orgLogLevel := Log.GetLevel()
if orgLogLevel < RequiredAuthLogLevel {
log.Infof("hiding auth request for security, use debug >= %d to debug authentication", RequiredAuthLogLevel)
log.SetLevel(log.Level((math.Min(float64(orgLogLevel), float64(log.InfoLevel)))))
Log.Infof("hiding auth request for security, use debug >= %d to debug authentication", RequiredAuthLogLevel)
Log.SetLevel(logrus.Level((math.Min(float64(orgLogLevel), float64(logrus.InfoLevel)))))
}
if msg, err := CreateRequest(RSCP_REQ_AUTHENTICATION,
RSCP_AUTHENTICATION_USER, c.config.Username, RSCP_AUTHENTICATION_PASSWORD, c.config.Password); err != nil {
if orgLogLevel < RequiredAuthLogLevel {
log.SetLevel(orgLogLevel)
Log.SetLevel(orgLogLevel)
}
return err
} else if err := c.send([]Message{*msg}); err != nil {
if orgLogLevel < RequiredAuthLogLevel {
log.SetLevel(orgLogLevel)
Log.SetLevel(orgLogLevel)
}
return err
}
if orgLogLevel < RequiredAuthLogLevel {
log.SetLevel(orgLogLevel)
Log.SetLevel(orgLogLevel)
}
var (
messages []Message
err error
)
if messages, err = c.receive(); err != nil {
if errors.Is(err, io.EOF) {
log.Warnf("Hint: EOF during authentification usually is due a wrong rscp key")
Log.Warnf("Hint: EOF during authentification usually is due a wrong rscp key")
}
return fmt.Errorf("authentication error: %w", err)
}
Expand All @@ -170,7 +172,7 @@ func (c *Client) authenticate() error {
}
}
c.isAuthenticated = true
log.Infof("successfully authenticated (level: %s)", AuthLevel(messages[0].Value.(uint8)))
Log.Infof("successfully authenticated (level: %s)", AuthLevel(messages[0].Value.(uint8)))
return nil
}

Expand All @@ -184,7 +186,7 @@ func (c *Client) Disconnect() error {
return err
}
}
log.Info("disconnected")
Log.Info("disconnected")
return nil
}

Expand Down
8 changes: 3 additions & 5 deletions rscp/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"math/big"
"reflect"
"time"

log "github.com/sirupsen/logrus"
)

// readHeader reads and checks the header
Expand Down Expand Up @@ -94,7 +92,7 @@ func readMessage(buf *bytes.Reader) (*Message, error) {
return nil, err
}
if !m.Tag.IsATag() {
log.Warnf("unknown tag 0x%08x received", big.NewInt(int64(m.Tag)))
Log.Warnf("unknown tag 0x%08x received", big.NewInt(int64(m.Tag)))
}
if err := read(buf, &m.DataType, RSCP_DATA_DATATYPE_SIZE); err != nil {
return nil, err
Expand All @@ -110,7 +108,7 @@ func readMessage(buf *bytes.Reader) (*Message, error) {
// test length against known length of data type's
if (m.DataType.length() != 0 || m.DataType == None) && m.DataType.length() != l {
// print data missed for debugging before returning the error
log.DebugFn(func() []interface{} {
Log.DebugFn(func() []interface{} {
b := make([]byte, l)
_ = read(buf, &b, l)
return []interface{}{fmt.Sprintf("Could not read data, due length missmatch: %#v", b)}
Expand Down Expand Up @@ -156,7 +154,7 @@ func Read(mode *cipher.BlockMode, buf *[]byte, crcFlag *bool, frameSize *uint32,
r := bytes.NewReader((*buf)[RSCP_FRAME_HEADER_SIZE:])
var m []Message
// defer logging to also trace the already read data before the error or panic
defer func() { log.Tracef("read plain %#v", *buf); log.Tracef("read %s", m) }()
defer func() { Log.Tracef("read plain %#v", *buf); Log.Tracef("read %s", m) }()
if err := read(r, &m, *dataSize); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions rscp/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/azihsoyn/rijndael256"
"github.com/go-test/deep"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

func Test_readHeader(t *testing.T) {
Expand Down Expand Up @@ -320,7 +320,7 @@ func Test_read(t *testing.T) {
}
for _, tt := range tests {
// to call debug callback function provided to log.DebugFn at least once
log.SetLevel(log.DebugLevel)
Log.SetLevel(logrus.DebugLevel)
t.Run(tt.name, func(t *testing.T) {
reader := bytes.NewReader(tt.bytes)
err := read(reader, tt.emptydata, tt.size)
Expand Down
8 changes: 3 additions & 5 deletions rscp/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"encoding/binary"
"hash/crc32"
"time"

log "github.com/sirupsen/logrus"
)

// Now returns the current local time.
Expand Down Expand Up @@ -113,21 +111,21 @@ func writeFrame(messages []Message, useChecksum bool) ([]byte, error) {

// Write writes the messages and returns the encrypted rscp frame.
func Write(mode *cipher.BlockMode, messages []Message, useChecksum bool) ([]byte, error) {
log.Debugf("write %s", messages)
Log.Debugf("write %s", messages)
var (
d []byte
err error
)
if d, err = writeFrame(messages, useChecksum); err != nil {
return nil, err
}
log.Tracef("write plain %#v", d)
Log.Tracef("write plain %#v", d)
// padding
if len(d)%int(RSCP_CRYPT_BLOCK_SIZE) != 0 {
d = append(d, bytes.Repeat([]byte{RSCP_CRYPT_BLOCK_PADDING}, int(RSCP_CRYPT_BLOCK_SIZE)-len(d)%int(RSCP_CRYPT_BLOCK_SIZE))...)
}
// encrypt
(*mode).CryptBlocks(d, d)
log.Tracef("write crypt %#v", d)
Log.Tracef("write crypt %#v", d)
return d, nil
}

0 comments on commit 627889b

Please sign in to comment.