From 3988ddc85da469e44ced80b21a3a142ad3e105f6 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 25 Feb 2020 11:35:46 -0800 Subject: [PATCH] types/logger: add WithPrefix, use it in two places Signed-off-by: Brad Fitzpatrick --- ipn/ipnserver/server.go | 7 ++----- ipn/local.go | 4 +--- types/logger/logger.go | 10 +++++++++- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ipn/ipnserver/server.go b/ipn/ipnserver/server.go index d736c4d10..20c7aa5d1 100644 --- a/ipn/ipnserver/server.go +++ b/ipn/ipnserver/server.go @@ -169,11 +169,8 @@ func Run(rctx context.Context, logf logger.Logf, logid string, opts Options, e w oldS = s go func(ctx context.Context, bs *ipn.BackendServer, s net.Conn, i int) { - // TODO: move this prefixing-Logf code into a new helper in types/logger? - si := fmt.Sprintf("%d: ", i) - pump(func(fmt string, args ...interface{}) { - logf(si+fmt, args...) - }, ctx, bs, s) + logf := logger.WithPrefix(logf, fmt.Sprintf("%d: ", i)) + pump(logf, ctx, bs, s) if !opts.SurviveDisconnects || bs.GotQuit { bs.Reset() s.Close() diff --git a/ipn/local.go b/ipn/local.go index e8f85e4b4..62e09e477 100644 --- a/ipn/local.go +++ b/ipn/local.go @@ -174,9 +174,7 @@ func (b *LocalBackend) Start(opts Options) error { persist = &controlclient.Persist{} } cli, err := controlclient.New(controlclient.Options{ - Logf: func(fmt string, args ...interface{}) { - b.logf("control: "+fmt, args...) - }, + Logf: logger.WithPrefix(b.logf, "control: "), Persist: *persist, ServerURL: b.serverURL, Hostinfo: hi, diff --git a/types/logger/logger.go b/types/logger/logger.go index 6c5d25292..bd335bd98 100644 --- a/types/logger/logger.go +++ b/types/logger/logger.go @@ -7,4 +7,12 @@ // types around. package logger -type Logf func(fmt string, args ...interface{}) +// Logf is the basic Tailscale logger type: a printf-like func. +type Logf func(format string, args ...interface{}) + +// WithPrefix wraps f, prefixing each format with the provided prefix. +func WithPrefix(f Logf, prefix string) Logf { + return func(format string, args ...interface{}) { + f(prefix+format, args...) + } +}