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 /worker | |
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 'worker')
-rw-r--r-- | worker/imap/cache.go | 29 | ||||
-rw-r--r-- | worker/imap/checkmail.go | 3 | ||||
-rw-r--r-- | worker/imap/connect.go | 9 | ||||
-rw-r--r-- | worker/imap/fetch.go | 10 | ||||
-rw-r--r-- | worker/imap/flags.go | 10 | ||||
-rw-r--r-- | worker/imap/idler.go | 13 | ||||
-rw-r--r-- | worker/imap/list.go | 4 | ||||
-rw-r--r-- | worker/imap/observer.go | 7 | ||||
-rw-r--r-- | worker/imap/open.go | 13 | ||||
-rw-r--r-- | worker/imap/worker.go | 11 | ||||
-rw-r--r-- | worker/maildir/container.go | 6 | ||||
-rw-r--r-- | worker/maildir/search.go | 5 | ||||
-rw-r--r-- | worker/maildir/worker.go | 79 | ||||
-rw-r--r-- | worker/mbox/worker.go | 29 | ||||
-rw-r--r-- | worker/notmuch/eventhandlers.go | 4 | ||||
-rw-r--r-- | worker/notmuch/lib/database.go | 15 | ||||
-rw-r--r-- | worker/notmuch/worker.go | 54 | ||||
-rw-r--r-- | worker/types/worker.go | 28 | ||||
-rw-r--r-- | worker/worker.go | 7 |
19 files changed, 165 insertions, 171 deletions
diff --git a/worker/imap/cache.go b/worker/imap/cache.go index ecbedd8..863b071 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) } diff --git a/worker/imap/checkmail.go b/worker/imap/checkmail.go index 57af3af..21f2e4b 100644 --- a/worker/imap/checkmail.go +++ b/worker/imap/checkmail.go @@ -1,6 +1,7 @@ package imap import ( + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/types" "github.com/emersion/go-imap" @@ -13,7 +14,7 @@ func (w *IMAPWorker) handleCheckMailMessage(msg *types.CheckMail) { imap.StatusUnseen, } for _, dir := range msg.Directories { - w.worker.Logger.Printf("Getting status of directory %s", dir) + logging.Debugf("Getting status of directory %s", dir) status, err := w.client.Status(dir, items) if err != nil { w.worker.PostMessage(&types.Error{ diff --git a/worker/imap/connect.go b/worker/imap/connect.go index 2581c6d..5f8cd1c 100644 --- a/worker/imap/connect.go +++ b/worker/imap/connect.go @@ -7,6 +7,7 @@ import ( "time" "git.sr.ht/~rjarry/aerc/lib" + "git.sr.ht/~rjarry/aerc/logging" "github.com/emersion/go-imap" "github.com/emersion/go-imap/client" ) @@ -66,7 +67,7 @@ func (w *IMAPWorker) connect() (*client.Client, error) { return nil, fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme) } - c.ErrorLog = w.worker.Logger + c.ErrorLog = logging.ErrorLogger() if w.config.user != nil { username := w.config.user.Username() @@ -162,14 +163,12 @@ func (w *IMAPWorker) setKeepaliveParameters(conn *net.TCPConn) error { // Max number of probes before failure err := lib.SetTcpKeepaliveProbes(fd, w.config.keepalive_probes) if err != nil { - w.worker.Logger.Printf( - "cannot set tcp keepalive probes: %v\n", err) + logging.Errorf("cannot set tcp keepalive probes: %v", err) } // Wait time after an unsuccessful probe err = lib.SetTcpKeepaliveInterval(fd, w.config.keepalive_interval) if err != nil { - w.worker.Logger.Printf( - "cannot set tcp keepalive interval: %v\n", err) + logging.Errorf("cannot set tcp keepalive interval: %v", err) } }) return err diff --git a/worker/imap/fetch.go b/worker/imap/fetch.go index cf27e38..6ef0bac 100644 --- a/worker/imap/fetch.go +++ b/worker/imap/fetch.go @@ -26,7 +26,7 @@ func (imapw *IMAPWorker) handleFetchMessageHeaders( nil) return } - imapw.worker.Logger.Printf("Fetching message headers") + logging.Infof("Fetching message headers: %v", toFetch) section := &imap.BodySectionName{ BodyPartName: imap.BodyPartName{ Specifier: imap.HeaderSpecifier, @@ -47,8 +47,8 @@ func (imapw *IMAPWorker) handleFetchMessageHeaders( reader := _msg.GetBody(section) textprotoHeader, err := textproto.ReadHeader(bufio.NewReader(reader)) if err != nil { - imapw.worker.Logger.Printf( - "message %v: could not read header: %v", _msg.Uid, err) + logging.Errorf( + "message %d: could not read header: %v", _msg.Uid, err) imapw.worker.PostMessage(&types.Error{ Message: types.RespondTo(msg), Error: err, @@ -78,7 +78,7 @@ func (imapw *IMAPWorker) handleFetchMessageHeaders( func (imapw *IMAPWorker) handleFetchMessageBodyPart( msg *types.FetchMessageBodyPart) { - imapw.worker.Logger.Printf("Fetching message part") + logging.Infof("Fetching message %d part: %v", msg.Uid, msg.Part) var partHeaderSection imap.BodySectionName partHeaderSection.Peek = true @@ -148,7 +148,7 @@ func (imapw *IMAPWorker) handleFetchMessageBodyPart( func (imapw *IMAPWorker) handleFetchFullMessages( msg *types.FetchFullMessages) { - imapw.worker.Logger.Printf("Fetching full messages") + logging.Infof("Fetching full messages: %v", msg.Uids) section := &imap.BodySectionName{} items := []imap.FetchItem{ imap.FetchEnvelope, diff --git a/worker/imap/flags.go b/worker/imap/flags.go index 2bded2a..5b2ac41 100644 --- a/worker/imap/flags.go +++ b/worker/imap/flags.go @@ -28,7 +28,7 @@ func (imapw *IMAPWorker) handleDeleteMessages(msg *types.DeleteMessages) { for seqNum := range ch { if uid, found := imapw.seqMap.Pop(seqNum); !found { - imapw.worker.Logger.Printf("handleDeleteMessages unknown seqnum: %v", seqNum) + logging.Errorf("handleDeleteMessages unknown seqnum: %d", seqNum) } else { deleted = append(deleted, uid) } @@ -73,8 +73,8 @@ func (imapw *IMAPWorker) handleAnsweredMessages(msg *types.AnsweredMessages) { }, func(_msg types.WorkerMessage) { switch m := _msg.(type) { case *types.Error: - err := fmt.Errorf("handleAnsweredMessages: %v", m.Error) - imapw.worker.Logger.Printf("could not fetch headers: %s", err) + err := fmt.Errorf("handleAnsweredMessages: %w", m.Error) + logging.Errorf("could not fetch headers: %v", err) emitErr(err) case *types.Done: imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil) @@ -104,8 +104,8 @@ func (imapw *IMAPWorker) handleFlagMessages(msg *types.FlagMessages) { }, func(_msg types.WorkerMessage) { switch m := _msg.(type) { case *types.Error: - err := fmt.Errorf("handleFlagMessages: %v", m.Error) - imapw.worker.Logger.Printf("could not fetch headers: %s", err) + err := fmt.Errorf("handleFlagMessages: %w", m.Error) + logging.Errorf("could not fetch headers: %v", err) emitErr(err) case *types.Done: imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil) diff --git a/worker/imap/idler.go b/worker/imap/idler.go index bac690f..055bab8 100644 --- a/worker/imap/idler.go +++ b/worker/imap/idler.go @@ -84,8 +84,7 @@ func (i *idler) Start() { }) i.idleing = false i.done <- err - i.log("elapsed idle time:", - time.Since(now)) + i.log("elapsed idle time: %v", time.Since(now)) } }() @@ -105,7 +104,7 @@ func (i *idler) Stop() error { if err == nil { i.log("<=(idle)") } else { - i.log("<=(idle) with err:", err) + i.log("<=(idle) with err: %v", err) } reterr = nil case <-time.After(i.config.idle_timeout): @@ -144,7 +143,7 @@ func (i *idler) waitOnIdle() { Message: types.RespondTo(&types.Connect{}), }, nil) } else { - i.log("<=(idle) waited; with err:", err) + i.log("<=(idle) waited; with err: %v", err) } i.setWaiting(false) i.stop = make(chan struct{}) @@ -155,7 +154,7 @@ func (i *idler) waitOnIdle() { }() } -func (i *idler) log(args ...interface{}) { - header := fmt.Sprintf("idler (%p) [idle:%t,wait:%t]", i, i.idleing, i.waiting) - i.worker.Logger.Println(append([]interface{}{header}, args...)...) +func (i *idler) log(format string, v ...interface{}) { + msg := fmt.Sprintf(format, v...) + logging.Debugf("idler (%p) [idle:%t,wait:%t] %s", i, i.idleing, i.waiting, msg) } diff --git a/worker/imap/list.go b/worker/imap/list.go index 026aa9a..51f4d3e 100644 --- a/worker/imap/list.go +++ b/worker/imap/list.go @@ -10,7 +10,7 @@ import ( func (imapw *IMAPWorker) handleListDirectories(msg *types.ListDirectories) { mailboxes := make(chan *imap.MailboxInfo) - imapw.worker.Logger.Println("Listing mailboxes") + logging.Infof("Listing mailboxes") done := make(chan interface{}) go func() { @@ -62,7 +62,7 @@ func (imapw *IMAPWorker) handleSearchDirectory(msg *types.SearchDirectory) { }, nil) } - imapw.worker.Logger.Println("Executing search") + logging.Infof("Executing search") criteria, err := parseSearch(msg.Argv) if err != nil { emitError(err) diff --git a/worker/imap/observer.go b/worker/imap/observer.go index e49744c..448128c 100644 --- a/worker/imap/observer.go +++ b/worker/imap/observer.go @@ -6,6 +6,7 @@ import ( "sync" "time" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/worker/types" "github.com/emersion/go-imap" ) @@ -146,7 +147,7 @@ func (o *observer) emit(errMsg string) { }, nil) } -func (o *observer) log(args ...interface{}) { - header := fmt.Sprintf("observer (%p) [running:%t]", o, o.running) - o.worker.Logger.Println(append([]interface{}{header}, args...)...) +func (o *observer) log(format string, args ...interface{}) { + msg := fmt.Sprintf(format, args...) + logging.Debugf("observer (%p) [running:%t] %s", o, o.running, msg) } diff --git a/worker/imap/open.go b/worker/imap/open.go index b52a3c6..636b936 100644 --- a/worker/imap/open.go +++ b/worker/imap/open.go @@ -5,11 +5,12 @@ import ( sortthread "github.com/emersion/go-imap-sortthread" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/worker/types" ) func (imapw *IMAPWorker) handleOpenDirectory(msg *types.OpenDirectory) { - imapw.worker.Logger.Printf("Opening %s", msg.Directory) + logging.Infof("Opening %s", msg.Directory) sel, err := imapw.client.Select(msg.Directory, false) if err != nil { @@ -26,7 +27,7 @@ func (imapw *IMAPWorker) handleOpenDirectory(msg *types.OpenDirectory) { func (imapw *IMAPWorker) handleFetchDirectoryContents( msg *types.FetchDirectoryContents) { - imapw.worker.Logger.Printf("Fetching UID list") + logging.Infof("Fetching UID list") searchCriteria, err := parseSearch(msg.FilterCriteria) if err != nil { @@ -51,7 +52,7 @@ func (imapw *IMAPWorker) handleFetchDirectoryContents( } else { if err != nil { // Non fatal, but we do want to print to get some debug info - imapw.worker.Logger.Printf("can't check for SORT support: %v", err) + logging.Errorf("can't check for SORT support: %v", err) } uids, err = imapw.client.UidSearch(searchCriteria) } @@ -61,7 +62,7 @@ func (imapw *IMAPWorker) handleFetchDirectoryContents( Error: err, }, nil) } else { - imapw.worker.Logger.Printf("Found %d UIDs", len(uids)) + logging.Infof("Found %d UIDs", len(uids)) imapw.seqMap.Clear() imapw.worker.PostMessage(&types.DirectoryContents{ Message: types.RespondTo(msg), @@ -97,7 +98,7 @@ func translateSortCriterions( func (imapw *IMAPWorker) handleDirectoryThreaded( msg *types.FetchDirectoryThreaded) { - imapw.worker.Logger.Printf("Fetching threaded UID list") + logging.Infof("Fetching threaded UID list") searchCriteria, err := parseSearch(msg.FilterCriteria) if err != nil { @@ -117,7 +118,7 @@ func (imapw *IMAPWorker) handleDirectoryThreaded( } else { aercThreads, count := convertThreads(threads, nil) sort.Sort(types.ByUID(aercThreads)) - imapw.worker.Logger.Printf("Found %d threaded messages", count) + logging.Infof("Found %d threaded messages", count) imapw.seqMap.Clear() imapw.worker.PostMessage(&types.DirectoryThreaded{ Message: types.RespondTo(msg), diff --git a/worker/imap/worker.go b/worker/imap/worker.go index 3ed646d..dee089e 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -12,6 +12,7 @@ import ( "github.com/syndtr/goleveldb/leveldb" "git.sr.ht/~rjarry/aerc/lib" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/handlers" "git.sr.ht/~rjarry/aerc/worker/types" @@ -89,12 +90,12 @@ func (w *IMAPWorker) newClient(c *client.Client) { sort, err := w.client.sort.SupportSort() if err == nil && sort { w.caps.Sort = true - w.worker.Logger.Println("Server Capability found: Sort") + logging.Infof("Server Capability found: Sort") } thread, err := w.client.thread.SupportThread() if err == nil && thread { w.caps.Thread = true - w.worker.Logger.Println("Server Capability found: Thread") + logging.Infof("Server Capability found: Thread") } } @@ -223,7 +224,7 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { } func (w *IMAPWorker) handleImapUpdate(update client.Update) { - w.worker.Logger.Printf("(= %T", update) + logging.Debugf("(= %T", update) switch update := update.(type) { case *client.MailboxUpdate: status := update.Mailbox @@ -246,7 +247,7 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) { msg := update.Message if msg.Uid == 0 { if uid, found := w.seqMap.Get(msg.SeqNum); !found { - w.worker.Logger.Printf("MessageUpdate unknown seqnum: %v", msg.SeqNum) + logging.Errorf("MessageUpdate unknown seqnum: %d", msg.SeqNum) return } else { msg.Uid = uid @@ -263,7 +264,7 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) { }, nil) case *client.ExpungeUpdate: if uid, found := w.seqMap.Pop(update.SeqNum); !found { - w.worker.Logger.Printf("ExpungeUpdate unknown seqnum: %v", update.SeqNum) + logging.Errorf("ExpungeUpdate unknown seqnum: %d", update.SeqNum) } else { w.worker.PostMessage(&types.MessagesDeleted{ Uids: []uint32{uid}, diff --git a/worker/maildir/container.go b/worker/maildir/container.go index fb0b190..83f850a 100644 --- a/worker/maildir/container.go +++ b/worker/maildir/container.go @@ -2,7 +2,6 @@ package maildir import ( "fmt" - "log" "os" "path/filepath" "sort" @@ -17,14 +16,13 @@ import ( // the Maildir spec type Container struct { dir string - log *log.Logger uids *uidstore.Store recentUIDS map[uint32]struct{} // used to set the recent flag maildirpp bool // whether to use Maildir++ directory layout } // NewContainer creates a new container at the specified directory -func NewContainer(dir string, l *log.Logger, maildirpp bool) (*Container, error) { +func NewContainer(dir string, maildirpp bool) (*Container, error) { f, err := os.Open(dir) if err != nil { return nil, err @@ -37,7 +35,7 @@ func NewContainer(dir string, l *log.Logger, maildirpp bool) (*Container, error) if !s.IsDir() { return nil, fmt.Errorf("Given maildir '%s' not a directory", dir) } - return &Container{dir: dir, uids: uidstore.NewStore(), log: l, + return &Container{dir: dir, uids: uidstore.NewStore(), recentUIDS: make(map[uint32]struct{}), maildirpp: maildirpp}, nil } diff --git a/worker/maildir/search.go b/worker/maildir/search.go index 7f97fb1..b8e91df 100644 --- a/worker/maildir/search.go +++ b/worker/maildir/search.go @@ -11,6 +11,7 @@ import ( "git.sr.ht/~sircmpwn/getopt" "git.sr.ht/~rjarry/aerc/lib" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" ) @@ -87,7 +88,7 @@ func getParsedFlag(name string) maildir.Flag { func (w *Worker) search(criteria *searchCriteria) ([]uint32, error) { requiredParts := getRequiredParts(criteria) - w.worker.Logger.Printf("Required parts bitmask for search: %b", requiredParts) + logging.Infof("Required parts bitmask for search: %b", requiredParts) keys, err := w.c.UIDs(*w.selected) if err != nil { @@ -99,7 +100,7 @@ func (w *Worker) search(criteria *searchCriteria) ([]uint32, error) { success, err := w.searchKey(key, criteria, requiredParts) if err != nil { // don't return early so that we can still get some results - w.worker.Logger.Printf("Failed to search key %v: %v", key, err) + logging.Errorf("Failed to search key %d: %v", key, err) } else if success { matchedUids = append(matchedUids, key) } diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go index 222b672..ec34455 100644 --- a/worker/maildir/worker.go +++ b/worker/maildir/worker.go @@ -17,6 +17,7 @@ import ( "github.com/emersion/go-maildir" "github.com/fsnotify/fsnotify" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/handlers" "git.sr.ht/~rjarry/aerc/worker/lib" @@ -109,18 +110,18 @@ func (w *Worker) handleFSEvent(ev fsnotify.Event) { } err := w.c.SyncNewMail(*w.selected) if err != nil { - w.worker.Logger.Printf("could not move new to cur : %v", err) + logging.Errorf("could not move new to cur : %v", err) return } uids, err := w.c.UIDs(*w.selected) if err != nil { - w.worker.Logger.Printf("could not scan UIDs: %v", err) + logging.Errorf("could not scan UIDs: %v", err) return } sortedUids, err := w.sort(uids, w.currentSortCriteria) if err != nil { - w.worker.Logger.Printf("error sorting directory: %v", err) + logging.Errorf("error sorting directory: %v", err) return } w.worker.PostMessage(&types.DirectoryContents{ @@ -201,18 +202,18 @@ func (w *Worker) getDirectoryInfo(name string) *models.DirectoryInfo { for _, v := range files { key, flags, err := splitMaildirFile(v) if err != nil { - w.worker.Logger.Printf("%q: error parsing flags (%q): %v", v, key, err) + logging.Errorf("%q: error parsing flags (%q): %v", v, key, err) continue } keyFlags[key] = flags } } else { - w.worker.Logger.Printf("disabled flags cache: %q: %v", dir, err) + logging.Infof("disabled flags cache: %q: %v", dir, err) } uids, err := w.c.UIDs(dir) if err != nil { - w.worker.Logger.Printf("could not get uids: %v", err) + logging.Errorf("could not get uids: %v", err) return dirInfo } @@ -220,7 +221,7 @@ func (w *Worker) getDirectoryInfo(name string) *models.DirectoryInfo { for _, uid := range uids { message, err := w.c.Message(dir, uid) if err != nil { - w.worker.Logger.Printf("could not get message: %v", err) + logging.Errorf("could not get message: %v", err) continue } var flags []maildir.Flag @@ -228,17 +229,17 @@ func (w *Worker) getDirectoryInfo(name string) *models.DirectoryInfo { ok := false flags, ok = keyFlags[message.key] if !ok { - w.worker.Logger.Printf("message (key=%q uid=%d) not found in map cache", message.key, message.uid) + logging.Debugf("message (key=%q uid=%d) not found in map cache", message.key, message.uid) flags, err = message.Flags() if err != nil { - w.worker.Logger.Printf("could not get flags: %v", err) + logging.Errorf("could not get flags: %v", err) continue } } } else { flags, err = message.Flags() if err != nil { - w.worker.Logger.Printf("could not get flags: %v", err) + logging.Errorf("could not get flags: %v", err) continue } } @@ -303,7 +304,7 @@ func (w *Worker) handleMessage(msg types.WorkerMessage) error { func (w *Worker) handleConfigure(msg *types.Configure) error { u, err := url.Parse(msg.Config.Source) if err != nil { - w.worker.Logger.Printf("error configuring maildir worker: %v", err) + logging.Errorf("error configuring maildir worker: %v", err) return err } dir := u.Path @@ -317,13 +318,13 @@ func (w *Worker) handleConfigure(msg *types.Configure) error { if len(dir) == 0 { return fmt.Errorf("could not resolve maildir from URL '%s'", msg.Config.Source) } - c, err := NewContainer(dir, w.worker.Logger, w.maildirpp) + c, err := NewContainer(dir, w.maildirpp) if err != nil { - w.worker.Logger.Printf("could not configure maildir: %s", dir) + logging.Errorf("could not configure maildir: %s", dir) return err } w.c = c - w.worker.Logger.Printf("configured base maildir: %s", dir) + logging.Infof("configured base maildir: %s", dir) return nil } @@ -340,7 +341,7 @@ func (w *Worker) handleListDirectories(msg *types.ListDirectories) error { } dirs, err := w.c.ListFolders() if err != nil { - w.worker.Logger.Printf("error listing directories: %v", err) + logging.Errorf("failed listing directories: %v", err) return err } for _, name := range dirs { @@ -360,7 +361,7 @@ func (w *Worker) handleListDirectories(msg *types.ListDirectories) error { } func (w *Worker) handleOpenDirectory(msg *types.OpenDirectory) error { - w.worker.Logger.Printf("opening %s", msg.Directory) + logging.Infof("opening %s", msg.Directory) // open the directory dir, err := w.c.OpenDirectory(msg.Directory) @@ -422,13 +423,13 @@ func (w *Worker) handleFetchDirectoryContents( } else { uids, err = w.c.UIDs(*w.selected) if err != nil { - w.worker.Logger.Printf("error scanning uids: %v", err) + logging.Errorf("failed scanning uids: %v", err) return err } } sortedUids, err := w.sort(uids, msg.SortCriteria) if err != nil { - w.worker.Logger.Printf("error sorting directory: %v", err) + logging.Errorf("failed sorting directory: %v", err) return err } w.currentSortCriteria = msg.SortCriteria @@ -447,14 +448,14 @@ func (w *Worker) sort(uids []uint32, criteria []*types.SortCriterion) ([]uint32, for _, uid := range uids { info, err := w.msgInfoFromUid(uid) if err != nil { - w.worker.Logger.Printf("could not get message info: %v", err) + logging.Errorf("could not get message info: %v", err) continue } msgInfos = append(msgInfos, info) } sortedUids, err := lib.Sort(msgInfos, criteria) if err != nil { - w.worker.Logger.Printf("could not sort the messages: %v", err) + logging.Errorf("could not sort the messages: %v", err) return nil, err } return sortedUids, nil @@ -463,7 +464,7 @@ func (w *Worker) sort(uids []uint32, criteria []*types.SortCriterion) ([]uint32, func (w *Worker) handleCreateDirectory(msg *types.CreateDirectory) error { dir := w.c.Dir(msg.Directory) if err := dir.Init(); err != nil { - w.worker.Logger.Printf("could not create directory %s: %v", + logging.Errorf("could not create directory %s: %v", msg.Directory, err) return err } @@ -473,7 +474,7 @@ func (w *Worker) handleCreateDirectory(msg *types.CreateDirectory) error { func (w *Worker) handleRemoveDirectory(msg *types.RemoveDirectory) error { dir := w.c.Dir(msg.Directory) if err := os.RemoveAll(string(dir)); err != nil { - w.worker.Logger.Printf("could not remove directory %s: %v", + logging.Errorf("could not remove directory %s: %v", msg.Directory, err) return err } @@ -485,7 +486,7 @@ func (w *Worker) handleFetchMessageHeaders( for _, uid := range msg.Uids { info, err := w.msgInfoFromUid(uid) if err != nil { - w.worker.Logger.Printf("could not get message info: %v", err) + logging.Errorf("could not get message info: %v", err) w.err(msg, err) continue } @@ -504,12 +505,12 @@ func (w *Worker) handleFetchMessageBodyPart( // get reader m, err := w.c.Message(*w.selected, msg.Uid) if err != nil { - w.worker.Logger.Printf("could not get message %d: %v", msg.Uid, err) + logging.Errorf("could not get message %d: %v", msg.Uid, err) return err } r, err := m.NewBodyPartReader(msg.Part) if err != nil { - w.worker.Logger.Printf( + logging.Errorf( "could not get body part reader for message=%d, parts=%#v: %v", msg.Uid, msg.Part, err) return err @@ -529,12 +530,12 @@ func (w *Worker) handleFetchFullMessages(msg *types.FetchFullMessages) error { for _, uid := range msg.Uids { m, err := w.c.Message(*w.selected, uid) if err != nil { - w.worker.Logger.Printf("could not get message %d: %v", uid, err) + logging.Errorf("could not get message %d: %v", uid, err) return err } r, err := m.NewReader() if err != nil { - w.worker.Logger.Printf("could not get message reader: %v", err) + logging.Errorf("could not get message reader: %v", err) return err } defer r.Close() @@ -565,7 +566,7 @@ func (w *Worker) handleDeleteMessages(msg *types.DeleteMessages) error { }, nil) } if err != nil { - w.worker.Logger.Printf("error removing some messages: %v", err) + logging.Errorf("failed removing messages: %v", err) return err } return nil @@ -575,19 +576,18 @@ func (w *Worker) handleAnsweredMessages(msg *types.AnsweredMessages) error { for _, uid := range msg.Uids { m, err := w.c.Message(*w.selected, uid) if err != nil { - w.worker.Logger.Printf("could not get message: %v", err) + logging.Errorf("could not get message: %v", err) w.err(msg, err) continue } if err := m.MarkReplied(msg.Answered); err != nil { - w.worker.Logger.Printf( - "could not mark message as answered: %v", err) + logging.Errorf("could not mark message as answered: %v", err) w.err(msg, err) continue } info, err := m.MessageInfo() if err != nil { - w.worker.Logger.Printf("could not get message info: %v", err) + logging.Errorf("could not get message info: %v", err) w.err(msg, err) continue } @@ -608,19 +608,19 @@ func (w *Worker) handleFlagMessages(msg *types.FlagMessages) error { for _, uid := range msg.Uids { m, err := w.c.Message(*w.selected, uid) if err != nil { - w.worker.Logger.Printf("could not get message: %v", err) + logging.Errorf("could not get message: %v", err) w.err(msg, err) continue } flag := flagToMaildir[msg.Flag] if err := m.SetOneFlag(flag, msg.Enable); err != nil { - w.worker.Logger.Printf("could change flag %v to %v on message: %v", flag, msg.Enable, err) + logging.Errorf("could change flag %v to %v on message: %v", flag, msg.Enable, err) w.err(msg, err) continue } info, err := m.MessageInfo() if err != nil { - w.worker.Logger.Printf("could not get message info: %v", err) + logging.Errorf("could not get message info: %v", err) w.err(msg, err) continue } @@ -657,13 +657,12 @@ func (w *Worker) handleAppendMessage(msg *types.AppendMessage) error { dest := w.c.Dir(msg.Destination) _, writer, err := dest.Create(translateFlags(msg.Flags)) if err != nil { - w.worker.Logger.Printf("could not create message at %s: %v", - msg.Destination, err) + logging.Errorf("could not create message at %s: %v", msg.Destination, err) return err } defer writer.Close() if _, err := io.Copy(writer, msg.Reader); err != nil { - w.worker.Logger.Printf("could not write message to destination: %v", err) + logging.Errorf("could not write message to destination: %v", err) return err } w.worker.PostMessage(&types.Done{ @@ -676,12 +675,12 @@ func (w *Worker) handleAppendMessage(msg *types.AppendMessage) error { } func (w *Worker) handleSearchDirectory(msg *types.SearchDirectory) error { - w.worker.Logger.Printf("Searching directory %v with args: %v", *w.selected, msg.Argv) + logging.Infof("Searching directory %v with args: %v", *w.selected, msg.Argv) criteria, err := parseSearch(msg.Argv) if err != nil { return err } - w.worker.Logger.Printf("Searching with parsed criteria: %#v", criteria) + logging.Infof("Searching with parsed criteria: %#v", criteria) uids, err := w.search(criteria) if err != nil { return err diff --git a/worker/mbox/worker.go b/worker/mbox/worker.go index c7f105b..4c0a127 100644 --- a/worker/mbox/worker.go +++ b/worker/mbox/worker.go @@ -9,6 +9,7 @@ import ( "path/filepath" "sort" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/handlers" "git.sr.ht/~rjarry/aerc/worker/lib" @@ -68,7 +69,7 @@ func (w *mboxWorker) handleMessage(msg types.WorkerMessage) error { reterr = err break } else { - w.worker.Logger.Printf("mbox: configured with mbox file %s", dir) + logging.Infof("configured with mbox file %s", dir) } case *types.Connect, *types.Reconnect, *types.Disconnect: @@ -104,19 +105,19 @@ func (w *mboxWorker) handleMessage(msg types.WorkerMessage) error { Info: w.data.DirectoryInfo(msg.Directory), }, nil) w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil) - w.worker.Logger.Printf("mbox: %s opened\n", msg.Directory) + logging.Infof("%s opened", msg.Directory) case *types.FetchDirectoryContents: var infos []*models.MessageInfo for _, uid := range w.folder.Uids() { m, err := w.folder.Message(uid) if err != nil { - w.worker.Logger.Println("mbox: could not get message", err) + logging.Errorf("could not get message %v", err) continue } info, err := lib.MessageInfo(m) if err != nil { - w.worker.Logger.Println("mbox: could not get message info", err) + logging.Errorf("could not get message info %v", err) continue } infos = append(infos, info) @@ -174,7 +175,7 @@ func (w *mboxWorker) handleMessage(msg types.WorkerMessage) error { case *types.FetchMessageBodyPart: m, err := w.folder.Message(msg.Uid) if err != nil { - w.worker.Logger.Printf("could not get message %d: %v", msg.Uid, err) + logging.Errorf("could not get message %d: %v", msg.Uid, err) reterr = err break } @@ -193,7 +194,7 @@ func (w *mboxWorker) handleMessage(msg types.WorkerMessage) error { r, err := lib.FetchEntityPartReader(fullMsg, msg.Part) if err != nil { - w.worker.Logger.Printf( + logging.Errorf( "could not get body part reader for message=%d, parts=%#v: %v", msg.Uid, msg.Part, err) reterr = err @@ -212,18 +213,18 @@ func (w *mboxWorker) handleMessage(msg types.WorkerMessage) error { for _, uid := range msg.Uids { m, err := w.folder.Message(uid) if err != nil { - w.worker.Logger.Printf("could not get message for uid %d: %v", uid, err) + logging.Errorf("could not get message for uid %d: %v", uid, err) continue } r, err := m.NewReader() if err != nil { - w.worker.Logger.Printf("could not get message reader: %v", err) + logging.Errorf("could not get message reader: %v", err) continue } defer r.Close() b, err := ioutil.ReadAll(r) if err != nil { - w.worker.Logger.Printf("could not get message reader: %v", err) + logging.Errorf("could not get message reader: %v", err) continue } w.worker.PostMessage(&types.FullMessage{ @@ -258,16 +259,16 @@ func (w *mboxWorker) handleMessage(msg types.WorkerMessage) error { for _, uid := range msg.Uids { m, err := w.folder.Message(uid) if err != nil { - w.worker.Logger.Printf("could not get message: %v", err) + logging.Errorf("could not get message: %v", err) continue } if err := m.(*message).SetFlag(msg.Flag, msg.Enable); err != nil { - w.worker.Logger.Printf("could change flag %v to %v on message: %v", msg.Flag, msg.Enable, err) + logging.Errorf("could change flag %v to %t on message: %v", msg.Flag, msg.Enable, err) continue } info, err := lib.MessageInfo(m) if err != nil { - w.worker.Logger.Printf("could not get message info: %v", err) + logging.Errorf("could not get message info: %v", err) continue } @@ -308,12 +309,12 @@ func (w *mboxWorker) handleMessage(msg types.WorkerMessage) error { reterr = err break } - w.worker.Logger.Printf("Searching with parsed criteria: %#v", criteria) + logging.Infof("Searching with parsed criteria: %#v", criteria) m := make([]lib.RawMessage, 0, len(w.folder.Uids())) for _, uid := range w.folder.Uids() { msg, err := w.folder.Message(uid) if err != nil { - w.worker.Logger.Println("faild to get message for uid:", uid) + logging.Errorf("failed to get message for uid: %d", uid) continue } m = append(m, msg) diff --git a/worker/notmuch/eventhandlers.go b/worker/notmuch/eventhandlers.go index 3daabda..4eec1f6 100644 --- a/worker/notmuch/eventhandlers.go +++ b/worker/notmuch/eventhandlers.go @@ -3,6 +3,8 @@ package notmuch +import "git.sr.ht/~rjarry/aerc/logging" + func (w *worker) handleNotmuchEvent(et eventType) error { switch ev := et.(type) { case *updateDirCounts: @@ -16,7 +18,7 @@ func (w *worker) handleUpdateDirCounts(ev eventType) error { for name, query := range w.nameQueryMap { info, err := w.gatherDirectoryInfo(name, query) if err != nil { - w.w.Logger.Printf("could not gather DirectoryInfo: %v\n", err) + logging.Errorf("could not gather DirectoryInfo: %v", err) continue } w.w.PostMessage(info, nil) diff --git a/worker/notmuch/lib/database.go b/worker/notmuch/lib/database.go index 670130f..801f122 100644 --- a/worker/notmuch/lib/database.go +++ b/worker/notmuch/lib/database.go @@ -5,10 +5,10 @@ package lib import ( "fmt" - "log" "time" "git.sr.ht/~rjarry/aerc/lib/uidstore" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/worker/types" notmuch "github.com/zenhack/go.notmuch" ) @@ -18,18 +18,15 @@ const MAX_DB_AGE time.Duration = 10 * time.Second type DB struct { path string excludedTags []string - logger *log.Logger lastOpenTime time.Time db *notmuch.DB uidStore *uidstore.Store } -func NewDB(path string, excludedTags []string, - logger *log.Logger) *DB { +func NewDB(path string, excludedTags []string) *DB { db := &DB{ path: path, excludedTags: excludedTags, - logger: logger, uidStore: uidstore.NewStore(), } return db @@ -71,11 +68,11 @@ func (db *DB) withConnection(writable bool, cb func(*notmuch.DB) error) error { too_old := time.Now().After(db.lastOpenTime.Add(MAX_DB_AGE)) if db.db == nil || writable || too_old { if cerr := db.close(); cerr != nil { - db.logger.Printf("failed to close the notmuch db: %v", cerr) + logging.Errorf("failed to close the notmuch db: %v", cerr) } err := db.connect(writable) if err != nil { - db.logger.Printf("failed to open the notmuch db: %v", err) + logging.Errorf("failed to open the notmuch db: %v", err) return err } } @@ -83,7 +80,7 @@ func (db *DB) withConnection(writable bool, cb func(*notmuch.DB) error) error { if writable { // we need to close to commit the changes, else we block others if cerr := db.close(); cerr != nil { - db.logger.Printf("failed to close the notmuch db: %v", cerr) + logging.Errorf("failed to close the notmuch db: %v", cerr) } } return err @@ -239,7 +236,7 @@ func (db *DB) msgModify(key string, cb(msg) err = msg.TagsToMaildirFlags() if err != nil { - db.logger.Printf("could not sync maildir flags: %v", err) + logging.Errorf("could not sync maildir flags: %v", err) } return nil }) diff --git a/worker/notmuch/worker.go b/worker/notmuch/worker.go index 27be73d..6941809 100644 --- a/worker/notmuch/worker.go +++ b/worker/notmuch/worker.go @@ -65,18 +65,18 @@ func (w *worker) Run() { w.w.PostMessage(&types.Unsupported{ Message: types.RespondTo(msg), }, nil) - w.w.Logger.Printf("ProcessAction(%T) unsupported: %v", msg, err) + logging.Errorf("ProcessAction(%T) unsupported: %v", msg, err) } else if err != nil { w.w.PostMessage(&types.Error{ Message: types.RespondTo(msg), Error: err, }, nil) - w.w.Logger.Printf("ProcessAction(%T) failure: %v", msg, err) + logging.Errorf("ProcessAction(%T) failure: %v", msg, err) } case nmEvent := <-w.nmEvents: err := w.handleNotmuchEvent(nmEvent) if err != nil { - w.w.Logger.Printf("notmuch event failure: %v", err) + logging.Errorf("notmuch event failure: %v", err) } } } @@ -161,7 +161,7 @@ func (w *worker) handleConfigure(msg *types.Configure) error { u, err := url.Parse(msg.Config.Source) if err != nil { - w.w.Logger.Printf("error configuring notmuch worker: %v", err) + logging.Errorf("error configuring notmuch worker: %v", err) return err } home, err := homedir.Expand(u.Hostname()) @@ -174,7 +174,7 @@ func (w *worker) handleConfigure(msg *types.Configure) error { return fmt.Errorf("could not load query map configuration: %v", err) } excludedTags := w.loadExcludeTags(msg.Config) - w.db = notmuch.NewDB(pathToDB, excludedTags, w.w.Logger) + w.db = notmuch.NewDB(pathToDB, excludedTags) return nil } @@ -260,7 +260,7 @@ func (w *worker) queryFromName(name string) string { } func (w *worker) handleOpenDirectory(msg *types.OpenDirectory) error { - w.w.Logger.Printf("opening %s", msg.Directory) + logging.Infof("opening %s", msg.Directory) // try the friendly name first, if that fails assume it's a query w.query = w.queryFromName(msg.Directory) w.currentQueryName = msg.Directory @@ -301,13 +301,13 @@ func (w *worker) handleFetchMessageHeaders( for _, uid := range msg.Uids { m, err := w.msgFromUid(uid) if err != nil { - w.w.Logger.Printf("could not get message: %v", err) + logging.Errorf("could not get message: %v", err) w.err(msg, err) continue } err = w.emitMessageInfo(m, msg) if err != nil { - w.w.Logger.Printf(err.Error()) + logging.Errorf("could not emit message info: %v", err) w.err(msg, err) continue } @@ -348,12 +348,12 @@ func (w *worker) handleFetchMessageBodyPart( m, err := w.msgFromUid(msg.Uid) if err != nil { - w.w.Logger.Printf("could not get message %d: %v", msg.Uid, err) + logging.Errorf("could not get message %d: %v", msg.Uid, err) return err } r, err := m.NewBodyPartReader(msg.Part) if err != nil { - w.w.Logger.Printf( + logging.Errorf( "could not get body part reader for message=%d, parts=%#v: %v", msg.Uid, msg.Part, err) return err @@ -374,12 +374,12 @@ func (w *worker) handleFetchFullMessages(msg *types.FetchFullMessages) error { for _, uid := range msg.Uids { m, err := w.msgFromUid(uid) if err != nil { - w.w.Logger.Printf("could not get message %d: %v", uid, err) + logging.Errorf("could not get message %d: %v", uid, err) return err } r, err := m.NewReader() if err != nil { - w.w.Logger.Printf("could not get message reader: %v", err) + logging.Errorf("could not get message reader: %v", err) return err } defer r.Close() @@ -403,24 +403,24 @@ func (w *worker) handleAnsweredMessages(msg *types.AnsweredMessages) error { for _, uid := range msg.Uids { m, err := w.msgFromUid(uid) if err != nil { - w.w.Logger.Printf("could not get message: %v", err) + logging.Errorf("could not get message: %v", err) w.err(msg, err) continue } if err := m.MarkAnswered(msg.Answered); err != nil { - w.w.Logger.Printf("could not mark message as answered: %v", err) + logging.Errorf("could not mark message as answered: %v", err) w.err(msg, err) continue } err = w.emitMessageInfo(m, msg) if err != nil { - w.w.Logger.Printf(err.Error()) + logging.Errorf("could not emit message info: %v", err) w.err(msg, err) continue } } if err := w.emitDirectoryInfo(w.currentQueryName); err != nil { - w.w.Logger.Printf(err.Error()) + logging.Errorf("could not emit directory info: %v", err) } w.done(msg) return nil @@ -430,24 +430,24 @@ func (w *worker) handleFlagMessages(msg *types.FlagMessages) error { for _, uid := range msg.Uids { m, err := w.msgFromUid(uid) if err != nil { - w.w.Logger.Printf("could not get message: %v", err) + logging.Errorf("could not get message: %v", err) w.err(msg, err) continue } if err := m.SetFlag(msg.Flag, msg.Enable); err != nil { - w.w.Logger.Printf("could not set flag %v as %v for message: %v", msg.Flag, msg.Enable, err) + logging.Errorf("could not set flag %v as %t for message: %v", msg.Flag, msg.Enable, err) w.err(msg, err) continue } err = w.emitMessageInfo(m, msg) if err != nil { - w.w.Logger.Printf(err.Error()) + logging.Errorf("could not emit message info: %v", err) w.err(msg, err) continue } } if err := w.emitDirectoryInfo(w.currentQueryName); err != nil { - w.w.Logger.Printf(err.Error()) + logging.Errorf("could not emit directory info: %v", err) } w.done(msg) return nil @@ -476,7 +476,7 @@ func (w *worker) handleModifyLabels(msg *types.ModifyLabels) error { for _, uid := range msg.Uids { m, err := w.msgFromUid(uid) if err != nil { - return fmt.Errorf("could not get message from uid %v: %v", uid, err) + return fmt.Errorf("could not get message from uid %d: %v", uid, err) } err = m.ModifyTags(msg.Add, msg.Remove) if err != nil { @@ -496,7 +496,7 @@ func (w *worker) handleModifyLabels(msg *types.ModifyLabels) error { // and update the list of possible tags w.emitLabelList() if err = w.emitDirectoryInfo(w.currentQueryName); err != nil { - w.w.Logger.Printf(err.Error()) + logging.Errorf("could not emit directory info: %v", err) } w.done(msg) return nil @@ -563,7 +563,7 @@ func (w *worker) emitDirectoryContents(parent types.WorkerMessage) error { } sortedUids, err := w.sort(uids, w.currentSortCriteria) if err != nil { - w.w.Logger.Printf("error sorting directory: %v", err) + logging.Errorf("error sorting directory: %v", err) return err } w.w.PostMessage(&types.DirectoryContents{ @@ -607,7 +607,7 @@ func (w *worker) emitMessageInfo(m *Message, func (w *worker) emitLabelList() { tags, err := w.db.ListTags() if err != nil { - w.w.Logger.Printf("could not load tags: %v", err) + logging.Errorf("could not load tags: %v", err) return } w.w.PostMessage(&types.LabelList{Labels: tags}, nil) @@ -622,19 +622,19 @@ func (w *worker) sort(uids []uint32, for _, uid := range uids { m, err := w.msgFromUid(uid) if err != nil { - w.w.Logger.Printf("could not get message: %v", err) + logging.Errorf("could not get message: %v", err) continue } info, err := m.MessageInfo() if err != nil { - w.w.Logger.Printf("could not get message info: %v", err) + logging.Errorf("could not get message info: %v", err) continue } msgInfos = append(msgInfos, info) } sortedUids, err := lib.Sort(msgInfos, criteria) if err != nil { - w.w.Logger.Printf("could not sort the messages: %v", err) + logging.Errorf("could not sort the messages: %v", err) return nil, err } return sortedUids, nil diff --git a/worker/types/worker.go b/worker/types/worker.go index 85c19f1..8f179df 100644 --- a/worker/types/worker.go +++ b/worker/types/worker.go @@ -1,8 +1,9 @@ package types import ( - "log" "sync/atomic" + + "git.sr.ht/~rjarry/aerc/logging" ) var lastId int64 = 1 // access via atomic @@ -15,17 +16,15 @@ type Worker struct { Backend Backend Actions chan WorkerMessage Messages chan WorkerMessage - Logger *log.Logger actionCallbacks map[int64]func(msg WorkerMessage) messageCallbacks map[int64]func(msg WorkerMessage) } -func NewWorker(logger *log.Logger) *Worker { +func NewWorker() *Worker { return &Worker{ Actions: make(chan WorkerMessage, 50), Messages: make(chan WorkerMessage, 50), - Logger: logger, actionCallbacks: make(map[int64]func(msg WorkerMessage)), messageCallbacks: make(map[int64]func(msg WorkerMessage)), } @@ -36,15 +35,14 @@ func (worker *Worker) setId(msg WorkerMessage) { msg.setId(id) } -func (worker *Worker) PostAction(msg WorkerMessage, - cb func(msg WorkerMessage)) { +func (worker *Worker) PostAction(msg WorkerMessage, cb func(msg WorkerMessage)) { worker.setId(msg) if resp := msg.InResponseTo(); resp != nil { - worker.Logger.Printf("(ui)=> %T:%T\n", msg, resp) + logging.Debugf("PostAction %T:%T", msg, resp) } else { - worker.Logger.Printf("(ui)=> %T\n", msg) + logging.Debugf("PostAction %T", msg) } worker.Actions <- msg @@ -59,9 +57,9 @@ func (worker *Worker) PostMessage(msg WorkerMessage, worker.setId(msg) if resp := msg.InResponseTo(); resp != nil { - worker.Logger.Printf("->(ui) %T:%T\n", msg, resp) + logging.Debugf("PostMessage %T:%T", msg, resp) } else { - worker.Logger.Printf("->(ui) %T\n", msg) + logging.Debugf("PostMessage %T", msg) } worker.Messages <- msg @@ -72,10 +70,9 @@ func (worker *Worker) PostMessage(msg WorkerMessage, func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage { if resp := msg.InResponseTo(); resp != nil { - worker.Logger.Printf("(ui)<= %T(%d):%T(%d)\n", - msg, msg.getId(), resp, resp.getId()) + logging.Debugf("ProcessMessage %T(%d):%T(%d)", msg, msg.getId(), resp, resp.getId()) } else { - worker.Logger.Printf("(ui)<= %T(%d)\n", msg, msg.getId()) + logging.Debugf("ProcessMessage %T(%d)", msg, msg.getId()) } if inResponseTo := msg.InResponseTo(); inResponseTo != nil { if f, ok := worker.actionCallbacks[inResponseTo.getId()]; ok { @@ -90,10 +87,9 @@ func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage { func (worker *Worker) ProcessAction(msg WorkerMessage) WorkerMessage { if resp := msg.InResponseTo(); resp != nil { - worker.Logger.Printf("<-(ui) %T(%d):%T(%d)\n", - msg, msg.getId(), resp, resp.getId()) + logging.Debugf("ProcessAction %T(%d):%T(%d)", msg, msg.getId(), resp, resp.getId()) } else { - worker.Logger.Printf("<-(ui) %T(%d)\n", msg, msg.getId()) + logging.Debugf("ProcessAction %T(%d)", msg, msg.getId()) } if inResponseTo := msg.InResponseTo(); inResponseTo != nil { if f, ok := worker.messageCallbacks[inResponseTo.getId()]; ok { diff --git a/worker/worker.go b/worker/worker.go index f65f0e3..2af892c 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -1,8 +1,6 @@ package worker import ( - "fmt" - "log" "net/url" "strings" @@ -11,16 +9,15 @@ import ( ) // Guesses the appropriate worker type based on the given source string -func NewWorker(source string, logger *log.Logger) (*types.Worker, error) { +func NewWorker(source string) (*types.Worker, error) { u, err := url.Parse(source) if err != nil { return nil, err } - worker := types.NewWorker(logger) + worker := types.NewWorker() scheme := u.Scheme if strings.ContainsRune(scheme, '+') { scheme = scheme[:strings.IndexRune(scheme, '+')] - fmt.Println(scheme) } backend, err := handlers.GetHandlerForScheme(scheme, worker) if err != nil { |