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

View File

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

View File

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

View File

@ -72,6 +72,9 @@ func connectToUpstream(u *user, upstream *Upstream) (*upstreamConn, error) {
go func() { go func() {
for msg := range msgs { for msg := range msgs {
if uc.srv.Debug {
uc.logger.Printf("sent: %v", msg)
}
if err := uc.irc.WriteMessage(msg); err != nil { if err := uc.irc.WriteMessage(msg); err != nil {
uc.logger.Printf("failed to write message: %v", err) 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) 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 { if err := uc.handleMessage(msg); err != nil {
uc.logger.Printf("failed to handle message %q: %v", msg, err) uc.logger.Printf("failed to handle message %q: %v", msg, err)
} }