Skip to content

Commit

Permalink
Improve portmaster-start update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Apr 6, 2023
1 parent 08b29ab commit 1d462dd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cmds/portmaster-start/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func initCobra() {

// set up logging
log.SetFlags(log.Ldate | log.Ltime | log.LUTC)
log.SetPrefix("[control] ")
log.SetPrefix("[pmstart] ")
log.SetOutput(os.Stdout)

// not using portbase logger
Expand Down Expand Up @@ -186,7 +186,7 @@ func ensureLoggingDir() error {

func updateRegistryIndex(mustLoadIndex bool) error {
// Set indexes based on the release channel.
warning := helper.SetIndexes(registry, "", false)
warning := helper.SetIndexes(registry, "", false, false, false)
if warning != nil {
log.Printf("WARNING: %s\n", warning)
}
Expand Down
59 changes: 43 additions & 16 deletions cmds/portmaster-start/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package main
import (
"context"
"fmt"
"log"
"os"

"github.com/spf13/cobra"

"github.com/safing/portbase/log"
portlog "github.com/safing/portbase/log"
"github.com/safing/portbase/updater"
"github.com/safing/portmaster/updates/helper"
)

Expand Down Expand Up @@ -58,23 +60,16 @@ func downloadUpdates() error {
helper.IntelOnly()
}

// Set registry state notify callback.
registry.StateNotifyFunc = logProgress

// Set required updates.
registry.MandatoryUpdates = helper.MandatoryUpdates()
registry.AutoUnpack = helper.AutoUnpackUpdates()

// logging is configured as a persistent pre-run method inherited from
// the root command but since we don't use run.Run() we need to start
// logging ourself.
log.SetLogLevel(log.InfoLevel)
err := log.Start()
if err != nil {
fmt.Printf("failed to start logging: %s\n", err)
}
defer log.Shutdown()

if reset {
// Delete storage.
err = os.RemoveAll(registry.StorageDir().Path)
err := os.RemoveAll(registry.StorageDir().Path)
if err != nil {
return fmt.Errorf("failed to reset update dir: %w", err)
}
Expand All @@ -88,11 +83,17 @@ func downloadUpdates() error {
}

// Update all indexes.
err = registry.UpdateIndexes(context.TODO())
err := registry.UpdateIndexes(context.TODO())
if err != nil {
return err
}

// Check if updates are available.
if len(registry.GetState().Updates.PendingDownload) == 0 {
log.Println("all resources are up to date")
return nil
}

// Download all required updates.
err = registry.DownloadUpdates(context.TODO(), false)
if err != nil {
Expand All @@ -116,17 +117,43 @@ func downloadUpdates() error {
return nil
}

func logProgress(state *updater.RegistryState) {
switch state.ID {
case updater.StateChecking:
if state.Updates.LastCheckAt == nil {
log.Println("checking for new versions")
}
case updater.StateDownloading:
if state.Details == nil {
log.Printf("downloading %d updates\n", len(state.Updates.PendingDownload))
} else if downloadDetails, ok := state.Details.(*updater.StateDownloadingDetails); ok {
if downloadDetails.FinishedUpTo < len(downloadDetails.Resources) {
log.Printf(
"[%d/%d] downloading %s",
downloadDetails.FinishedUpTo+1,
len(downloadDetails.Resources),
downloadDetails.Resources[downloadDetails.FinishedUpTo],
)
} else {
if state.Updates.LastDownloadAt == nil {
log.Println("finalizing downloads")
}
}
}
}
}

func purge() error {
log.SetLogLevel(log.TraceLevel)
portlog.SetLogLevel(portlog.TraceLevel)

// logging is configured as a persistent pre-run method inherited from
// the root command but since we don't use run.Run() we need to start
// logging ourself.
err := log.Start()
err := portlog.Start()
if err != nil {
fmt.Printf("failed to start logging: %s\n", err)
}
defer log.Shutdown()
defer portlog.Shutdown()

registry.Purge(3)
return nil
Expand Down

0 comments on commit 1d462dd

Please sign in to comment.