2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package logger defines a type for writing to logs. It's just a
|
|
|
|
// convenience type so that we don't have to pass verbose func(...)
|
|
|
|
// types around.
|
|
|
|
package logger
|
|
|
|
|
2020-03-06 19:59:46 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2020-02-25 19:35:46 +00:00
|
|
|
// 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...)
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 19:59:46 +00:00
|
|
|
|
|
|
|
// FuncWriter returns an io.Writer that writes to f.
|
|
|
|
func FuncWriter(f Logf) io.Writer {
|
|
|
|
return funcWriter{f}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StdLogger returns a standard library logger from a Logf.
|
|
|
|
func StdLogger(f Logf) *log.Logger {
|
|
|
|
return log.New(FuncWriter(f), "", 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
type funcWriter struct{ f Logf }
|
|
|
|
|
|
|
|
func (w funcWriter) Write(p []byte) (int, error) {
|
|
|
|
w.f("%s", p)
|
|
|
|
return len(p), nil
|
|
|
|
}
|