From cd1999555714fb886493d2d04b6c472be55cebef Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Tue, 19 Jul 2022 22:31:51 +0200 Subject: 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 Acked-by: Moritz Poldrack --- worker/imap/cache.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'worker/imap/cache.go') diff --git a/worker/imap/cache.go b/worker/imap/cache.go index ecbedd89..863b0714 100644 --- a/worker/imap/cache.go +++ b/worker/imap/cache.go @@ -9,6 +9,7 @@ import ( "path" "time" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/types" "github.com/emersion/go-message" @@ -33,18 +34,18 @@ func (w *IMAPWorker) initCacheDb(acct string) { cd, err := cacheDir() if err != nil { w.cache = nil - w.worker.Logger.Panicf("cache: unable to find cache directory: %v", err) + logging.Errorf("unable to find cache directory: %v", err) return } p := path.Join(cd, acct) db, err := leveldb.OpenFile(p, nil) if err != nil { w.cache = nil - w.worker.Logger.Printf("cache: error opening cache db: %v", err) + logging.Errorf("failed opening cache db: %v", err) return } w.cache = db - w.worker.Logger.Printf("cache: cache db opened: %s", p) + logging.Infof("cache db opened: %s", p) if w.config.cacheMaxAge.Hours() > 0 { go w.cleanCache() } @@ -53,11 +54,11 @@ func (w *IMAPWorker) initCacheDb(acct string) { func (w *IMAPWorker) cacheHeader(mi *models.MessageInfo) { uv := fmt.Sprintf("%d", w.selected.UidValidity) uid := fmt.Sprintf("%d", mi.Uid) - w.worker.Logger.Printf("cache: caching header for message %s.%s", uv, uid) + logging.Debugf("caching header for message %s.%s", uv, uid) hdr := bytes.NewBuffer(nil) err := textproto.WriteHeader(hdr, mi.RFC822Headers.Header.Header) if err != nil { - w.worker.Logger.Printf("cache: error writing header %s.%s: %v", uv, uid, err) + logging.Errorf("cannot write header %s.%s: %v", uv, uid, err) return } h := &CachedHeader{ @@ -72,18 +73,18 @@ func (w *IMAPWorker) cacheHeader(mi *models.MessageInfo) { enc := gob.NewEncoder(data) err = enc.Encode(h) if err != nil { - w.worker.Logger.Printf("cache: error encoding message %s.%s: %v", uv, uid, err) + logging.Errorf("cannot encode message %s.%s: %v", uv, uid, err) return } err = w.cache.Put([]byte("header."+uv+"."+uid), data.Bytes(), nil) if err != nil { - w.worker.Logger.Printf("cache: error writing header to database for message %s.%s: %v", uv, uid, err) + logging.Errorf("cannot write header for message %s.%s: %v", uv, uid, err) return } } func (w *IMAPWorker) getCachedHeaders(msg *types.FetchMessageHeaders) []uint32 { - w.worker.Logger.Println("Retrieving headers from cache") + logging.Debugf("Retrieving headers from cache: %v", msg.Uids) var need, found []uint32 uv := fmt.Sprintf("%d", w.selected.UidValidity) for _, uid := range msg.Uids { @@ -97,14 +98,14 @@ func (w *IMAPWorker) getCachedHeaders(msg *types.FetchMessageHeaders) []uint32 { dec := gob.NewDecoder(bytes.NewReader(data)) err = dec.Decode(ch) if err != nil { - w.worker.Logger.Printf("cache: error decoding cached header %s.%s: %v", uv, u, err) + logging.Errorf("cannot decode cached header %s.%s: %v", uv, u, err) need = append(need, uid) continue } hr := bytes.NewReader(ch.Header) textprotoHeader, err := textproto.ReadHeader(bufio.NewReader(hr)) if err != nil { - w.worker.Logger.Printf("cache: error reading cached header %s.%s: %v", uv, u, err) + logging.Errorf("cannot read cached header %s.%s: %v", uv, u, err) need = append(need, uid) continue } @@ -118,7 +119,7 @@ func (w *IMAPWorker) getCachedHeaders(msg *types.FetchMessageHeaders) []uint32 { RFC822Headers: hdr, } found = append(found, uid) - w.worker.Logger.Printf("cache: located cached header %s.%s", uv, u) + logging.Debugf("located cached header %s.%s", uv, u) w.worker.PostMessage(&types.MessageInfo{ Message: types.RespondTo(msg), Info: mi, @@ -154,14 +155,14 @@ func (w *IMAPWorker) cleanCache() { dec := gob.NewDecoder(bytes.NewReader(data)) err := dec.Decode(ch) if err != nil { - w.worker.Logger.Printf("cache: error cleaning database %d: %v", w.selected.UidValidity, err) + logging.Errorf("cannot clean database %d: %v", w.selected.UidValidity, err) continue } exp := ch.Created.Add(w.config.cacheMaxAge) if exp.Before(time.Now()) { err = w.cache.Delete(iter.Key(), nil) if err != nil { - w.worker.Logger.Printf("cache: error cleaning database %d: %v", w.selected.UidValidity, err) + logging.Errorf("cannot clean database %d: %v", w.selected.UidValidity, err) continue } removed = removed + 1 @@ -170,5 +171,5 @@ func (w *IMAPWorker) cleanCache() { } iter.Release() elapsed := time.Since(start) - w.worker.Logger.Printf("cache: cleaned cache, removed %d of %d entries in %f seconds", removed, scanned, elapsed.Seconds()) + logging.Infof("cleaned cache, removed %d of %d entries in %s", removed, scanned, elapsed) } -- cgit