From 06cd1ce44fec7b8d76b1bac04721cd2cc96bab6f Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Thu, 6 Feb 2020 15:50:46 +0100 Subject: [PATCH] Add Server.Logger --- cmd/jounce/main.go | 5 ++++- server.go | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cmd/jounce/main.go b/cmd/jounce/main.go index a957b59..e5a055c 100644 --- a/cmd/jounce/main.go +++ b/cmd/jounce/main.go @@ -15,7 +15,10 @@ func main() { log.Fatalf("failed to start listener: %v", err) } - s := jounce.Server{Hostname: "localhost"} + s := jounce.Server{ + Hostname: "localhost", + Logger: log.New(log.Writer(), "", log.LstdFlags), + } log.Printf("Server listening on %v", addr) log.Fatal(s.Serve(ln)) diff --git a/server.go b/server.go index 7e1f6cc..600158a 100644 --- a/server.go +++ b/server.go @@ -9,6 +9,11 @@ import ( "gopkg.in/irc.v3" ) +type Logger interface { + Print(v ...interface{}) + Printf(format string, v ...interface{}) +} + type ircError struct { Message *irc.Message } @@ -169,6 +174,7 @@ func (c *downstreamConn) handleMessageRegistered(msg *irc.Message) error { type Server struct{ Hostname string + Logger Logger } func (s *Server) prefix() *irc.Prefix { @@ -176,6 +182,8 @@ func (s *Server) prefix() *irc.Prefix { } func (s *Server) handleConn(netConn net.Conn) error { + s.Logger.Printf("Handling connection from %v", netConn.RemoteAddr()) + c := downstreamConn{net: netConn, irc: irc.NewConn(netConn), srv: s} defer c.Close() for { @@ -185,7 +193,7 @@ func (s *Server) handleConn(netConn net.Conn) error { } else if err != nil { return fmt.Errorf("failed to read IRC command: %v", err) } - log.Println(msg) + s.Logger.Print(msg) err = c.handleMessage(msg) if ircErr, ok := err.(ircError); ok {