ipn/ipnserver, logpolicy: move Windows disk logging up earlier

This moves the Windows-only initialization of the filelogger into
logpolicy. Previously we only did it when babysitting the tailscaled
subprocess, but this meant that log messages from the service itself
never made it to disk. Examples that weren't logged to disk:

* logtail unable to dial out,
* DNS flush messages from the service
* svc.ChangeRequest messages (#3581)

This is basically the same fix as #3571 but staying in the Logf type,
and avoiding build-tagged file (which wasn't quite a goal, but
happened and seemed nice)

Fixes #3570

Co-authored-by: Aaron Klotz <aaron@tailscale.com>
Change-Id: Iacd80c4720b7218365ec80ae143339d030842702
pull/3592/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent 689426d6bc
commit 40e2b312b6

@ -181,7 +181,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
tailscale.com/ipn/policy from tailscale.com/ipn/ipnlocal tailscale.com/ipn/policy from tailscale.com/ipn/ipnlocal
tailscale.com/ipn/store/aws from tailscale.com/ipn/ipnserver tailscale.com/ipn/store/aws from tailscale.com/ipn/ipnserver
tailscale.com/kube from tailscale.com/ipn tailscale.com/kube from tailscale.com/ipn
tailscale.com/log/filelogger from tailscale.com/ipn/ipnserver tailscale.com/log/filelogger from tailscale.com/logpolicy
tailscale.com/log/logheap from tailscale.com/control/controlclient tailscale.com/log/logheap from tailscale.com/control/controlclient
tailscale.com/logpolicy from tailscale.com/cmd/tailscaled tailscale.com/logpolicy from tailscale.com/cmd/tailscaled
tailscale.com/logtail from tailscale.com/logpolicy+ tailscale.com/logtail from tailscale.com/logpolicy+

@ -35,7 +35,6 @@ import (
"tailscale.com/ipn/ipnlocal" "tailscale.com/ipn/ipnlocal"
"tailscale.com/ipn/localapi" "tailscale.com/ipn/localapi"
"tailscale.com/ipn/store/aws" "tailscale.com/ipn/store/aws"
"tailscale.com/log/filelogger"
"tailscale.com/logtail/backoff" "tailscale.com/logtail/backoff"
"tailscale.com/net/netstat" "tailscale.com/net/netstat"
"tailscale.com/net/tsdial" "tailscale.com/net/tsdial"
@ -869,14 +868,6 @@ func BabysitProc(ctx context.Context, args []string, logf logger.Logf) {
panic("cannot determine executable: " + err.Error()) panic("cannot determine executable: " + err.Error())
} }
if runtime.GOOS == "windows" {
if len(args) != 2 && args[0] != "/subproc" {
panic(fmt.Sprintf("unexpected arguments %q", args))
}
logID := args[1]
logf = filelogger.New("tailscale-service", logID, logf)
}
var proc struct { var proc struct {
mu sync.Mutex mu sync.Mutex
p *os.Process p *os.Process

@ -13,6 +13,7 @@ import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
@ -29,6 +30,7 @@ import (
"golang.org/x/term" "golang.org/x/term"
"tailscale.com/atomicfile" "tailscale.com/atomicfile"
"tailscale.com/log/filelogger"
"tailscale.com/logtail" "tailscale.com/logtail"
"tailscale.com/logtail/filch" "tailscale.com/logtail/filch"
"tailscale.com/net/dnscache" "tailscale.com/net/dnscache"
@ -524,8 +526,20 @@ func New(collection string) *Policy {
} }
} }
lw := logtail.NewLogger(c, log.Printf) lw := logtail.NewLogger(c, log.Printf)
var logOutput io.Writer = lw
if runtime.GOOS == "windows" && c.Collection == logtail.CollectionNode {
logID := newc.PublicID.String()
exe, _ := os.Executable()
if strings.EqualFold(filepath.Base(exe), "tailscaled.exe") {
diskLogf := filelogger.New("tailscale-service", logID, lw.Logf)
logOutput = logger.FuncWriter(diskLogf)
}
}
log.SetFlags(0) // other logflags are set on console, not here log.SetFlags(0) // other logflags are set on console, not here
log.SetOutput(lw) log.SetOutput(logOutput)
log.Printf("Program starting: v%v, Go %v: %#v", log.Printf("Program starting: v%v, Go %v: %#v",
version.Long, version.Long,

@ -523,6 +523,11 @@ func (l *Logger) encode(buf []byte) []byte {
return b return b
} }
// Logf logs to l using the provided fmt-style format and optional arguments.
func (l *Logger) Logf(format string, args ...interface{}) {
fmt.Fprintf(l, format, args...)
}
// Write logs an encoded JSON blob. // Write logs an encoded JSON blob.
// //
// If the []byte passed to Write is not an encoded JSON blob, // If the []byte passed to Write is not an encoded JSON blob,

Loading…
Cancel
Save