Add a -debug flag

This commit is contained in:
Simon Ser 2020-02-18 16:31:18 +01:00
parent 9fea758956
commit 286fb4b18c
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 21 additions and 0 deletions

View File

@ -12,8 +12,10 @@ import (
func main() {
var addr, configPath string
var debug bool
flag.StringVar(&addr, "listen", "", "listening address")
flag.StringVar(&configPath, "config", "", "path to configuration file")
flag.BoolVar(&debug, "debug", false, "enable debug logging")
flag.Parse()
var cfg *config.Server
@ -54,6 +56,7 @@ func main() {
srv := jounce.NewServer()
// TODO: load from config/DB
srv.Hostname = cfg.Hostname
srv.Debug = debug
srv.Upstreams = []jounce.Upstream{{
Addr: "chat.freenode.net:6697",
Nick: "jounce",

View File

@ -108,6 +108,10 @@ func (dc *downstreamConn) readMessages() error {
return fmt.Errorf("failed to read IRC command: %v", err)
}
if dc.srv.Debug {
dc.logger.Printf("received: %v", msg)
}
err = dc.handleMessage(msg)
if ircErr, ok := err.(ircError); ok {
ircErr.Message.Prefix = dc.srv.prefix()
@ -130,6 +134,9 @@ func (dc *downstreamConn) writeMessages() error {
var closed bool
select {
case msg := <-dc.messages:
if dc.srv.Debug {
dc.logger.Printf("sent: %v", msg)
}
err = dc.irc.WriteMessage(msg)
case consumer := <-dc.consumers:
for {
@ -137,6 +144,9 @@ func (dc *downstreamConn) writeMessages() error {
if msg == nil {
break
}
if dc.srv.Debug {
dc.logger.Printf("sent: %v", msg)
}
err = dc.irc.WriteMessage(msg)
if err != nil {
break

View File

@ -102,6 +102,7 @@ type Server struct {
Hostname string
Logger Logger
RingCap int
Debug bool
Upstreams []Upstream // TODO: per-user
lock sync.Mutex

View File

@ -72,6 +72,9 @@ func connectToUpstream(u *user, upstream *Upstream) (*upstreamConn, error) {
go func() {
for msg := range msgs {
if uc.srv.Debug {
uc.logger.Printf("sent: %v", msg)
}
if err := uc.irc.WriteMessage(msg); err != nil {
uc.logger.Printf("failed to write message: %v", err)
}
@ -344,6 +347,10 @@ func (uc *upstreamConn) readMessages() error {
return fmt.Errorf("failed to read IRC command: %v", err)
}
if uc.srv.Debug {
uc.logger.Printf("received: %v", msg)
}
if err := uc.handleMessage(msg); err != nil {
uc.logger.Printf("failed to handle message %q: %v", msg, err)
}