From 1dc3180eb07df9898fae90331fb7e040d3852be2 Mon Sep 17 00:00:00 2001 From: Tulili Date: Sun, 25 Feb 2024 22:49:47 -0300 Subject: [PATCH 1/3] fix: implement file-based logging and log levels properly --- cmd/layer/add/add.go | 2 - cmd/layer/build/build.go | 2 - cmd/layer/clean/clean.go | 2 - cmd/layer/list/list.go | 5 +++ cmd/layer/remove/remove.go | 2 - cmd/root.go | 78 +++++++++++++++++++------------------- 6 files changed, 44 insertions(+), 47 deletions(-) diff --git a/cmd/layer/add/add.go b/cmd/layer/add/add.go index 5b8c9b6..4fb17f7 100644 --- a/cmd/layer/add/add.go +++ b/cmd/layer/add/add.go @@ -16,7 +16,6 @@ import ( "github.com/ublue-os/bext/internal" "github.com/ublue-os/bext/pkg/filecomp" "github.com/ublue-os/bext/pkg/fileio" - "github.com/ublue-os/bext/pkg/logging" "github.com/ublue-os/bext/pkg/percentmanager" ) @@ -56,7 +55,6 @@ func addCmd(cmd *cobra.Command, args []string) error { pw := percent.NewProgressWriter() if !*internal.Config.NoProgress { go pw.Render() - slog.SetDefault(logging.NewMuteLogger()) } var expectedSections int = 4 diff --git a/cmd/layer/build/build.go b/cmd/layer/build/build.go index 34a9b97..cb295b7 100644 --- a/cmd/layer/build/build.go +++ b/cmd/layer/build/build.go @@ -20,7 +20,6 @@ import ( "github.com/opencontainers/runtime-spec/specs-go" "github.com/spf13/cobra" "github.com/ublue-os/bext/internal" - "github.com/ublue-os/bext/pkg/logging" "github.com/ublue-os/bext/pkg/percentmanager" ) @@ -79,7 +78,6 @@ func buildCmd(cmd *cobra.Command, args []string) error { if !*internal.Config.NoProgress { go pw.Render() - slog.SetDefault(logging.NewMuteLogger()) } pw.AppendTracker(build_tracker.Tracker) diff --git a/cmd/layer/clean/clean.go b/cmd/layer/clean/clean.go index 734085e..9a5dfda 100644 --- a/cmd/layer/clean/clean.go +++ b/cmd/layer/clean/clean.go @@ -11,7 +11,6 @@ import ( "github.com/jedib0t/go-pretty/v6/progress" "github.com/spf13/cobra" "github.com/ublue-os/bext/internal" - "github.com/ublue-os/bext/pkg/logging" "github.com/ublue-os/bext/pkg/percentmanager" ) @@ -113,7 +112,6 @@ func cleanCmd(cmd *cobra.Command, args []string) error { if !*internal.Config.NoProgress { go pw.Render() - slog.SetDefault(logging.NewMuteLogger()) } for _, entry := range target_cache { diff --git a/cmd/layer/list/list.go b/cmd/layer/list/list.go index 6389e96..a7b0d75 100644 --- a/cmd/layer/list/list.go +++ b/cmd/layer/list/list.go @@ -13,6 +13,7 @@ import ( "github.com/spf13/cobra" "github.com/ublue-os/bext/internal" "github.com/ublue-os/bext/pkg/fileio" + "github.com/ublue-os/bext/pkg/logging" ) var ListCmd = &cobra.Command{ @@ -53,6 +54,10 @@ func listCmd(cmd *cobra.Command, args []string) error { return err } + if !*fLogOnly { + slog.SetDefault(logging.NewMuteLogger()) + } + if *fCheck { if len(args) < 1 { return internal.NewPositionalError("LAYER") diff --git a/cmd/layer/remove/remove.go b/cmd/layer/remove/remove.go index 3b39a57..06115fc 100644 --- a/cmd/layer/remove/remove.go +++ b/cmd/layer/remove/remove.go @@ -10,7 +10,6 @@ import ( "github.com/spf13/cobra" "github.com/ublue-os/bext/internal" "github.com/ublue-os/bext/pkg/fileio" - "github.com/ublue-os/bext/pkg/logging" percent "github.com/ublue-os/bext/pkg/percentmanager" ) @@ -39,7 +38,6 @@ func removeCmd(cmd *cobra.Command, args []string) error { pw := percent.NewProgressWriter() if !*internal.Config.NoProgress { go pw.Render() - slog.SetDefault(logging.NewMuteLogger()) } if len(args) < 1 { diff --git a/cmd/root.go b/cmd/root.go index c98ec77..f2ae1e1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "log/slog" "os" "path" @@ -11,65 +10,66 @@ import ( "github.com/ublue-os/bext/cmd/layer" "github.com/ublue-os/bext/cmd/mount" "github.com/ublue-os/bext/internal" + "github.com/ublue-os/bext/pkg/logging" appLogging "github.com/ublue-os/bext/pkg/logging" ) var RootCmd = &cobra.Command{ - Use: "bext", - Short: "Manager for Systemd system extensions", - Long: `Manage your systemd system extensions from your CLI, managing their cache, multiple versions, and building.`, - SilenceUsage: true, + Use: "bext", + Short: "Manager for Systemd system extensions", + Long: `Manage your systemd system extensions from your CLI, managing their cache, multiple versions, and building.`, + PersistentPreRunE: initLogging, + SilenceUsage: true, } +var ( + fLogFile string + fLogLevel string + fNoLogging bool +) + func Execute() { - err := RootCmd.Execute() - if err != nil { + if err := RootCmd.Execute(); err != nil { + slog.Debug("Application exited with error", slog.String("errormsg", err.Error()), slog.Int("exitcode", 1)) os.Exit(1) } } -var ( - fLogFile *string - fLogLevel *string - fNoLogging *bool -) - -func init() { - fLogFile = RootCmd.PersistentFlags().String("log-file", "", "File where user facing logs will be written to") - fLogLevel = RootCmd.PersistentFlags().String("log-level", "info", "File where user facing logs will be written to") - fNoLogging = RootCmd.PersistentFlags().Bool("quiet", false, "Do not log anything to anywhere") - internal.Config.NoProgress = RootCmd.PersistentFlags().Bool("no-progress", false, "Do not use progress bars whenever they would be") - - RootCmd.AddCommand(layer.LayerCmd) - RootCmd.AddCommand(mount.MountCmd) - RootCmd.AddCommand(AddToPathCmd) - - if *fNoLogging { - slog.SetDefault(appLogging.NewMuteLogger()) - return - } +func initLogging(cmd *cobra.Command, args []string) error { var logWriter *os.File = os.Stdout - if *fLogFile != "" { - abs, err := filepath.Abs(path.Clean(*fLogFile)) + if fLogFile != "-" { + abs, err := filepath.Abs(path.Clean(fLogFile)) if err != nil { - os.Exit(1) + return err } - logWriter, err = os.Create(abs) + logWriter, err = os.OpenFile(abs, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755) if err != nil { - fmt.Println("Could not open log file") - os.Exit(1) + return err } - defer logWriter.Close() } - logLevel, err := appLogging.StrToLogLevel(*fLogLevel) + logLevel, err := appLogging.StrToLogLevel(fLogLevel) if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err.Error()) - os.Exit(1) + return err } - main_app_logger := slog.New(appLogging.SetupAppLogger(logWriter, logLevel, *fLogFile != "")) + main_app_logger := slog.New(appLogging.SetupAppLogger(logWriter, logLevel, fLogFile != "-")) + + if fNoLogging { + slog.SetDefault(logging.NewMuteLogger()) + } else { + slog.SetDefault(main_app_logger) + } + return nil +} - slog.SetDefault(main_app_logger) +func init() { + RootCmd.PersistentFlags().StringVar(&fLogFile, "log-file", "-", "File where user-facing logs will be written to") + RootCmd.PersistentFlags().StringVar(&fLogLevel, "log-level", "info", "Log level for user-facing logs") + RootCmd.PersistentFlags().BoolVar(&fNoLogging, "quiet", false, "Do not log anything to anywhere") + internal.Config.NoProgress = RootCmd.PersistentFlags().Bool("no-progress", false, "Do not use progress bars whenever they would be") + RootCmd.AddCommand(layer.LayerCmd) + RootCmd.AddCommand(mount.MountCmd) + RootCmd.AddCommand(AddToPathCmd) } From 1428b8bb6a788d460f8113b62c70cbf4cf22af84 Mon Sep 17 00:00:00 2001 From: Tulili Date: Sun, 25 Feb 2024 23:07:02 -0300 Subject: [PATCH 2/3] fix: progress bars conflicting with logging --- cmd/layer/add/add.go | 2 ++ cmd/layer/build/build.go | 2 ++ cmd/layer/clean/clean.go | 2 ++ 3 files changed, 6 insertions(+) diff --git a/cmd/layer/add/add.go b/cmd/layer/add/add.go index 4fb17f7..5b8c9b6 100644 --- a/cmd/layer/add/add.go +++ b/cmd/layer/add/add.go @@ -16,6 +16,7 @@ import ( "github.com/ublue-os/bext/internal" "github.com/ublue-os/bext/pkg/filecomp" "github.com/ublue-os/bext/pkg/fileio" + "github.com/ublue-os/bext/pkg/logging" "github.com/ublue-os/bext/pkg/percentmanager" ) @@ -55,6 +56,7 @@ func addCmd(cmd *cobra.Command, args []string) error { pw := percent.NewProgressWriter() if !*internal.Config.NoProgress { go pw.Render() + slog.SetDefault(logging.NewMuteLogger()) } var expectedSections int = 4 diff --git a/cmd/layer/build/build.go b/cmd/layer/build/build.go index cb295b7..34a9b97 100644 --- a/cmd/layer/build/build.go +++ b/cmd/layer/build/build.go @@ -20,6 +20,7 @@ import ( "github.com/opencontainers/runtime-spec/specs-go" "github.com/spf13/cobra" "github.com/ublue-os/bext/internal" + "github.com/ublue-os/bext/pkg/logging" "github.com/ublue-os/bext/pkg/percentmanager" ) @@ -78,6 +79,7 @@ func buildCmd(cmd *cobra.Command, args []string) error { if !*internal.Config.NoProgress { go pw.Render() + slog.SetDefault(logging.NewMuteLogger()) } pw.AppendTracker(build_tracker.Tracker) diff --git a/cmd/layer/clean/clean.go b/cmd/layer/clean/clean.go index 9a5dfda..734085e 100644 --- a/cmd/layer/clean/clean.go +++ b/cmd/layer/clean/clean.go @@ -11,6 +11,7 @@ import ( "github.com/jedib0t/go-pretty/v6/progress" "github.com/spf13/cobra" "github.com/ublue-os/bext/internal" + "github.com/ublue-os/bext/pkg/logging" "github.com/ublue-os/bext/pkg/percentmanager" ) @@ -112,6 +113,7 @@ func cleanCmd(cmd *cobra.Command, args []string) error { if !*internal.Config.NoProgress { go pw.Render() + slog.SetDefault(logging.NewMuteLogger()) } for _, entry := range target_cache { From b4f316375cf18a69bef94c3d80f8a15af5a4f2da Mon Sep 17 00:00:00 2001 From: Tulili Date: Sun, 25 Feb 2024 23:14:08 -0300 Subject: [PATCH 3/3] fix(logging): progress bar in remove cmd --- cmd/layer/remove/remove.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/layer/remove/remove.go b/cmd/layer/remove/remove.go index 06115fc..3b39a57 100644 --- a/cmd/layer/remove/remove.go +++ b/cmd/layer/remove/remove.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" "github.com/ublue-os/bext/internal" "github.com/ublue-os/bext/pkg/fileio" + "github.com/ublue-os/bext/pkg/logging" percent "github.com/ublue-os/bext/pkg/percentmanager" ) @@ -38,6 +39,7 @@ func removeCmd(cmd *cobra.Command, args []string) error { pw := percent.NewProgressWriter() if !*internal.Config.NoProgress { go pw.Render() + slog.SetDefault(logging.NewMuteLogger()) } if len(args) < 1 {