Skip to content
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
4 changes: 2 additions & 2 deletions ascii_over_tcp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (mb *asciiTCPTransporter) Send(aduRequest []byte) (aduResponse []byte, err
}

// Send the request
mb.Debug("modbus: send %q\n", aduRequest)
mb.logf("modbus: send %q\n", aduRequest)
if _, err = mb.conn.Write(aduRequest); err != nil {
return
}
Expand All @@ -78,6 +78,6 @@ func (mb *asciiTCPTransporter) Send(aduRequest []byte) (aduResponse []byte, err
}
}
aduResponse = data[:length]
mb.Debug("modbus: recv %q\n", aduResponse)
mb.logf("modbus: recv %q\n", aduResponse)
return
}
4 changes: 2 additions & 2 deletions asciiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (mb *asciiSerialTransporter) Send(aduRequest []byte) (aduResponse []byte, e
mb.startCloseTimer()

// Send the request
mb.Debug("modbus: send % x\n", aduRequest)
mb.logf("modbus: send % x\n", aduRequest)
if _, err = mb.port.Write(aduRequest); err != nil {
return
}
Expand All @@ -204,7 +204,7 @@ func (mb *asciiSerialTransporter) Send(aduRequest []byte) (aduResponse []byte, e
}
}
aduResponse = data[:length]
mb.Debug("modbus: recv % x\n", aduResponse)
mb.logf("modbus: recv % x\n", aduResponse)
return
}

Expand Down
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"fmt"
)

// Logger is the interface to the required logging functions
type Logger interface {
Printf(format string, v ...any)
}

// DataSizeError represents an error for invalid data-sizes i.e. for cases
// where the data-size does not match the expectation.
type DataSizeError struct {
Expand Down
11 changes: 11 additions & 0 deletions cmd/modbus-cli/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import "log/slog"

type debugAdapter struct {
*slog.Logger
}

func (log *debugAdapter) Printf(msg string, args ...any) {
log.Logger.Debug(msg, args...)
}
6 changes: 3 additions & 3 deletions cmd/modbus-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func main() {
startReg := uint16(*register)

if *logframe {
opt.logger = logger
opt.logger = &debugAdapter{logger}
}

var (
Expand Down Expand Up @@ -238,7 +238,7 @@ func convertToBytes(eType string, order binary.ByteOrder, forcedOrder string, va
}

func resultToFile(r []byte, filename string) error {
return os.WriteFile(filename, r, 0644)
return os.WriteFile(filename, r, 0o644)
}

func resultToRawString(r []byte, startReg int) (string, error) {
Expand Down Expand Up @@ -421,7 +421,7 @@ type option struct {
slaveID int
timeout time.Duration

logger *slog.Logger
logger modbus.Logger

rtu struct {
baudrate int
Expand Down
4 changes: 2 additions & 2 deletions rtu_over_tcp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (mb *rtuTCPTransporter) Send(aduRequest []byte) (aduResponse []byte, err er
}

// Send the request
mb.Debug("modbus: send % x\n", aduRequest)
mb.logf("modbus: send % x\n", aduRequest)
if _, err = mb.conn.Write(aduRequest); err != nil {
return
}
Expand Down Expand Up @@ -95,6 +95,6 @@ func (mb *rtuTCPTransporter) Send(aduRequest []byte) (aduResponse []byte, err er
return
}
aduResponse = data[:n]
mb.Debug("modbus: recv % x\n", aduResponse)
mb.logf("modbus: recv % x\n", aduResponse)
return
}
5 changes: 2 additions & 3 deletions rtu_over_udp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package modbus
import (
"fmt"
"io"
logger "log/slog"
"net"
"sync"
)
Expand Down Expand Up @@ -46,7 +45,7 @@ type rtuUDPTransporter struct {
// Connect string
Address string
// Transmission logger
Logger *logger.Logger
Logger Logger

// UDP connection
mu sync.Mutex
Expand Down Expand Up @@ -121,7 +120,7 @@ func (mb *rtuUDPTransporter) Send(aduRequest []byte) (aduResponse []byte, err er

func (mb *rtuUDPTransporter) logf(format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.Info(format, v...)
mb.Logger.Printf(format, v...)
}
}

Expand Down
4 changes: 2 additions & 2 deletions rtuclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (mb *rtuSerialTransporter) Send(aduRequest []byte) (aduResponse []byte, err
mb.startCloseTimer()

// Send the request
mb.Debug("modbus: send % x\n", aduRequest)
mb.logf("modbus: send % x\n", aduRequest)
if _, err = mb.port.Write(aduRequest); err != nil {
return
}
Expand All @@ -271,7 +271,7 @@ func (mb *rtuSerialTransporter) Send(aduRequest []byte) (aduResponse []byte, err
time.Sleep(mb.calculateDelay(len(aduRequest) + bytesToRead))

data, err := readIncrementally(aduRequest[0], aduRequest[1], mb.port, time.Now().Add(mb.Config.Timeout))
mb.Debug("modbus: recv % x\n", data[:])
mb.logf("modbus: recv % x\n", data[:])
aduResponse = data
return
}
Expand Down
40 changes: 4 additions & 36 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
package modbus

import (
"context"
"fmt"
"io"
"log/slog"
"sync"
"time"

Expand All @@ -26,7 +24,7 @@ type serialPort struct {
// Serial port configuration.
serial.Config

Logger *slog.Logger
Logger Logger
IdleTimeout time.Duration

mu sync.Mutex
Expand Down Expand Up @@ -71,39 +69,9 @@ func (mb *serialPort) close() (err error) {
return
}

func (mb *serialPort) Debug(format string, v ...interface{}) {
func (mb *serialPort) logf(format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.Debug(format, v...)
}
}

func (mb *serialPort) Info(format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.Info(format, v...)
}
}

func (mb *serialPort) Error(format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.Error(format, v...)
}
}

func (mb *serialPort) DebugContext(ctx context.Context, format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.DebugContext(ctx, format, v...)
}
}

func (mb *serialPort) InfoContext(ctx context.Context, format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.InfoContext(ctx, format, v...)
}
}

func (mb *serialPort) ErrorContext(ctx context.Context, format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.ErrorContext(ctx, format, v...)
mb.Logger.Printf(format, v...)
}
}

Expand All @@ -128,7 +96,7 @@ func (mb *serialPort) closeIdle() {
}

if idle := time.Since(mb.lastActivity); idle >= mb.IdleTimeout {
mb.Debug("modbus: closing connection due to idle timeout: %v", idle)
mb.logf("modbus: closing connection due to idle timeout: %v", idle)
mb.close()
}
}
46 changes: 7 additions & 39 deletions tcpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
package modbus

import (
"context"
"encoding/binary"
"fmt"
"io"
"log/slog"
"net"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -138,7 +136,7 @@ type tcpTransporter struct {
// Silent period after successful connection
ConnectDelay time.Duration
// Transmission logger
Logger *slog.Logger
Logger Logger

// TCP connection
mu sync.Mutex
Expand Down Expand Up @@ -185,7 +183,7 @@ func (mb *tcpTransporter) Send(aduRequest []byte) (aduResponse []byte, err error
return
}
// Send data
mb.Debug("modbus: send % x", aduRequest)
mb.logf("modbus: send % x", aduRequest)
if _, err = mb.conn.Write(aduRequest); err != nil {
return
}
Expand All @@ -203,7 +201,7 @@ func (mb *tcpTransporter) Send(aduRequest []byte) (aduResponse []byte, err error
continue
}

mb.Debug("modbus: close connection and retry, because of %v", err)
mb.logf("modbus: close connection and retry, because of %v", err)

mb.close()
time.Sleep(mb.LinkRecoveryTimeout)
Expand All @@ -218,7 +216,7 @@ func (mb *tcpTransporter) readResponse(aduRequest []byte, data []byte, recoveryD
if err == nil {
err = verify(aduRequest, aduResponse)
if err == nil {
mb.Debug("modbus: recv % x\n", aduResponse)
mb.logf("modbus: recv % x\n", aduResponse)
return // everything is OK
}
}
Expand Down Expand Up @@ -384,39 +382,9 @@ func (mb *tcpTransporter) flush(b []byte) (err error) {
return
}

func (mb *tcpTransporter) Debug(format string, v ...interface{}) {
func (mb *tcpTransporter) logf(format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.Debug(format, v...)
}
}

func (mb *tcpTransporter) Info(format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.Info(format, v...)
}
}

func (mb *tcpTransporter) Error(format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.Error(format, v...)
}
}

func (mb *tcpTransporter) DebugContext(ctx context.Context, format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.DebugContext(ctx, format, v...)
}
}

func (mb *tcpTransporter) InfoContext(ctx context.Context, format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.InfoContext(ctx, format, v...)
}
}

func (mb *tcpTransporter) ErrorContext(ctx context.Context, format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.ErrorContext(ctx, format, v...)
mb.Logger.Printf(format, v...)
}
}

Expand All @@ -439,7 +407,7 @@ func (mb *tcpTransporter) closeIdle() {
}

if idle := time.Since(mb.lastActivity); idle >= mb.IdleTimeout {
mb.Debug("modbus: closing connection due to idle timeout: %v", idle)
mb.logf("modbus: closing connection due to idle timeout: %v", idle)
mb.close()
}
}
5 changes: 2 additions & 3 deletions test/asciiclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
package test

import (
"log/slog"
"os"
"log"
"testing"

"github.com/grid-x/modbus"
Expand All @@ -30,7 +29,7 @@ func TestASCIIClientAdvancedUsage(t *testing.T) {
handler.Parity = "E"
handler.StopBits = 1
handler.SlaveID = 12
handler.Logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
handler.Logger = log.Default()
err := handler.Connect()
if err != nil {
t.Fatal(err)
Expand Down
5 changes: 2 additions & 3 deletions test/rtu_over_tcp_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
package test

import (
"log/slog"
"os"
"log"
"testing"
"time"

Expand All @@ -28,7 +27,7 @@ func TestRTUOverTCPClientAdvancedUsage(t *testing.T) {
handler := modbus.NewRTUOverTCPClientHandler(rtuOverTCPDevice)
handler.Timeout = 5 * time.Second
handler.SlaveID = 1
handler.Logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
handler.Logger = log.Default()
handler.Connect()
defer handler.Close()

Expand Down
5 changes: 2 additions & 3 deletions test/rtuclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
package test

import (
"log/slog"
"os"
"log"
"testing"

"github.com/grid-x/modbus"
Expand All @@ -30,7 +29,7 @@ func TestRTUClientAdvancedUsage(t *testing.T) {
handler.Parity = "E"
handler.StopBits = 1
handler.SlaveID = 11
handler.Logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
handler.Logger = log.Default()
err := handler.Connect()
if err != nil {
t.Fatal(err)
Expand Down
5 changes: 2 additions & 3 deletions test/tcpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
package test

import (
"log/slog"
"os"
"log"
"testing"
"time"

Expand All @@ -26,7 +25,7 @@ func TestTCPClientAdvancedUsage(t *testing.T) {
handler := modbus.NewTCPClientHandler(tcpDevice)
handler.Timeout = 5 * time.Second
handler.SlaveID = 1
handler.Logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
handler.Logger = log.Default()
handler.Connect()
defer handler.Close()

Expand Down