mirror of
https://github.com/ergochat/ergo.git
synced 2024-12-22 18:52:41 +01:00
Make debugging messages easier to turn off.
This commit is contained in:
parent
2ff93d74be
commit
8e5ff51257
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user