4
4
"crypto/tls"
5
5
"crypto/x509"
6
6
"fmt"
7
+ "log"
7
8
"net"
8
9
"os"
9
10
"strings"
@@ -52,6 +53,9 @@ type ClientConfiguration struct {
52
53
// the server (tcp+tls only). Leaf (i.e. server) certificates can also
53
54
// be used in case of self-signed certs, or if cert pinning is required.
54
55
TLSRootCAs * x509.CertPool
56
+ // Logger provides a custom sink for log messages.
57
+ // If nil, messages will be written to stdout.
58
+ Logger * log.Logger
55
59
}
56
60
57
61
// Modbus client object.
@@ -81,7 +85,8 @@ func NewClient(conf *ClientConfiguration) (mc *ModbusClient, err error) {
81
85
mc .conf .URL = splitURL [1 ]
82
86
}
83
87
84
- mc .logger = newLogger (fmt .Sprintf ("modbus-client(%s)" , mc .conf .URL ))
88
+ mc .logger = newLogger (
89
+ fmt .Sprintf ("modbus-client(%s)" , mc .conf .URL ), conf .Logger )
85
90
86
91
switch clientType {
87
92
case "rtu" :
@@ -221,7 +226,7 @@ func (mc *ModbusClient) Open() (err error) {
221
226
222
227
// create the RTU transport
223
228
mc .transport = newRTUTransport (
224
- spw , mc .conf .URL , mc .conf .Speed , mc .conf .Timeout )
229
+ spw , mc .conf .URL , mc .conf .Speed , mc .conf .Timeout , mc . conf . Logger )
225
230
226
231
case modbusRTUOverTCP :
227
232
// connect to the remote host
@@ -235,7 +240,7 @@ func (mc *ModbusClient) Open() (err error) {
235
240
236
241
// create the RTU transport
237
242
mc .transport = newRTUTransport (
238
- sock , mc .conf .URL , mc .conf .Speed , mc .conf .Timeout )
243
+ sock , mc .conf .URL , mc .conf .Speed , mc .conf .Timeout , mc . conf . Logger )
239
244
240
245
case modbusRTUOverUDP :
241
246
// open a socket to the remote host (note: no actual connection is
@@ -250,7 +255,7 @@ func (mc *ModbusClient) Open() (err error) {
250
255
// packets byte per byte
251
256
mc .transport = newRTUTransport (
252
257
newUDPSockWrapper (sock ),
253
- mc .conf .URL , mc .conf .Speed , mc .conf .Timeout )
258
+ mc .conf .URL , mc .conf .Speed , mc .conf .Timeout , mc . conf . Logger )
254
259
255
260
case modbusTCP :
256
261
// connect to the remote host
@@ -260,7 +265,7 @@ func (mc *ModbusClient) Open() (err error) {
260
265
}
261
266
262
267
// create the TCP transport
263
- mc .transport = newTCPTransport (sock , mc .conf .Timeout )
268
+ mc .transport = newTCPTransport (sock , mc .conf .Timeout , mc . conf . Logger )
264
269
265
270
case modbusTCPOverTLS :
266
271
// connect to the remote host with TLS
@@ -288,7 +293,7 @@ func (mc *ModbusClient) Open() (err error) {
288
293
}
289
294
290
295
// create the TCP transport
291
- mc .transport = newTCPTransport (sock , mc .conf .Timeout )
296
+ mc .transport = newTCPTransport (sock , mc .conf .Timeout , mc . conf . Logger )
292
297
293
298
case modbusTCPOverUDP :
294
299
// open a socket to the remote host (note: no actual connection is
@@ -302,7 +307,7 @@ func (mc *ModbusClient) Open() (err error) {
302
307
// an adapter to allow the transport to read the stream of
303
308
// packets byte per byte
304
309
mc .transport = newTCPTransport (
305
- newUDPSockWrapper (sock ), mc .conf .Timeout )
310
+ newUDPSockWrapper (sock ), mc .conf .Timeout , mc . conf . Logger )
306
311
307
312
default :
308
313
// should never happen
0 commit comments