diff options
author | Josh Bleecher Snyder <josh@tailscale.com> | 2021-01-22 14:11:17 -0800 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-01-26 22:40:20 +0100 |
commit | 7139279cd0b08ebbd2c0322bc01d5678aa00cd0e (patch) | |
tree | b49339aabb016e640f2f6088bc5753395d4239b8 /device/tun.go | |
parent | 37efdcaccfb16e47137728b5462c90d0b2ae8460 (diff) |
device: change logging interface to use functions
This commit overhauls wireguard-go's logging.
The primary, motivating change is to use a function instead
of a *log.Logger as the basic unit of logging.
Using functions provides a lot more flexibility for
people to bring their own logging system.
It also introduces logging helper methods on Device.
These reduce line noise at the call site.
They also allow for log functions to be nil;
when nil, instead of generating a log line and throwing it away,
we don't bother generating it at all.
This spares allocation and pointless work.
This is a breaking change, although the fix required
of clients is fairly straightforward.
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
Diffstat (limited to 'device/tun.go')
-rw-r--r-- | device/tun.go | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/device/tun.go b/device/tun.go index 3816f9b..97f1276 100644 --- a/device/tun.go +++ b/device/tun.go @@ -15,41 +15,37 @@ const DefaultMTU = 1420 func (device *Device) RoutineTUNEventReader() { setUp := false - logDebug := device.log.Debug - logInfo := device.log.Info - logError := device.log.Error - - logDebug.Println("Routine: event worker - started") + device.debugf("Routine: event worker - started") for event := range device.tun.device.Events() { if event&tun.EventMTUUpdate != 0 { mtu, err := device.tun.device.MTU() old := atomic.LoadInt32(&device.tun.mtu) if err != nil { - logError.Println("Failed to load updated MTU of device:", err) + device.errorf("Failed to load updated MTU of device: %v", err) } else if int(old) != mtu { if mtu+MessageTransportSize > MaxMessageSize { - logInfo.Println("MTU updated:", mtu, "(too large)") + device.infof("MTU updated: %v (too large)", mtu) } else { - logInfo.Println("MTU updated:", mtu) + device.infof("MTU updated: %v", mtu) } atomic.StoreInt32(&device.tun.mtu, int32(mtu)) } } if event&tun.EventUp != 0 && !setUp { - logInfo.Println("Interface set up") + device.infof("Interface set up") setUp = true device.Up() } if event&tun.EventDown != 0 && setUp { - logInfo.Println("Interface set down") + device.infof("Interface set down") setUp = false device.Down() } } - logDebug.Println("Routine: event worker - stopped") + device.debugf("Routine: event worker - stopped") device.state.stopping.Done() } |