diff options
author | Robin Jarry <robin@jarry.cc> | 2023-06-04 21:52:51 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-06-12 11:23:08 +0200 |
commit | 13e356c4a4a296edea4245fb8850aeba49ae1851 (patch) | |
tree | 4bef7eef06ea554b82c16cace41eb9b9c5109459 /worker/imap/cache.go | |
parent | 68e1809b3c602f2119505df78eea3d1d93854f83 (diff) | |
download | aerc-13e356c4a4a296edea4245fb8850aeba49ae1851.tar.gz |
imap: use named logger
Use the worker's logging functions to have all imap related messages
prefixed by the account name.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Inwit <inwit@sindominio.net>
Diffstat (limited to 'worker/imap/cache.go')
-rw-r--r-- | worker/imap/cache.go | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/worker/imap/cache.go b/worker/imap/cache.go index cf372154..449a0d7e 100644 --- a/worker/imap/cache.go +++ b/worker/imap/cache.go @@ -54,35 +54,35 @@ func (w *IMAPWorker) initCacheDb(acct string) { cd, err := cacheDir() if err != nil { w.cache = nil - log.Errorf("unable to find cache directory: %v", err) + w.worker.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 - log.Errorf("failed opening cache db: %v", err) + w.worker.Errorf("failed opening cache db: %v", err) return } w.cache = db - log.Debugf("cache db opened: %s", p) + w.worker.Debugf("cache db opened: %s", p) tag, err := w.cache.Get(cacheTagKey, nil) clearCache := errors.Is(err, leveldb.ErrNotFound) || !bytes.Equal(tag, cacheTag) switch { case clearCache: - log.Infof("current cache tag is '%s' but found '%s'", + w.worker.Infof("current cache tag is '%s' but found '%s'", cacheTag, tag) - log.Warnf("tag mismatch: clear cache") + w.worker.Warnf("tag mismatch: clear cache") w.clearCache() if err = w.cache.Put(cacheTagKey, cacheTag, nil); err != nil { - log.Errorf("could not set the current cache tag") + w.worker.Errorf("could not set the current cache tag") } case err != nil: - log.Errorf("could not get the cache tag from db") + w.worker.Errorf("could not get the cache tag from db") default: - log.Tracef("cache version match") + w.worker.Tracef("cache version match") if w.config.cacheMaxAge.Hours() > 0 { go w.cleanCache(p) } @@ -92,11 +92,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) - log.Debugf("caching header for message %s.%s", uv, uid) + w.worker.Debugf("caching header for message %s.%s", uv, uid) hdr := bytes.NewBuffer(nil) err := textproto.WriteHeader(hdr, mi.RFC822Headers.Header.Header) if err != nil { - log.Errorf("cannot write header %s.%s: %v", uv, uid, err) + w.worker.Errorf("cannot write header %s.%s: %v", uv, uid, err) return } h := &CachedHeader{ @@ -112,18 +112,18 @@ func (w *IMAPWorker) cacheHeader(mi *models.MessageInfo) { enc := gob.NewEncoder(data) err = enc.Encode(h) if err != nil { - log.Errorf("cannot encode message %s.%s: %v", uv, uid, err) + w.worker.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 { - log.Errorf("cannot write header for message %s.%s: %v", uv, uid, err) + w.worker.Errorf("cannot write header for message %s.%s: %v", uv, uid, err) return } } func (w *IMAPWorker) getCachedHeaders(msg *types.FetchMessageHeaders) []uint32 { - log.Tracef("Retrieving headers from cache: %v", msg.Uids) + w.worker.Tracef("Retrieving headers from cache: %v", msg.Uids) var need []uint32 uv := fmt.Sprintf("%d", w.selected.UidValidity) for _, uid := range msg.Uids { @@ -137,14 +137,14 @@ func (w *IMAPWorker) getCachedHeaders(msg *types.FetchMessageHeaders) []uint32 { dec := gob.NewDecoder(bytes.NewReader(data)) err = dec.Decode(ch) if err != nil { - log.Errorf("cannot decode cached header %s.%s: %v", uv, u, err) + w.worker.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 { - log.Errorf("cannot read cached header %s.%s: %v", uv, u, err) + w.worker.Errorf("cannot read cached header %s.%s: %v", uv, u, err) need = append(need, uid) continue } @@ -159,7 +159,7 @@ func (w *IMAPWorker) getCachedHeaders(msg *types.FetchMessageHeaders) []uint32 { Refs: parse.MsgIDList(hdr, "references"), Size: ch.Size, } - log.Tracef("located cached header %s.%s", uv, u) + w.worker.Tracef("located cached header %s.%s", uv, u) w.worker.PostMessage(&types.MessageInfo{ Message: types.RespondTo(msg), Info: mi, @@ -195,14 +195,16 @@ func (w *IMAPWorker) cleanCache(path string) { dec := gob.NewDecoder(bytes.NewReader(data)) err := dec.Decode(ch) if err != nil { - log.Errorf("cannot clean database %d: %v", w.selected.UidValidity, err) + w.worker.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 { - log.Errorf("cannot clean database %d: %v", w.selected.UidValidity, err) + w.worker.Errorf("cannot clean database %d: %v", + w.selected.UidValidity, err) continue } removed++ @@ -211,7 +213,7 @@ func (w *IMAPWorker) cleanCache(path string) { } iter.Release() elapsed := time.Since(start) - log.Debugf("%s: removed %d/%d expired entries in %s", + w.worker.Debugf("%s: removed %d/%d expired entries in %s", path, removed, scanned, elapsed) } @@ -220,7 +222,7 @@ func (w *IMAPWorker) clearCache() { iter := w.cache.NewIterator(nil, nil) for iter.Next() { if err := w.cache.Delete(iter.Key(), nil); err != nil { - log.Errorf("error clearing cache: %v", err) + w.worker.Errorf("error clearing cache: %v", err) } } iter.Release() |