From d829d2ab12b78bda5913858e56f77d0d13feb532 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 6 Dec 2021 18:35:49 +0100 Subject: [PATCH] Add Logger.Debugf Makes it easy to print debugging messages which aren't targeted at the user. See [1] for motivation. [1]: https://dave.cheney.net/2015/11/05/lets-talk-about-logging --- cmd/soju/main.go | 2 +- conn.go | 9 ++------- server.go | 28 ++++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/cmd/soju/main.go b/cmd/soju/main.go index eb3ab68..9219fee 100644 --- a/cmd/soju/main.go +++ b/cmd/soju/main.go @@ -97,7 +97,6 @@ func loadConfig() (*config.Server, *soju.Config, error) { MaxUserNetworks: raw.MaxUserNetworks, MultiUpstream: raw.MultiUpstream, UpstreamUserIPs: raw.UpstreamUserIPs, - Debug: debug, MOTD: motd, } return raw, cfg, nil @@ -140,6 +139,7 @@ func main() { srv := soju.NewServer(db) srv.SetConfig(serverCfg) + srv.Logger = soju.NewLogger(log.Writer(), debug) for _, listen := range cfg.Listen { listenURI := listen diff --git a/conn.go b/conn.go index 1ffa88c..72d7af8 100644 --- a/conn.go +++ b/conn.go @@ -156,9 +156,7 @@ func newConn(srv *Server, ic ircConn, options *connOptions) *conn { break } - if c.srv.Config().Debug { - c.logger.Printf("sent: %v", msg) - } + c.logger.Debugf("sent: %v", msg) c.conn.SetWriteDeadline(time.Now().Add(writeTimeout)) if err := c.conn.WriteMessage(msg); err != nil { c.logger.Printf("failed to write message: %v", err) @@ -210,10 +208,7 @@ func (c *conn) ReadMessage() (*irc.Message, error) { return nil, err } - if c.srv.Config().Debug { - c.logger.Printf("received: %v", msg) - } - + c.logger.Debugf("received: %v", msg) return msg, nil } diff --git a/server.go b/server.go index 520723d..a1c8774 100644 --- a/server.go +++ b/server.go @@ -38,6 +38,26 @@ var backlogLimit = 4000 type Logger interface { Printf(format string, v ...interface{}) + Debugf(format string, v ...interface{}) +} + +type logger struct { + *log.Logger + debug bool +} + +func (l logger) Debugf(format string, v ...interface{}) { + if !l.debug { + return + } + l.Logger.Printf(format, v...) +} + +func NewLogger(out io.Writer, debug bool) Logger { + return logger{ + Logger: log.New(log.Writer(), "", log.LstdFlags), + debug: debug, + } } type prefixLogger struct { @@ -52,6 +72,11 @@ func (l *prefixLogger) Printf(format string, v ...interface{}) { l.logger.Printf("%v"+format, v...) } +func (l *prefixLogger) Debugf(format string, v ...interface{}) { + v = append([]interface{}{l.prefix}, v...) + l.logger.Debugf("%v"+format, v...) +} + type int64Gauge struct { v int64 // atomic } @@ -72,7 +97,6 @@ type Config struct { Hostname string Title string LogPath string - Debug bool HTTPOrigins []string AcceptProxyIPs config.IPSet MaxUserNetworks int @@ -109,7 +133,7 @@ type Server struct { func NewServer(db Database) *Server { srv := &Server{ - Logger: log.New(log.Writer(), "", log.LstdFlags), + Logger: NewLogger(log.Writer(), true), db: db, listeners: make(map[net.Listener]struct{}), users: make(map[string]*user),