From 577313729066e52a019bd631a2c5bff98cba956a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Tue, 31 Oct 2023 17:57:18 +0100 Subject: [PATCH] Basic log functionality --- windows-agent/cmd/ubuntu-pro-agent/main.go | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/windows-agent/cmd/ubuntu-pro-agent/main.go b/windows-agent/cmd/ubuntu-pro-agent/main.go index ae0be32dd..31f7a8288 100644 --- a/windows-agent/cmd/ubuntu-pro-agent/main.go +++ b/windows-agent/cmd/ubuntu-pro-agent/main.go @@ -3,10 +3,14 @@ package main import ( "context" + "errors" + "fmt" "os" "os/signal" + "path/filepath" "sync" "syscall" + "time" "github.com/canonical/ubuntu-pro-for-windows/common" "github.com/canonical/ubuntu-pro-for-windows/common/i18n" @@ -40,6 +44,13 @@ func run(a app) int { ForceColors: true, }) + cleanup, err := setLoggerOutput() + if err != nil { + log.Warningf("could not set logger output: %v", err) + } else { + defer cleanup() + } + if err := a.Run(); err != nil { log.Error(context.Background(), err) @@ -52,6 +63,25 @@ func run(a app) int { return 0 } +func setLoggerOutput() (func(), error) { + lad := os.Getenv("LocalAppData") + if lad == "" { + return nil, errors.New("could not find LocalAppData") + } + + p := filepath.Join(lad, common.LocalAppDataDir, "log") + + f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE, 0600) + if err != nil { + return nil, fmt.Errorf("could not open log file: %v", err) + } + + fmt.Fprintf(f, "\n======== Startup %s ========\n", time.Now().Format(time.RFC3339)) + log.SetOutput(f) + + return func() { _ = f.Close() }, nil +} + func installSignalHandler(a app) func() { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)