diff options
author | Robin Jarry <robin@jarry.cc> | 2022-07-19 22:31:51 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-07-23 22:52:15 +0200 |
commit | cd1999555714fb886493d2d04b6c472be55cebef (patch) | |
tree | 1df3bcf5f687752db671d8bc9c7eab8a5c0fde71 /commands | |
parent | a1f779ccc9b16b22ad6cb2e0bf73c290fd0cc756 (diff) | |
download | aerc-cd1999555714fb886493d2d04b6c472be55cebef.zip |
logging: use level-based logger functions
Do not pass logger objects around anymore. Shuffle some messages to make
them consistent with the new logging API. Avoid using %v when a more
specific verb exists for the argument types.
The loggers are completely disabled (i.e. Sprintf is not even called)
by default. They are only enabled when redirecting stdout to a file.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'commands')
-rw-r--r-- | commands/account/export-mbox.go | 21 | ||||
-rw-r--r-- | commands/account/import-mbox.go | 16 | ||||
-rw-r--r-- | commands/account/search.go | 5 | ||||
-rw-r--r-- | commands/compose/postpone.go | 4 | ||||
-rw-r--r-- | commands/msg/forward.go | 5 | ||||
-rw-r--r-- | commands/msg/recall.go | 5 | ||||
-rw-r--r-- | commands/msg/unsubscribe.go | 5 |
7 files changed, 33 insertions, 28 deletions
diff --git a/commands/account/export-mbox.go b/commands/account/export-mbox.go index 528934e..3e86fc1 100644 --- a/commands/account/export-mbox.go +++ b/commands/account/export-mbox.go @@ -8,6 +8,7 @@ import ( "sync" "time" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/widgets" mboxer "git.sr.ht/~rjarry/aerc/worker/mbox" "git.sr.ht/~rjarry/aerc/worker/types" @@ -54,7 +55,7 @@ func (ExportMbox) Execute(aerc *widgets.Aerc, args []string) error { go func() { file, err := os.Create(filename) if err != nil { - acct.Logger().Println(args[0], err.Error()) + logging.Errorf("failed to create file: %v", err) aerc.PushError(err.Error()) return } @@ -72,28 +73,26 @@ func (ExportMbox) Execute(aerc *widgets.Aerc, args []string) error { for len(uids) > 0 { if retries > 0 { if retries > 10 { - errorMsg := fmt.Sprintln(args[0], "too many retries:", retries, "; stopping export") - acct.Logger().Println(errorMsg) - aerc.PushError(errorMsg) + errorMsg := fmt.Sprintf("too many retries: %d; stopping export", retries) + logging.Errorf(errorMsg) + aerc.PushError(args[0] + " " + errorMsg) break } sleeping := time.Duration(retries * 1e9 * 2) - acct.Logger().Println(args[0], "sleeping for", sleeping, "before retrying; retries:", retries) + logging.Debugf("sleeping for %s before retrying; retries: %d", sleeping, retries) time.Sleep(sleeping) } - acct.Logger().Println(args[0], "fetching", len(uids), "for export") + logging.Debugf("fetching %d for export", len(uids)) acct.Worker().PostAction(&types.FetchFullMessages{ Uids: uids, }, func(msg types.WorkerMessage) { switch msg := msg.(type) { case *types.Done: - acct.Logger().Println(args[0], "done") done <- true case *types.Error: - errMsg := fmt.Sprintln(args[0], "error encountered:", msg.Error.Error()) - acct.Logger().Println(errMsg) - aerc.PushError(errMsg) + logging.Errorf("failed to fetch message: %v", msg.Error) + aerc.PushError(args[0] + " error encountered: " + msg.Error.Error()) done <- false case *types.FullMessage: mu.Lock() @@ -115,7 +114,7 @@ func (ExportMbox) Execute(aerc *widgets.Aerc, args []string) error { } statusInfo := fmt.Sprintf("Exported %d of %d messages to %s.", ctr, len(store.Uids()), filename) aerc.PushStatus(statusInfo, 10*time.Second) - acct.Logger().Println(args[0], statusInfo) + logging.Infof(statusInfo) }() return nil diff --git a/commands/account/import-mbox.go b/commands/account/import-mbox.go index 1430deb..b497787 100644 --- a/commands/account/import-mbox.go +++ b/commands/account/import-mbox.go @@ -11,6 +11,7 @@ import ( "time" "git.sr.ht/~rjarry/aerc/commands" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/widgets" mboxer "git.sr.ht/~rjarry/aerc/worker/mbox" @@ -54,7 +55,7 @@ func (ImportMbox) Execute(aerc *widgets.Aerc, args []string) error { importFolder := func() { statusInfo := fmt.Sprintln("Importing", filename, "to folder", folder) aerc.PushStatus(statusInfo, 10*time.Second) - acct.Logger().Println(args[0], statusInfo) + logging.Infof(statusInfo) f, err := os.Open(filename) if err != nil { aerc.PushError(err.Error()) @@ -77,7 +78,7 @@ func (ImportMbox) Execute(aerc *widgets.Aerc, args []string) error { var buf bytes.Buffer r, err := m.NewReader() if err != nil { - acct.Logger().Println(fmt.Sprintf("%s: could not get reader for uid %d", args[0], m.UID())) + logging.Errorf("could not get reader for uid %d", m.UID()) break } nbytes, _ := io.Copy(&buf, r) @@ -91,11 +92,11 @@ func (ImportMbox) Execute(aerc *widgets.Aerc, args []string) error { switch msg := msg.(type) { case *types.Unsupported: errMsg := fmt.Sprintf("%s: AppendMessage is unsupported", args[0]) - acct.Logger().Println(errMsg) + logging.Errorf(errMsg) aerc.PushError(errMsg) return case *types.Error: - acct.Logger().Println(args[0], msg.Error.Error()) + logging.Errorf("AppendMessage failed: %v", msg.Error) done <- false case *types.Done: atomic.AddUint32(&appended, 1) @@ -111,17 +112,18 @@ func (ImportMbox) Execute(aerc *widgets.Aerc, args []string) error { // error encountered; try to append again after a quick nap retries -= 1 sleeping := time.Duration((5 - retries) * 1e9) - acct.Logger().Println(args[0], "sleeping for", sleeping, "before append message", i, "again") + + logging.Debugf("sleeping for %s before append message %d again", sleeping, i) time.Sleep(sleeping) } case <-time.After(30 * time.Second): - acct.Logger().Println(args[0], "timed-out; appended", appended, "of", len(messages)) + logging.Warnf("timed-out; appended %d of %d", appended, len(messages)) return } } } infoStr := fmt.Sprintf("%s: imported %d of %d sucessfully.", args[0], appended, len(messages)) - acct.Logger().Println(infoStr) + logging.Infof(infoStr) aerc.SetStatus(infoStr) } diff --git a/commands/account/search.go b/commands/account/search.go index 7cae227..290aafc 100644 --- a/commands/account/search.go +++ b/commands/account/search.go @@ -5,6 +5,7 @@ import ( "strings" "git.sr.ht/~rjarry/aerc/lib/statusline" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/widgets" "git.sr.ht/~rjarry/aerc/worker/types" ) @@ -42,7 +43,7 @@ func (SearchFilter) Execute(aerc *widgets.Aerc, args []string) error { cb := func(msg types.WorkerMessage) { if _, ok := msg.(*types.Done); ok { acct.SetStatus(statusline.FilterResult(strings.Join(args, " "))) - acct.Logger().Printf("Filter results: %v", store.Uids()) + logging.Infof("Filter results: %v", store.Uids()) } } store.Sort(nil, cb) @@ -50,7 +51,7 @@ func (SearchFilter) Execute(aerc *widgets.Aerc, args []string) error { acct.SetStatus(statusline.Search("Searching...")) cb := func(uids []uint32) { acct.SetStatus(statusline.Search(strings.Join(args, " "))) - acct.Logger().Printf("Search results: %v", uids) + logging.Infof("Search results: %v", uids) store.ApplySearch(uids) // TODO: Remove when stores have multiple OnUpdate handlers acct.Messages().Invalidate() diff --git a/commands/compose/postpone.go b/commands/compose/postpone.go index 4b1e441..7469b23 100644 --- a/commands/compose/postpone.go +++ b/commands/compose/postpone.go @@ -47,7 +47,7 @@ func (Postpone) Execute(aerc *widgets.Aerc, args []string) error { return errors.New("No Postpone location configured") } - aerc.Logger().Println("Postponing mail") + logging.Infof("Postponing mail") header, err := composer.PrepareHeader() if err != nil { @@ -80,7 +80,7 @@ func (Postpone) Execute(aerc *widgets.Aerc, args []string) error { handleErr := func(err error) { aerc.PushError(err.Error()) - aerc.Logger().Println("Postponing failed:", err) + logging.Errorf("Postponing failed: %v", err) aerc.NewTab(composer, tabName) } diff --git a/commands/msg/forward.go b/commands/msg/forward.go index ad80fea..cd62417 100644 --- a/commands/msg/forward.go +++ b/commands/msg/forward.go @@ -15,6 +15,7 @@ import ( "git.sr.ht/~rjarry/aerc/lib" "git.sr.ht/~rjarry/aerc/lib/format" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/widgets" "git.sr.ht/~rjarry/aerc/worker/types" @@ -73,7 +74,7 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error { if err != nil { return err } - acct.Logger().Println("Forwarding email " + msg.Envelope.MessageId) + logging.Infof("Forwarding email %s", msg.Envelope.MessageId) h := &mail.Header{} subject := "Fwd: " + msg.Envelope.Subject @@ -187,7 +188,7 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error { } bs, err := msg.BodyStructure.PartAtIndex(p) if err != nil { - acct.Logger().Println("forward: PartAtIndex:", err) + logging.Errorf("cannot get PartAtIndex %v: %v", p, err) continue } store.FetchBodyPart(msg.Uid, p, func(reader io.Reader) { diff --git a/commands/msg/recall.go b/commands/msg/recall.go index 3c9f0f2..477a363 100644 --- a/commands/msg/recall.go +++ b/commands/msg/recall.go @@ -13,6 +13,7 @@ import ( "github.com/pkg/errors" "git.sr.ht/~rjarry/aerc/lib" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/widgets" "git.sr.ht/~rjarry/aerc/worker/types" @@ -69,7 +70,7 @@ func (Recall) Execute(aerc *widgets.Aerc, args []string) error { if err != nil { return errors.Wrap(err, "Recall failed") } - acct.Logger().Println("Recalling message " + msgInfo.Envelope.MessageId) + logging.Infof("Recalling message %s", msgInfo.Envelope.MessageId) composer, err := widgets.NewComposer(aerc, acct, aerc.Config(), acct.AccountConfig(), acct.Worker(), "", msgInfo.RFC822Headers, @@ -197,7 +198,7 @@ func (Recall) Execute(aerc *widgets.Aerc, args []string) error { } bs, err := msg.BodyStructure().PartAtIndex(p) if err != nil { - acct.Logger().Println("recall: PartAtIndex:", err) + logging.Infof("cannot get PartAtIndex %v: %v", p, err) continue } msg.FetchBodyPart(p, func(reader io.Reader) { diff --git a/commands/msg/unsubscribe.go b/commands/msg/unsubscribe.go index 126a23d..bec0f5c 100644 --- a/commands/msg/unsubscribe.go +++ b/commands/msg/unsubscribe.go @@ -9,6 +9,7 @@ import ( "time" "git.sr.ht/~rjarry/aerc/lib" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/widgets" "github.com/emersion/go-message/mail" @@ -54,10 +55,10 @@ func (Unsubscribe) Execute(aerc *widgets.Aerc, args []string) error { if len(methods) == 0 { return fmt.Errorf("no methods found to unsubscribe") } - aerc.Logger().Printf("unsubscribe: found %d methods", len(methods)) + logging.Infof("unsubscribe: found %d methods", len(methods)) unsubscribe := func(method *url.URL) { - aerc.Logger().Printf("unsubscribe: trying to unsubscribe using %v", method.Scheme) + logging.Infof("unsubscribe: trying to unsubscribe using %s", method.Scheme) var err error switch strings.ToLower(method.Scheme) { case "mailto": |