From 01b70f265116eff780fba3e8873e80fdffc46dd7 Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Sat, 25 Jan 2025 17:05:00 +0100 Subject: [PATCH] cmd/incus-user: unify logging, support --verbose and --debug Refactor logging setup so that it matches the rest of the code base. Add support for --verbose and --debug, similarly to other commands. Signed-off-by: Maciek Borzecki --- cmd/incus-user/main.go | 25 +++++++++++++++++++++---- cmd/incus-user/main_daemon.go | 17 +++++------------ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/cmd/incus-user/main.go b/cmd/incus-user/main.go index 7a9c305d0..c1b2b8862 100644 --- a/cmd/incus-user/main.go +++ b/cmd/incus-user/main.go @@ -1,19 +1,28 @@ package main import ( + "fmt" "os" "github.com/spf13/cobra" "github.com/lxc/incus/v6/internal/version" + "github.com/lxc/incus/v6/shared/logger" ) type cmdGlobal struct { - flagHelp bool - flagVersion bool + flagHelp bool + flagVersion bool + flagLogVerbose bool + flagLogDebug bool } -func main() { +// PreRun runs immediately prior to the main Run function. +func (c *cmdGlobal) PreRun(cmd *cobra.Command, args []string) error { + return logger.InitLogger("", "", c.flagLogVerbose, c.flagLogDebug, nil) +} + +func run() error { // daemon command (main) daemonCmd := cmdDaemon{} app := daemonCmd.Command() @@ -32,14 +41,22 @@ func main() { globalCmd := cmdGlobal{} app.PersistentFlags().BoolVar(&globalCmd.flagVersion, "version", false, "Print version number") app.PersistentFlags().BoolVarP(&globalCmd.flagHelp, "help", "h", false, "Print help") + app.PersistentFlags().BoolVarP(&globalCmd.flagLogVerbose, "verbose", "v", false, "Show all information messages") + app.PersistentFlags().BoolVarP(&globalCmd.flagLogDebug, "debug", "d", false, "Show debug messages") + app.PersistentPreRunE = globalCmd.PreRun // Version handling app.SetVersionTemplate("{{.Version}}\n") app.Version = version.Version // Run the main command and handle errors - err := app.Execute() + return app.Execute() +} + +func main() { + err := run() if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v", err) os.Exit(1) } } diff --git a/cmd/incus-user/main_daemon.go b/cmd/incus-user/main_daemon.go index 55e023842..2bc86b38d 100644 --- a/cmd/incus-user/main_daemon.go +++ b/cmd/incus-user/main_daemon.go @@ -10,12 +10,12 @@ import ( "sync" "time" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" incus "github.com/lxc/incus/v6/client" "github.com/lxc/incus/v6/internal/linux" internalUtil "github.com/lxc/incus/v6/internal/util" + "github.com/lxc/incus/v6/shared/logger" ) var mu sync.RWMutex @@ -43,13 +43,6 @@ func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error { return fmt.Errorf("This must be run as root") } - // Setup logger. - log.SetFormatter(&log.TextFormatter{ - FullTimestamp: true, - }) - log.SetLevel(log.InfoLevel) - log.SetOutput(os.Stdout) - // Create storage. err := os.MkdirAll(internalUtil.VarPath("users"), 0700) if err != nil && !os.IsExist(err) { @@ -57,7 +50,7 @@ func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error { } // Connect. - log.Debug("Connecting to the daemon") + logger.Debug("Connecting to the daemon") client, err := incus.ConnectIncusUnix("", nil) if err != nil { return fmt.Errorf("Unable to connect to the daemon: %w", err) @@ -78,7 +71,7 @@ func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error { } if !ok { - log.Info("Performing initial configuration") + logger.Info("Performing initial configuration") err = serverInitialConfiguration(client) if err != nil { return fmt.Errorf("Failed to apply initial configuration: %w", err) @@ -185,13 +178,13 @@ func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error { } // Start accepting requests. - log.Info("Starting up the server") + logger.Info("Starting up the server") for { // Accept new connection. conn, err := listener.AcceptUnix() if err != nil { - log.Error("Failed to accept new connection: %w", err) + logger.Errorf("Failed to accept new connection: %v", err) continue }