Add Server.Logger

This commit is contained in:
Simon Ser 2020-02-06 15:50:46 +01:00
parent 003ce38bf8
commit 06cd1ce44f
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 13 additions and 2 deletions

View File

@ -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))

View File

@ -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 {