diff --git a/internal/home/controlinstall.go b/internal/home/controlinstall.go index 42ea33c6..085003dc 100644 --- a/internal/home/controlinstall.go +++ b/internal/home/controlinstall.go @@ -243,27 +243,18 @@ func checkDNSStubListener(ctx context.Context, l *slog.Logger) (ok bool) { return false } - var cmd *exec.Cmd - var err error - - defer func() { - if ok { - return - } - - l.ErrorContext( + cmd := exec.Command("systemctl", "is-enabled", "systemd-resolved") + l.DebugContext(ctx, "executing", "cmd", cmd.Path, "args", cmd.Args) + _, err := cmd.Output() + if err != nil || cmd.ProcessState.ExitCode() != 0 { + l.InfoContext( ctx, "execution failed", "cmd", cmd.Path, "code", cmd.ProcessState.ExitCode(), slogutil.KeyError, err, ) - }() - cmd = exec.Command("systemctl", "is-enabled", "systemd-resolved") - l.DebugContext(ctx, "executing", "cmd", cmd.Path, "args", cmd.Args) - _, err = cmd.Output() - if err != nil || cmd.ProcessState.ExitCode() != 0 { return false } @@ -271,6 +262,14 @@ func checkDNSStubListener(ctx context.Context, l *slog.Logger) (ok bool) { l.DebugContext(ctx, "executing", "cmd", cmd.Path, "args", cmd.Args) _, err = cmd.Output() if err != nil || cmd.ProcessState.ExitCode() != 0 { + l.InfoContext( + ctx, + "execution failed", + "cmd", cmd.Path, + "code", cmd.ProcessState.ExitCode(), + slogutil.KeyError, err, + ) + return false } diff --git a/internal/home/home.go b/internal/home/home.go index 2827dce2..cb9f7fc4 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -531,9 +531,11 @@ func initWeb( baseLogger *slog.Logger, customURL bool, ) (web *webAPI, err error) { + logger := baseLogger.With(slogutil.KeyPrefix, "webapi") + var clientFS fs.FS if opts.localFrontend { - log.Info("warning: using local frontend files") + logger.WarnContext(ctx, "using local frontend files") clientFS = os.DirFS("build/static") } else { @@ -546,7 +548,9 @@ func initWeb( disableUpdate := !isUpdateEnabled(ctx, baseLogger, &opts, customURL) webConf := &webConfig{ - updater: upd, + updater: upd, + logger: logger, + baseLogger: baseLogger, clientFS: clientFS, @@ -562,7 +566,7 @@ func initWeb( serveHTTP3: config.DNS.ServeHTTP3, } - web = newWebAPI(ctx, webConf, baseLogger) + web = newWebAPI(ctx, webConf) if web == nil { return nil, errors.Error("can not initialize web") } diff --git a/internal/home/web.go b/internal/home/web.go index a88e672d..56032ed7 100644 --- a/internal/home/web.go +++ b/internal/home/web.go @@ -41,6 +41,13 @@ const ( type webConfig struct { updater *updater.Updater + // logger is a slog logger used in webAPI. It must not be nil. + logger *slog.Logger + + // baseLogger is used to create loggers for other entities. It must not be + // nil. + baseLogger *slog.Logger + clientFS fs.FS // BindAddr is the binding address with port for plain HTTP web interface. @@ -109,14 +116,13 @@ type webAPI struct { // must not be nil. // // TODO(a.garipov): Return a proper error. -func newWebAPI(ctx context.Context, conf *webConfig, baseLogger *slog.Logger) (w *webAPI) { - logger := baseLogger.With(slogutil.KeyPrefix, "webapi") - logger.InfoContext(ctx, "initializing") +func newWebAPI(ctx context.Context, conf *webConfig) (w *webAPI) { + conf.logger.InfoContext(ctx, "initializing") w = &webAPI{ conf: conf, - logger: logger, - baseLogger: baseLogger, + logger: conf.logger, + baseLogger: conf.baseLogger, } clientFS := http.FileServer(http.FS(conf.clientFS)) @@ -126,7 +132,7 @@ func newWebAPI(ctx context.Context, conf *webConfig, baseLogger *slog.Logger) (w // add handlers for /install paths, we only need them when we're not configured yet if conf.firstRun { - logger.InfoContext( + conf.logger.InfoContext( ctx, "This is the first launch of AdGuard Home, redirecting everything to /install.html", ) @@ -204,10 +210,10 @@ const loggerKeyServer = "server" // start - start serving HTTP requests func (web *webAPI) start(ctx context.Context) { - web.logger.InfoContext(ctx, "AdGuard Home is available at the following addresses:") - defer slogutil.RecoverAndLog(ctx, web.logger) + web.logger.InfoContext(ctx, "AdGuard Home is available at the following addresses:") + // for https, we have a separate goroutine loop go web.tlsServerLoop(ctx) @@ -358,10 +364,10 @@ func startPprof(baseLogger *slog.Logger, port uint16) { mux := http.NewServeMux() httputil.RoutePprof(mux) - go func() { - ctx := context.Background() - logger := baseLogger.With(slogutil.KeyPrefix, "pprof") + ctx := context.Background() + logger := baseLogger.With(slogutil.KeyPrefix, "pprof") + go func() { defer slogutil.RecoverAndLog(ctx, logger) logger.InfoContext(ctx, "listening", "addr", addr)