From 13f541469198e29f9a16f54aeecf7b55cea629c8 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 11 Feb 2020 18:39:52 +0100 Subject: [PATCH] cmd/koushin: add -debug flag --- cmd/koushin/main.go | 7 ++++++- docs/cli.md | 2 ++ server.go | 3 ++- session.go | 9 ++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/cmd/koushin/main.go b/cmd/koushin/main.go index afdf7f3..392150f 100644 --- a/cmd/koushin/main.go +++ b/cmd/koushin/main.go @@ -23,9 +23,10 @@ func main() { var addr string flag.StringVar(&options.Theme, "theme", "", "default theme") flag.StringVar(&addr, "addr", ":1323", "listening address") + flag.BoolVar(&options.Debug, "debug", false, "enable debug logs") flag.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "usage: koushin [options...] \n") + fmt.Fprintf(flag.CommandLine.Output(), "usage: koushin [options...] \n") flag.PrintDefaults() } @@ -48,6 +49,10 @@ func main() { } e.Use(middleware.Recover()) + if options.Debug { + e.Logger.SetLevel(log.DEBUG) + } + sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGUSR1) go func() { diff --git a/docs/cli.md b/docs/cli.md index 36964e9..c3bae3f 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -33,6 +33,8 @@ The following URL schemes are supported: **-addr**: listening address (default: ":1323") +**-debug**: enable debug logs and print IMAP network activity + **-h**, **--help**: show help message and exit # SIGNALS diff --git a/server.go b/server.go index bccbed8..5fdcc8b 100644 --- a/server.go +++ b/server.go @@ -59,7 +59,7 @@ func newServer(e *echo.Echo, options *Options) (*Server, error) { return nil, err } - s.Sessions = newSessionManager(s.dialIMAP, s.dialSMTP, e.Logger) + s.Sessions = newSessionManager(s.dialIMAP, s.dialSMTP, e.Logger, options.Debug) return s, nil } @@ -283,6 +283,7 @@ func handleUnauthenticated(next echo.HandlerFunc, ctx *Context) error { type Options struct { Upstreams []string Theme string + Debug bool } // New creates a new server. diff --git a/session.go b/session.go index 3aea68e..2da29c2 100644 --- a/session.go +++ b/session.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "net/http" + "os" "sync" "time" @@ -142,17 +143,19 @@ type SessionManager struct { dialIMAP DialIMAPFunc dialSMTP DialSMTPFunc logger echo.Logger + debug bool locker sync.Mutex sessions map[string]*Session // protected by locker } -func newSessionManager(dialIMAP DialIMAPFunc, dialSMTP DialSMTPFunc, logger echo.Logger) *SessionManager { +func newSessionManager(dialIMAP DialIMAPFunc, dialSMTP DialSMTPFunc, logger echo.Logger, debug bool) *SessionManager { return &SessionManager{ sessions: make(map[string]*Session), dialIMAP: dialIMAP, dialSMTP: dialSMTP, logger: logger, + debug: debug, } } @@ -167,6 +170,10 @@ func (sm *SessionManager) connectIMAP(username, password string) (*imapclient.Cl return nil, AuthError{err} } + if sm.debug { + c.SetDebug(os.Stderr) + } + return c, nil }