From 8e5ff5125776842b257117a6cb6f3c0b5ab8671c Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Sat, 11 May 2013 14:43:06 -0700 Subject: [PATCH] Make debugging messages easier to turn off. --- src/irc/client.go | 12 ++++++++---- src/irc/net.go | 13 ++++++++++++- src/irc/nickserv.go | 12 +++++++++++- src/irc/reply.go | 13 +++++++++---- src/irc/server.go | 8 +++++++- src/irc/service.go | 7 +++++-- 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/irc/client.go b/src/irc/client.go index 14650312..7158ce63 100644 --- a/src/irc/client.go +++ b/src/irc/client.go @@ -7,6 +7,10 @@ import ( "time" ) +const ( + DEBUG_CLIENT = true +) + type Client struct { conn net.Conn username string @@ -44,7 +48,6 @@ func NewClient(server *Server, conn net.Conn) *Client { func (c *Client) readConn(recv <-chan string) { for str := range recv { - log.Printf("%s → %s", c.conn.RemoteAddr(), str) m, err := ParseCommand(str) if err != nil { @@ -63,9 +66,10 @@ func (c *Client) readConn(recv <-chan string) { func (c *Client) writeConn(write chan<- string, replies <-chan Reply) { for reply := range replies { - replyStr := reply.String(c) - log.Printf("%s ← %s", c.conn.RemoteAddr(), replyStr) - write <- replyStr + if DEBUG_CLIENT { + log.Printf("%s ← %s", c, reply) + } + write <- reply.Format(c) } } diff --git a/src/irc/net.go b/src/irc/net.go index 65e3f2a9..ce371c55 100644 --- a/src/irc/net.go +++ b/src/irc/net.go @@ -2,10 +2,15 @@ package irc import ( "bufio" + "log" "net" "strings" ) +const ( + DEBUG_NET = false +) + func readTrimmedLine(reader *bufio.Reader) (string, error) { line, err := reader.ReadString('\n') if err != nil { @@ -24,6 +29,9 @@ func StringReadChan(conn net.Conn) <-chan string { if err != nil { break } + if DEBUG_NET { + log.Printf("%s → %s", conn.RemoteAddr(), line) + } ch <- line } close(ch) @@ -36,7 +44,10 @@ func StringWriteChan(conn net.Conn) chan<- string { writer := bufio.NewWriter(conn) go func() { for str := range ch { - if _, err := writer.WriteString(str); err != nil { + if DEBUG_NET { + log.Printf("%s ← %s", conn.RemoteAddr(), str) + } + if _, err := writer.WriteString(str + "\r\n"); err != nil { break } writer.Flush() diff --git a/src/irc/nickserv.go b/src/irc/nickserv.go index eb9007c9..7f0b4d19 100644 --- a/src/irc/nickserv.go +++ b/src/irc/nickserv.go @@ -5,6 +5,10 @@ import ( "log" ) +const ( + DEBUG_NICKSERV = true +) + type NickServCommand interface { HandleNickServ(*NickServ) Client() *Client @@ -23,6 +27,10 @@ func (ns *NickServ) SetBase(base *BaseService) { ns.BaseService = *base } +func (ns NickServ) Debug() bool { + return DEBUG_NICKSERV +} + var ( parseNickServCommandFuncs = map[string]func([]string) (NickServCommand, error){ "REGISTER": NewRegisterCommand, @@ -49,7 +57,9 @@ func (ns *NickServ) HandlePrivMsg(m *PrivMsgCommand) { } cmd.SetClient(m.Client()) - log.Printf("%s ← %s", ns, cmd) + if ns.Debug() { + log.Printf("%s ← %s %s", ns, cmd.Client(), cmd) + } cmd.HandleNickServ(ns) } diff --git a/src/irc/reply.go b/src/irc/reply.go index ec5c42b9..d670dbd3 100644 --- a/src/irc/reply.go +++ b/src/irc/reply.go @@ -18,7 +18,7 @@ type Replier interface { } type Reply interface { - String(client *Client) string + Format(client *Client) string Source() Identifier } @@ -31,11 +31,16 @@ type BasicReply struct { func NewBasicReply(source Identifier, code string, format string, args ...interface{}) *BasicReply { message := fmt.Sprintf(format, args...) - fullMessage := fmt.Sprintf(":%s %s %s\r\n", source.Id(), code, message) + fullMessage := fmt.Sprintf(":%s %s %s", source.Id(), code, message) return &BasicReply{source, code, fullMessage} } -func (reply BasicReply) String(client *Client) string { +func (reply BasicReply) String() string { + return fmt.Sprintf("Reply(source=%s, code=%s, message=%s)", + reply.source, reply.code, reply.message) +} + +func (reply BasicReply) Format(client *Client) string { return reply.message } @@ -52,7 +57,7 @@ func NewNumericReply(source Identifier, code string, return &NumericReply{BasicReply{source, code, fmt.Sprintf(format, args...)}} } -func (reply NumericReply) String(client *Client) string { +func (reply NumericReply) Format(client *Client) string { return fmt.Sprintf(":%s %s %s %s\r\n", reply.source.Id(), reply.code, client.Nick(), reply.message) } diff --git a/src/irc/server.go b/src/irc/server.go index 2cedab3b..de8df906 100644 --- a/src/irc/server.go +++ b/src/irc/server.go @@ -7,6 +7,10 @@ import ( "time" ) +const ( + DEBUG_SERVER = true +) + type ClientNameMap map[string]*Client type ChannelNameMap map[string]*Channel type UserNameMap map[string]*User @@ -40,7 +44,9 @@ func NewServer(name string) *Server { func (server *Server) receiveCommands(commands <-chan Command) { for command := range commands { - log.Printf("%s ← %s %s", server, command.Client(), command) + if DEBUG_SERVER { + log.Printf("%s ← %s %s", server, command.Client(), command) + } command.Client().atime = time.Now() command.HandleServer(server) } diff --git a/src/irc/service.go b/src/irc/service.go index b13a20c9..d12b6e18 100644 --- a/src/irc/service.go +++ b/src/irc/service.go @@ -14,6 +14,7 @@ type Service interface { Identifier Commands() chan<- ServiceCommand HandlePrivMsg(*PrivMsgCommand) + Debug() bool } type EditableService interface { @@ -36,13 +37,15 @@ func NewService(service EditableService, s *Server, name string) Service { } go receiveCommands(service, commands) service.SetBase(base) - s.services[name] = service + s.services[service.Nick()] = service return service } func receiveCommands(service Service, commands <-chan ServiceCommand) { for command := range commands { - log.Printf("%s ← %s %s", service.Id(), command.Client(), command) + if service.Debug() { + log.Printf("%s ← %s %s", service.Id(), command.Client(), command) + } command.HandleService(service) } }