Skip to content

Commit 8e15962

Browse files
committed
Simplify logging with gather's closeConnAndLog()
1 parent ecfb354 commit 8e15962

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

gather.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ const (
2525
)
2626

2727
// Close a net.Conn and log if we have a failure
28-
func closeConnAndLog(c io.Closer, log logging.LeveledLogger, msg string) {
28+
func closeConnAndLog(c io.Closer, log logging.LeveledLogger, msg string, args ...interface{}) {
2929
if c == nil || (reflect.ValueOf(c).Kind() == reflect.Ptr && reflect.ValueOf(c).IsNil()) {
30-
log.Warnf("Connection is not allocated (%s)", msg)
30+
log.Warnf("Connection is not allocated: "+msg, args...)
3131
return
3232
}
3333

@@ -229,13 +229,13 @@ func (a *Agent) gatherCandidatesLocal(ctx context.Context, networkTypes []Networ
229229

230230
c, err := NewCandidateHost(&hostConfig)
231231
if err != nil {
232-
closeConnAndLog(connAndPort.conn, a.log, fmt.Sprintf("Failed to create host candidate: %s %s %d: %v", network, mappedIP, connAndPort.port, err))
232+
closeConnAndLog(connAndPort.conn, a.log, "failed to create host candidate: %s %s %d: %v", network, mappedIP, connAndPort.port, err)
233233
continue
234234
}
235235

236236
if a.mDNSMode == MulticastDNSModeQueryAndGather {
237237
if err = c.setIP(ip); err != nil {
238-
closeConnAndLog(connAndPort.conn, a.log, fmt.Sprintf("Failed to create host candidate: %s %s %d: %v", network, mappedIP, connAndPort.port, err))
238+
closeConnAndLog(connAndPort.conn, a.log, "failed to create host candidate: %s %s %d: %v", network, mappedIP, connAndPort.port, err)
239239
continue
240240
}
241241
}
@@ -297,7 +297,7 @@ func (a *Agent) gatherCandidatesLocalUDPMux(ctx context.Context) error { //nolin
297297

298298
c, err := NewCandidateHost(&hostConfig)
299299
if err != nil {
300-
closeConnAndLog(conn, a.log, fmt.Sprintf("Failed to create host mux candidate: %s %d: %v", candidateIP, udpAddr.Port, err))
300+
closeConnAndLog(conn, a.log, "failed to create host mux candidate: %s %d: %v", candidateIP, udpAddr.Port, err)
301301
continue
302302
}
303303

@@ -306,7 +306,7 @@ func (a *Agent) gatherCandidatesLocalUDPMux(ctx context.Context) error { //nolin
306306
a.log.Warnf("Failed to close candidate: %v", closeErr)
307307
}
308308

309-
closeConnAndLog(conn, a.log, fmt.Sprintf("Failed to add candidate: %s %d: %v", candidateIP, udpAddr.Port, err))
309+
closeConnAndLog(conn, a.log, "failed to add candidate: %s %d: %v", candidateIP, udpAddr.Port, err)
310310
continue
311311
}
312312

@@ -344,7 +344,7 @@ func (a *Agent) gatherCandidatesSrflxMapped(ctx context.Context, networkTypes []
344344

345345
mappedIP, err := a.extIPMapper.findExternalIP(lAddr.IP.String())
346346
if err != nil {
347-
closeConnAndLog(conn, a.log, fmt.Sprintf("1:1 NAT mapping is enabled but no external IP is found for %s", lAddr.IP.String()))
347+
closeConnAndLog(conn, a.log, "1:1 NAT mapping is enabled but no external IP is found for %s", lAddr.IP.String())
348348
return
349349
}
350350

@@ -358,11 +358,11 @@ func (a *Agent) gatherCandidatesSrflxMapped(ctx context.Context, networkTypes []
358358
}
359359
c, err := NewCandidateServerReflexive(&srflxConfig)
360360
if err != nil {
361-
closeConnAndLog(conn, a.log, fmt.Sprintf("Failed to create server reflexive candidate: %s %s %d: %v",
361+
closeConnAndLog(conn, a.log, "failed to create server reflexive candidate: %s %s %d: %v",
362362
network,
363363
mappedIP.String(),
364364
lAddr.Port,
365-
err))
365+
err)
366366
return
367367
}
368368

@@ -428,7 +428,7 @@ func (a *Agent) gatherCandidatesSrflxUDPMux(ctx context.Context, urls []*URL, ne
428428
}
429429
c, err := NewCandidateServerReflexive(&srflxConfig)
430430
if err != nil {
431-
closeConnAndLog(conn, a.log, fmt.Sprintf("Failed to create server reflexive candidate: %s %s %d: %v", network, ip, port, err))
431+
closeConnAndLog(conn, a.log, "failed to create server reflexive candidate: %s %s %d: %v", network, ip, port, err)
432432
return
433433
}
434434

@@ -467,7 +467,7 @@ func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*URL, networkT
467467

468468
conn, err := listenUDPInPortRange(a.net, a.log, int(a.portMax), int(a.portMin), network, &net.UDPAddr{IP: nil, Port: 0})
469469
if err != nil {
470-
closeConnAndLog(conn, a.log, fmt.Sprintf("Failed to listen for %s: %v", serverAddr.String(), err))
470+
closeConnAndLog(conn, a.log, "failed to listen for %s: %v", serverAddr.String(), err)
471471
return
472472
}
473473
// If the agent closes midway through the connection
@@ -485,7 +485,7 @@ func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*URL, networkT
485485

486486
xorAddr, err := stunx.GetXORMappedAddr(conn, serverAddr, stunGatherTimeout)
487487
if err != nil {
488-
closeConnAndLog(conn, a.log, fmt.Sprintf("could not get server reflexive address %s %s: %v", network, url, err))
488+
closeConnAndLog(conn, a.log, "failed to get server reflexive address %s %s: %v", network, url, err)
489489
return
490490
}
491491

@@ -503,7 +503,7 @@ func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*URL, networkT
503503
}
504504
c, err := NewCandidateServerReflexive(&srflxConfig)
505505
if err != nil {
506-
closeConnAndLog(conn, a.log, fmt.Sprintf("Failed to create server reflexive candidate: %s %s %d: %v", network, ip, port, err))
506+
closeConnAndLog(conn, a.log, "failed to create server reflexive candidate: %s %s %d: %v", network, ip, port, err)
507507
return
508508
}
509509

@@ -661,20 +661,20 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*URL) { //noli
661661
Net: a.net,
662662
})
663663
if err != nil {
664-
closeConnAndLog(locConn, a.log, fmt.Sprintf("Failed to build new turn.Client %s %s", TURNServerAddr, err))
664+
closeConnAndLog(locConn, a.log, "failed to create new TURN client %s %s", TURNServerAddr, err)
665665
return
666666
}
667667

668668
if err = client.Listen(); err != nil {
669669
client.Close()
670-
closeConnAndLog(locConn, a.log, fmt.Sprintf("Failed to listen on turn.Client %s %s", TURNServerAddr, err))
670+
closeConnAndLog(locConn, a.log, "failed to listen on TURN client %s %s", TURNServerAddr, err)
671671
return
672672
}
673673

674674
relayConn, err := client.Allocate()
675675
if err != nil {
676676
client.Close()
677-
closeConnAndLog(locConn, a.log, fmt.Sprintf("Failed to allocate on turn.Client %s %s", TURNServerAddr, err))
677+
closeConnAndLog(locConn, a.log, "failed to allocate on TURN client %s %s", TURNServerAddr, err)
678678
return
679679
}
680680

@@ -702,7 +702,7 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*URL) { //noli
702702
relayConnClose()
703703

704704
client.Close()
705-
closeConnAndLog(locConn, a.log, fmt.Sprintf("Failed to create relay candidate: %s %s: %v", network, rAddr.String(), err))
705+
closeConnAndLog(locConn, a.log, "failed to create relay candidate: %s %s: %v", network, rAddr.String(), err)
706706
return
707707
}
708708

0 commit comments

Comments
 (0)