mirror of
https://git.zx2c4.com/wireguard-go
synced 2025-10-05 16:47:02 +08:00
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>
This commit is contained in:

committed by
Jason A. Donenfeld

parent
37efdcaccf
commit
7139279cd0
@@ -130,7 +130,7 @@ func (device *Device) IpcSetOperation(r io.Reader) (err error) {
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
device.log.Error.Println(err)
|
||||
device.errorf("%v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -188,7 +188,7 @@ func (device *Device) handleDeviceLine(key, value string) error {
|
||||
if err != nil {
|
||||
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set private_key: %w", err)
|
||||
}
|
||||
device.log.Debug.Println("UAPI: Updating private key")
|
||||
device.debugf("UAPI: Updating private key")
|
||||
device.SetPrivateKey(sk)
|
||||
|
||||
case "listen_port":
|
||||
@@ -198,7 +198,7 @@ func (device *Device) handleDeviceLine(key, value string) error {
|
||||
}
|
||||
|
||||
// update port and rebind
|
||||
device.log.Debug.Println("UAPI: Updating listen port")
|
||||
device.debugf("UAPI: Updating listen port")
|
||||
|
||||
device.net.Lock()
|
||||
device.net.port = uint16(port)
|
||||
@@ -214,7 +214,7 @@ func (device *Device) handleDeviceLine(key, value string) error {
|
||||
return ipcErrorf(ipc.IpcErrorInvalid, "invalid fwmark: %w", err)
|
||||
}
|
||||
|
||||
device.log.Debug.Println("UAPI: Updating fwmark")
|
||||
device.debugf("UAPI: Updating fwmark")
|
||||
if err := device.BindSetMark(uint32(mark)); err != nil {
|
||||
return ipcErrorf(ipc.IpcErrorPortInUse, "failed to update fwmark: %w", err)
|
||||
}
|
||||
@@ -223,7 +223,7 @@ func (device *Device) handleDeviceLine(key, value string) error {
|
||||
if value != "true" {
|
||||
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set replace_peers, invalid value: %v", value)
|
||||
}
|
||||
device.log.Debug.Println("UAPI: Removing all peers")
|
||||
device.debugf("UAPI: Removing all peers")
|
||||
device.RemoveAllPeers()
|
||||
|
||||
default:
|
||||
@@ -265,7 +265,7 @@ func (device *Device) handlePublicKeyLine(peer *ipcSetPeer, value string) error
|
||||
if err != nil {
|
||||
return ipcErrorf(ipc.IpcErrorInvalid, "failed to create new peer: %w", err)
|
||||
}
|
||||
device.log.Debug.Println(peer, "- UAPI: Created")
|
||||
device.debugf("%v - UAPI: Created", peer.Peer)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -289,14 +289,14 @@ func (device *Device) handlePeerLine(peer *ipcSetPeer, key, value string) error
|
||||
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set remove, invalid value: %v", value)
|
||||
}
|
||||
if !peer.dummy {
|
||||
device.log.Debug.Println(peer, "- UAPI: Removing")
|
||||
device.debugf("%v - UAPI: Removing", peer.Peer)
|
||||
device.RemovePeer(peer.handshake.remoteStatic)
|
||||
}
|
||||
peer.Peer = &Peer{}
|
||||
peer.dummy = true
|
||||
|
||||
case "preshared_key":
|
||||
device.log.Debug.Println(peer, "- UAPI: Updating preshared key")
|
||||
device.debugf("%v - UAPI: Updating preshared key", peer.Peer)
|
||||
|
||||
peer.handshake.mutex.Lock()
|
||||
err := peer.handshake.presharedKey.FromHex(value)
|
||||
@@ -307,7 +307,7 @@ func (device *Device) handlePeerLine(peer *ipcSetPeer, key, value string) error
|
||||
}
|
||||
|
||||
case "endpoint":
|
||||
device.log.Debug.Println(peer, "- UAPI: Updating endpoint")
|
||||
device.debugf("%v - UAPI: Updating endpoint", peer.Peer)
|
||||
endpoint, err := conn.CreateEndpoint(value)
|
||||
if err != nil {
|
||||
return ipcErrorf(ipc.IpcErrorInvalid, "failed to set endpoint %v: %w", value, err)
|
||||
@@ -317,7 +317,7 @@ func (device *Device) handlePeerLine(peer *ipcSetPeer, key, value string) error
|
||||
peer.endpoint = endpoint
|
||||
|
||||
case "persistent_keepalive_interval":
|
||||
device.log.Debug.Println(peer, "- UAPI: Updating persistent keepalive interval")
|
||||
device.debugf("%v - UAPI: Updating persistent keepalive interval", peer.Peer)
|
||||
|
||||
secs, err := strconv.ParseUint(value, 10, 16)
|
||||
if err != nil {
|
||||
@@ -337,7 +337,7 @@ func (device *Device) handlePeerLine(peer *ipcSetPeer, key, value string) error
|
||||
}
|
||||
|
||||
case "replace_allowed_ips":
|
||||
device.log.Debug.Println(peer, "- UAPI: Removing all allowedips")
|
||||
device.debugf("%v - UAPI: Removing all allowedips", peer.Peer)
|
||||
if value != "true" {
|
||||
return ipcErrorf(ipc.IpcErrorInvalid, "failed to replace allowedips, invalid value: %v", value)
|
||||
}
|
||||
@@ -347,7 +347,7 @@ func (device *Device) handlePeerLine(peer *ipcSetPeer, key, value string) error
|
||||
device.allowedips.RemoveByPeer(peer.Peer)
|
||||
|
||||
case "allowed_ip":
|
||||
device.log.Debug.Println(peer, "- UAPI: Adding allowedip")
|
||||
device.debugf("%v - UAPI: Adding allowedip", peer.Peer)
|
||||
|
||||
_, network, err := net.ParseCIDR(value)
|
||||
if err != nil {
|
||||
@@ -414,7 +414,7 @@ func (device *Device) IpcHandle(socket net.Conn) {
|
||||
}
|
||||
err = device.IpcGetOperation(buffered.Writer)
|
||||
default:
|
||||
device.log.Error.Println("invalid UAPI operation:", op)
|
||||
device.errorf("invalid UAPI operation: %v", op)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -425,7 +425,7 @@ func (device *Device) IpcHandle(socket net.Conn) {
|
||||
status = ipcErrorf(ipc.IpcErrorUnknown, "other UAPI error: %w", err)
|
||||
}
|
||||
if status != nil {
|
||||
device.log.Error.Println(status)
|
||||
device.errorf("%v", status)
|
||||
fmt.Fprintf(buffered, "errno=%d\n\n", status.ErrorCode())
|
||||
} else {
|
||||
fmt.Fprintf(buffered, "errno=0\n\n")
|
||||
|
Reference in New Issue
Block a user