From fa40ccece1da3925ce6db890aec295ffc311a1d4 Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Fri, 18 Dec 2020 19:44:00 +0100 Subject: [PATCH] lib: Consistently set suture logging (#7202) --- lib/api/api_test.go | 2 +- lib/beacon/beacon.go | 7 ++----- lib/connections/service.go | 5 +---- lib/db/lowlevel.go | 5 +---- lib/discover/local.go | 2 +- lib/discover/manager.go | 2 +- lib/model/folder_summary.go | 2 +- lib/model/model.go | 5 +---- lib/syncthing/syncthing.go | 5 +---- lib/util/utils.go | 12 +++++++++++- 10 files changed, 21 insertions(+), 26 deletions(-) diff --git a/lib/api/api_test.go b/lib/api/api_test.go index ed38f207e..323f3443d 100644 --- a/lib/api/api_test.go +++ b/lib/api/api_test.go @@ -119,7 +119,7 @@ func TestStopAfterBrokenConfig(t *testing.T) { defer os.Remove(token) srv.started = make(chan string) - sup := suture.New("test", util.Spec()) + sup := suture.New("test", util.SpecWithDebugLogger(l)) sup.Add(srv) ctx, cancel := context.WithCancel(context.Background()) sup.ServeBackground(ctx) diff --git a/lib/beacon/beacon.go b/lib/beacon/beacon.go index ed7545c36..e937d5d1a 100644 --- a/lib/beacon/beacon.go +++ b/lib/beacon/beacon.go @@ -44,16 +44,13 @@ type cast struct { // caller needs to set reader and writer with the addReader and addWriter // methods to get a functional implementation of Interface. func newCast(name string) *cast { - spec := util.Spec() + // Only log restarts in debug mode. + spec := util.SpecWithDebugLogger(l) // Don't retry too frenetically: an error to open a socket or // whatever is usually something that is either permanent or takes // a while to get solved... spec.FailureThreshold = 2 spec.FailureBackoff = 60 * time.Second - // Only log restarts in debug mode. - spec.EventHook = func(e suture.Event) { - l.Debugln(e) - } c := &cast{ Supervisor: suture.New(name, spec), name: name, diff --git a/lib/connections/service.go b/lib/connections/service.go index 7723a12a9..d0c6f119f 100644 --- a/lib/connections/service.go +++ b/lib/connections/service.go @@ -139,10 +139,7 @@ type service struct { } func NewService(cfg config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *tls.Config, discoverer discover.Finder, bepProtocolName string, tlsDefaultCommonName string, evLogger events.Logger) Service { - spec := util.Spec() - spec.EventHook = func(e suture.Event) { - l.Infoln(e) - } + spec := util.SpecWithInfoLogger(l) service := &service{ Supervisor: suture.New("connections.Service", spec), connectionStatusHandler: newConnectionStatusHandler(), diff --git a/lib/db/lowlevel.go b/lib/db/lowlevel.go index 8074564aa..7d8eb2217 100644 --- a/lib/db/lowlevel.go +++ b/lib/db/lowlevel.go @@ -68,11 +68,8 @@ type Lowlevel struct { } func NewLowlevel(backend backend.Backend, opts ...Option) *Lowlevel { - spec := util.Spec() // Only log restarts in debug mode. - spec.EventHook = func(e suture.Event) { - l.Debugln(e) - } + spec := util.SpecWithDebugLogger(l) db := &Lowlevel{ Supervisor: suture.New("db.Lowlevel", spec), Backend: backend, diff --git a/lib/discover/local.go b/lib/discover/local.go index 3769931b3..9dfe17559 100644 --- a/lib/discover/local.go +++ b/lib/discover/local.go @@ -52,7 +52,7 @@ const ( func NewLocal(id protocol.DeviceID, addr string, addrList AddressLister, evLogger events.Logger) (FinderService, error) { c := &localClient{ - Supervisor: suture.New("local", util.Spec()), + Supervisor: suture.New("local", util.SpecWithDebugLogger(l)), myID: id, addrList: addrList, evLogger: evLogger, diff --git a/lib/discover/manager.go b/lib/discover/manager.go index c01c74840..d3d0cf626 100644 --- a/lib/discover/manager.go +++ b/lib/discover/manager.go @@ -47,7 +47,7 @@ type manager struct { func NewManager(myID protocol.DeviceID, cfg config.Wrapper, cert tls.Certificate, evLogger events.Logger, lister AddressLister) Manager { m := &manager{ - Supervisor: suture.New("discover.Manager", util.Spec()), + Supervisor: suture.New("discover.Manager", util.SpecWithDebugLogger(l)), myID: myID, cfg: cfg, cert: cert, diff --git a/lib/model/folder_summary.go b/lib/model/folder_summary.go index 07bc28b68..2b1234513 100644 --- a/lib/model/folder_summary.go +++ b/lib/model/folder_summary.go @@ -52,7 +52,7 @@ type folderSummaryService struct { func NewFolderSummaryService(cfg config.Wrapper, m Model, id protocol.DeviceID, evLogger events.Logger) FolderSummaryService { service := &folderSummaryService{ - Supervisor: suture.New("folderSummaryService", util.Spec()), + Supervisor: suture.New("folderSummaryService", util.SpecWithDebugLogger(l)), cfg: cfg, model: m, id: id, diff --git a/lib/model/model.go b/lib/model/model.go index 5512ccb5d..af3b61b14 100644 --- a/lib/model/model.go +++ b/lib/model/model.go @@ -198,10 +198,7 @@ var ( // where it sends index information to connected peers and responds to requests // for file data without altering the local folder in any way. func NewModel(cfg config.Wrapper, id protocol.DeviceID, clientName, clientVersion string, ldb *db.Lowlevel, protectedFiles []string, evLogger events.Logger) Model { - spec := util.Spec() - spec.EventHook = func(e suture.Event) { - l.Debugln(e) - } + spec := util.SpecWithDebugLogger(l) m := &model{ Supervisor: suture.New("model", spec), diff --git a/lib/syncthing/syncthing.go b/lib/syncthing/syncthing.go index 772ff580f..ee497535c 100644 --- a/lib/syncthing/syncthing.go +++ b/lib/syncthing/syncthing.go @@ -99,10 +99,7 @@ func New(cfg config.Wrapper, dbBackend backend.Backend, evLogger events.Logger, func (a *App) Start() error { // Create a main service manager. We'll add things to this as we go along. // We want any logging it does to go through our log system. - spec := util.Spec() - spec.EventHook = func(e suture.Event) { - l.Debugln(e) - } + spec := util.SpecWithDebugLogger(l) a.mainService = suture.New("main", spec) // Start the supervisor and wait for it to stop to handle cleanup. diff --git a/lib/util/utils.go b/lib/util/utils.go index 8f26c4634..a8a04655c 100644 --- a/lib/util/utils.go +++ b/lib/util/utils.go @@ -16,6 +16,7 @@ import ( "strings" "time" + "github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/sync" "github.com/thejerf/suture/v4" @@ -390,8 +391,17 @@ func OnSupervisorDone(sup *suture.Supervisor, fn func()) { sup.Add(&doneService{fn}) } -func Spec() suture.Spec { +func SpecWithDebugLogger(l logger.Logger) suture.Spec { + return spec(func(e suture.Event) { l.Debugln(e) }) +} + +func SpecWithInfoLogger(l logger.Logger) suture.Spec { + return spec(func(e suture.Event) { l.Infoln(e) }) +} + +func spec(eventHook suture.EventHook) suture.Spec { return suture.Spec{ + EventHook: eventHook, PassThroughPanics: true, DontPropagateTermination: false, }