3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

Make debugging messages easier to turn off.

This commit is contained in:
Jeremy Latt 2013-05-11 14:43:06 -07:00
parent 2ff93d74be
commit 8e5ff51257
6 changed files with 52 additions and 13 deletions

View File

@ -7,6 +7,10 @@ import (
"time" "time"
) )
const (
DEBUG_CLIENT = true
)
type Client struct { type Client struct {
conn net.Conn conn net.Conn
username string username string
@ -44,7 +48,6 @@ func NewClient(server *Server, conn net.Conn) *Client {
func (c *Client) readConn(recv <-chan string) { func (c *Client) readConn(recv <-chan string) {
for str := range recv { for str := range recv {
log.Printf("%s → %s", c.conn.RemoteAddr(), str)
m, err := ParseCommand(str) m, err := ParseCommand(str)
if err != nil { if err != nil {
@ -63,9 +66,10 @@ func (c *Client) readConn(recv <-chan string) {
func (c *Client) writeConn(write chan<- string, replies <-chan Reply) { func (c *Client) writeConn(write chan<- string, replies <-chan Reply) {
for reply := range replies { for reply := range replies {
replyStr := reply.String(c) if DEBUG_CLIENT {
log.Printf("%s ← %s", c.conn.RemoteAddr(), replyStr) log.Printf("%s ← %s", c, reply)
write <- replyStr }
write <- reply.Format(c)
} }
} }

View File

@ -2,10 +2,15 @@ package irc
import ( import (
"bufio" "bufio"
"log"
"net" "net"
"strings" "strings"
) )
const (
DEBUG_NET = false
)
func readTrimmedLine(reader *bufio.Reader) (string, error) { func readTrimmedLine(reader *bufio.Reader) (string, error) {
line, err := reader.ReadString('\n') line, err := reader.ReadString('\n')
if err != nil { if err != nil {
@ -24,6 +29,9 @@ func StringReadChan(conn net.Conn) <-chan string {
if err != nil { if err != nil {
break break
} }
if DEBUG_NET {
log.Printf("%s → %s", conn.RemoteAddr(), line)
}
ch <- line ch <- line
} }
close(ch) close(ch)
@ -36,7 +44,10 @@ func StringWriteChan(conn net.Conn) chan<- string {
writer := bufio.NewWriter(conn) writer := bufio.NewWriter(conn)
go func() { go func() {
for str := range ch { 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 break
} }
writer.Flush() writer.Flush()

View File

@ -5,6 +5,10 @@ import (
"log" "log"
) )
const (
DEBUG_NICKSERV = true
)
type NickServCommand interface { type NickServCommand interface {
HandleNickServ(*NickServ) HandleNickServ(*NickServ)
Client() *Client Client() *Client
@ -23,6 +27,10 @@ func (ns *NickServ) SetBase(base *BaseService) {
ns.BaseService = *base ns.BaseService = *base
} }
func (ns NickServ) Debug() bool {
return DEBUG_NICKSERV
}
var ( var (
parseNickServCommandFuncs = map[string]func([]string) (NickServCommand, error){ parseNickServCommandFuncs = map[string]func([]string) (NickServCommand, error){
"REGISTER": NewRegisterCommand, "REGISTER": NewRegisterCommand,
@ -49,7 +57,9 @@ func (ns *NickServ) HandlePrivMsg(m *PrivMsgCommand) {
} }
cmd.SetClient(m.Client()) 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) cmd.HandleNickServ(ns)
} }

View File

@ -18,7 +18,7 @@ type Replier interface {
} }
type Reply interface { type Reply interface {
String(client *Client) string Format(client *Client) string
Source() Identifier Source() Identifier
} }
@ -31,11 +31,16 @@ type BasicReply struct {
func NewBasicReply(source Identifier, code string, func NewBasicReply(source Identifier, code string,
format string, args ...interface{}) *BasicReply { format string, args ...interface{}) *BasicReply {
message := fmt.Sprintf(format, args...) 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} 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 return reply.message
} }
@ -52,7 +57,7 @@ func NewNumericReply(source Identifier, code string,
return &NumericReply{BasicReply{source, code, fmt.Sprintf(format, args...)}} 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(), return fmt.Sprintf(":%s %s %s %s\r\n", reply.source.Id(), reply.code, client.Nick(),
reply.message) reply.message)
} }

View File

@ -7,6 +7,10 @@ import (
"time" "time"
) )
const (
DEBUG_SERVER = true
)
type ClientNameMap map[string]*Client type ClientNameMap map[string]*Client
type ChannelNameMap map[string]*Channel type ChannelNameMap map[string]*Channel
type UserNameMap map[string]*User type UserNameMap map[string]*User
@ -40,7 +44,9 @@ func NewServer(name string) *Server {
func (server *Server) receiveCommands(commands <-chan Command) { func (server *Server) receiveCommands(commands <-chan Command) {
for command := range commands { 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.Client().atime = time.Now()
command.HandleServer(server) command.HandleServer(server)
} }

View File

@ -14,6 +14,7 @@ type Service interface {
Identifier Identifier
Commands() chan<- ServiceCommand Commands() chan<- ServiceCommand
HandlePrivMsg(*PrivMsgCommand) HandlePrivMsg(*PrivMsgCommand)
Debug() bool
} }
type EditableService interface { type EditableService interface {
@ -36,13 +37,15 @@ func NewService(service EditableService, s *Server, name string) Service {
} }
go receiveCommands(service, commands) go receiveCommands(service, commands)
service.SetBase(base) service.SetBase(base)
s.services[name] = service s.services[service.Nick()] = service
return service return service
} }
func receiveCommands(service Service, commands <-chan ServiceCommand) { func receiveCommands(service Service, commands <-chan ServiceCommand) {
for command := range commands { 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) command.HandleService(service)
} }
} }