Improve logging

This commit is contained in:
Drew DeVault 2018-01-31 21:18:21 -05:00
parent 1710c90548
commit a21afdaa6b
5 changed files with 68 additions and 20 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.go .go
/aerc /aerc
log

View File

@ -1,13 +1,30 @@
package main package main
import ( import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"time" "time"
"github.com/mattn/go-isatty"
"git.sr.ht/~sircmpwn/aerc2/config" "git.sr.ht/~sircmpwn/aerc2/config"
"git.sr.ht/~sircmpwn/aerc2/ui" "git.sr.ht/~sircmpwn/aerc2/ui"
) )
func main() { func main() {
var logOut io.Writer
var logger *log.Logger
if !isatty.IsTerminal(os.Stdout.Fd()) {
logOut = os.Stdout
} else {
logOut = ioutil.Discard
}
logger = log.New(logOut, "", log.LstdFlags|log.Lshortfile)
logger.Println("Starting up aerc")
conf, err := config.LoadConfig(nil) conf, err := config.LoadConfig(nil)
if err != nil { if err != nil {
panic(err) panic(err)
@ -18,7 +35,11 @@ func main() {
} }
defer _ui.Close() defer _ui.Close()
for _, account := range conf.Accounts { for _, account := range conf.Accounts {
tab, err := ui.NewAccountTab(&account) logger.Printf("Initializing account %s\n", account.Name)
tab, err := ui.NewAccountTab(&account, log.New(
logOut,
fmt.Sprintf("[%s] ", account.Name),
log.LstdFlags|log.Lshortfile))
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -1,26 +1,29 @@
package ui package ui
import ( import (
"fmt" "log"
tb "github.com/nsf/termbox-go" tb "github.com/nsf/termbox-go"
"github.com/davecgh/go-spew/spew"
"git.sr.ht/~sircmpwn/aerc2/config" "git.sr.ht/~sircmpwn/aerc2/config"
"git.sr.ht/~sircmpwn/aerc2/worker" "git.sr.ht/~sircmpwn/aerc2/worker"
"git.sr.ht/~sircmpwn/aerc2/worker/types" "git.sr.ht/~sircmpwn/aerc2/worker/types"
) )
type AccountTab struct { type AccountTab struct {
Config *config.AccountConfig Config *config.AccountConfig
Worker worker.Worker Worker worker.Worker
Parent *UIState Parent *UIState
logger *log.Logger
counter int counter int
log []string
} }
func NewAccountTab(conf *config.AccountConfig) (*AccountTab, error) { func NewAccountTab(conf *config.AccountConfig,
work, err := worker.NewWorker(conf.Source) logger *log.Logger) (*AccountTab, error) {
work, err := worker.NewWorker(conf.Source, logger)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -30,6 +33,7 @@ func NewAccountTab(conf *config.AccountConfig) (*AccountTab, error) {
return &AccountTab{ return &AccountTab{
Config: conf, Config: conf,
Worker: work, Worker: work,
logger: logger,
}, nil }, nil
} }
@ -49,9 +53,6 @@ func (acc *AccountTab) Render(at Geometry) {
} }
TFill(at, cell) TFill(at, cell)
TPrintf(&at, cell, "%s %d\n", acc.Name(), acc.counter) TPrintf(&at, cell, "%s %d\n", acc.Name(), acc.counter)
for _, str := range acc.log {
TPrintf(&at, cell, "%s\n", str)
}
acc.counter++ acc.counter++
if acc.counter%10000 == 0 { if acc.counter%10000 == 0 {
acc.counter = 0 acc.counter = 0
@ -64,5 +65,11 @@ func (acc *AccountTab) GetChannel() chan types.WorkerMessage {
} }
func (acc *AccountTab) HandleMessage(msg types.WorkerMessage) { func (acc *AccountTab) HandleMessage(msg types.WorkerMessage) {
acc.log = append(acc.log, fmt.Sprintf("<- %T", msg)) switch msg.InResponseTo().(type) {
case types.Configure:
// Avoid printing passwords
acc.logger.Printf("<- %T\n", msg)
default:
acc.logger.Printf("<- %s", spew.Sdump(msg))
}
} }

View File

@ -2,13 +2,16 @@ package imap
import ( import (
"fmt" "fmt"
"log"
"net/url" "net/url"
"strings" "strings"
"git.sr.ht/~sircmpwn/aerc2/worker/types" "github.com/davecgh/go-spew/spew"
"github.com/emersion/go-imap" "github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-imap-idle" "github.com/emersion/go-imap-idle"
"github.com/emersion/go-imap/client"
"git.sr.ht/~sircmpwn/aerc2/worker/types"
) )
var errUnsupported = fmt.Errorf("unsupported command") var errUnsupported = fmt.Errorf("unsupported command")
@ -31,13 +34,15 @@ type IMAPWorker struct {
client *imapClient client *imapClient
updates chan client.Update updates chan client.Update
logger *log.Logger
} }
func NewIMAPWorker() *IMAPWorker { func NewIMAPWorker(logger *log.Logger) *IMAPWorker {
return &IMAPWorker{ return &IMAPWorker{
messages: make(chan types.WorkerMessage, 50), messages: make(chan types.WorkerMessage, 50),
actions: make(chan types.WorkerMessage, 50), actions: make(chan types.WorkerMessage, 50),
updates: make(chan client.Update, 50), updates: make(chan client.Update, 50),
logger: logger,
} }
} }
@ -126,11 +131,24 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
return nil return nil
} }
// Logs an action but censors passwords
func (w *IMAPWorker) logAction(msg types.WorkerMessage) {
switch msg := msg.(type) {
case types.Configure:
src := msg.Config.Source
msg.Config.Source = "[obsfucated]"
w.logger.Printf("<= %s", spew.Sdump(msg))
msg.Config.Source = src
default:
w.logger.Printf("<= %s", spew.Sdump(msg))
}
}
func (w *IMAPWorker) Run() { func (w *IMAPWorker) Run() {
for { for {
select { select {
case msg := <-w.actions: case msg := <-w.actions:
fmt.Printf("<= %T\n", msg) w.logAction(msg)
if err := w.handleMessage(msg); err == errUnsupported { if err := w.handleMessage(msg); err == errUnsupported {
w.messages <- types.Unsupported{ w.messages <- types.Unsupported{
Message: types.RespondTo(msg), Message: types.RespondTo(msg),
@ -146,7 +164,7 @@ func (w *IMAPWorker) Run() {
} }
} }
case update := <-w.updates: case update := <-w.updates:
fmt.Printf("<= %T\n", update) w.logger.Printf("[= %s", spew.Sdump(update))
} }
} }
} }

View File

@ -5,6 +5,7 @@ import (
"git.sr.ht/~sircmpwn/aerc2/worker/types" "git.sr.ht/~sircmpwn/aerc2/worker/types"
"fmt" "fmt"
"log"
"net/url" "net/url"
) )
@ -15,7 +16,7 @@ type Worker interface {
} }
// Guesses the appropriate worker type based on the given source string // Guesses the appropriate worker type based on the given source string
func NewWorker(source string) (Worker, error) { func NewWorker(source string, logger *log.Logger) (Worker, error) {
u, err := url.Parse(source) u, err := url.Parse(source)
if err != nil { if err != nil {
return nil, err return nil, err
@ -23,7 +24,7 @@ func NewWorker(source string) (Worker, error) {
switch u.Scheme { switch u.Scheme {
case "imap": case "imap":
case "imaps": case "imaps":
return imap.NewIMAPWorker(), nil return imap.NewIMAPWorker(logger), nil
} }
return nil, fmt.Errorf("Unknown backend %s", u.Scheme) return nil, fmt.Errorf("Unknown backend %s", u.Scheme)
} }