Skip to content

incus-user: unify logging, support --verbose and --debug #1606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions cmd/incus-user/main.go
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
}
}
17 changes: 5 additions & 12 deletions cmd/incus-user/main_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,21 +43,14 @@ 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) {
return fmt.Errorf("Couldn't create storage: %w", err)
}

// 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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
Loading