Add verbose flag

This commit is contained in:
Trevor Slocum 2023-12-21 12:52:56 -08:00
parent 6d63fc5db3
commit cb3ead6cd0
4 changed files with 22 additions and 10 deletions

View file

@ -20,6 +20,7 @@ func main() {
mailServer string
passwordSalt string
resetSalt string
verbose bool
debug int
debugCommands bool
rollStatistics bool
@ -29,6 +30,7 @@ func main() {
flag.StringVar(&tz, "tz", "", "Time zone used when calculating statistics")
flag.StringVar(&dataSource, "db", "", "Database data source (postgres://username:password@localhost:5432/database_name")
flag.StringVar(&mailServer, "smtp", "", "SMTP server address")
flag.BoolVar(&verbose, "verbose", false, "Print all client messages")
flag.IntVar(&debug, "debug", 0, "print debug information and serve pprof on specified port")
flag.BoolVar(&debugCommands, "debug-commands", false, "allow players to use restricted commands")
flag.BoolVar(&rollStatistics, "statistics", false, "print dice roll statistics and exit")
@ -60,7 +62,7 @@ func main() {
}()
}
s := server.NewServer(tz, dataSource, mailServer, passwordSalt, resetSalt, false, debugCommands)
s := server.NewServer(tz, dataSource, mailServer, passwordSalt, resetSalt, false, verbose || debug > 0, debugCommands)
if tcpAddress != "" {
s.Listen("tcp", tcpAddress)
}

View file

@ -19,13 +19,15 @@ type socketClient struct {
commands chan<- []byte
terminated bool
wgEvents sync.WaitGroup
verbose bool
}
func newSocketClient(conn net.Conn, commands chan<- []byte, events chan []byte) *socketClient {
func newSocketClient(conn net.Conn, commands chan<- []byte, events chan []byte, verbose bool) *socketClient {
return &socketClient{
conn: conn,
events: events,
commands: commands,
verbose: verbose,
}
}
@ -76,7 +78,9 @@ func (c *socketClient) readCommands() {
copy(buf, scanner.Bytes())
c.commands <- buf
logClientRead(scanner.Bytes())
if c.verbose {
logClientRead(scanner.Bytes())
}
setTimeout()
}
@ -120,7 +124,7 @@ func (c *socketClient) writeEvents(closeWrite chan struct{}) {
continue
}
if !bytes.HasPrefix(event, []byte(`{"Type":"ping"`)) && !bytes.HasPrefix(event, []byte(`{"Type":"list"`)) {
if c.verbose && !bytes.HasPrefix(event, []byte(`{"Type":"ping"`)) && !bytes.HasPrefix(event, []byte(`{"Type":"list"`)) {
log.Printf("-> %s", event)
}
c.wgEvents.Done()

View file

@ -21,9 +21,10 @@ type webSocketClient struct {
commands chan<- []byte
terminated bool
wgEvents sync.WaitGroup
verbose bool
}
func newWebSocketClient(r *http.Request, w http.ResponseWriter, commands chan<- []byte, events chan []byte) *webSocketClient {
func newWebSocketClient(r *http.Request, w http.ResponseWriter, commands chan<- []byte, events chan []byte, verbose bool) *webSocketClient {
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
return nil
@ -33,6 +34,7 @@ func newWebSocketClient(r *http.Request, w http.ResponseWriter, commands chan<-
conn: conn,
events: events,
commands: commands,
verbose: verbose,
}
}
@ -85,7 +87,9 @@ func (c *webSocketClient) readCommands() {
copy(buf, msg)
c.commands <- buf
logClientRead(msg)
if c.verbose {
logClientRead(msg)
}
}
}
@ -127,7 +131,7 @@ func (c *webSocketClient) writeEvents(closeWrite chan struct{}) {
continue
}
if !bytes.HasPrefix(event, []byte(`{"Type":"ping"`)) && !bytes.HasPrefix(event, []byte(`{"Type":"list"`)) {
if c.verbose && !bytes.HasPrefix(event, []byte(`{"Type":"ping"`)) && !bytes.HasPrefix(event, []byte(`{"Type":"list"`)) {
log.Printf("-> %s", event)
}
c.wgEvents.Done()

View file

@ -57,9 +57,10 @@ type server struct {
tz *time.Location
relayChat bool // Chats are not relayed normally. This option is only used by local servers.
verbose bool
}
func NewServer(tz string, dataSource string, mailServer string, passwordSalt string, resetSalt string, relayChat bool, allowDebug bool) *server {
func NewServer(tz string, dataSource string, mailServer string, passwordSalt string, resetSalt string, relayChat bool, verbose bool, allowDebug bool) *server {
const bufferSize = 10
s := &server{
newGameIDs: make(chan int),
@ -70,6 +71,7 @@ func NewServer(tz string, dataSource string, mailServer string, passwordSalt str
passwordSalt: passwordSalt,
resetSalt: resetSalt,
relayChat: relayChat,
verbose: verbose,
}
if tz != "" {
@ -245,7 +247,7 @@ func (s *server) handleWebSocket(w http.ResponseWriter, r *http.Request) {
commands := make(chan []byte, bufferSize)
events := make(chan []byte, bufferSize)
wsClient := newWebSocketClient(r, w, commands, events)
wsClient := newWebSocketClient(r, w, commands, events, s.verbose)
if wsClient == nil {
return
}
@ -411,7 +413,7 @@ func (s *server) handleConnection(conn net.Conn) {
connected: now,
lastActive: now,
commands: commands,
Client: newSocketClient(conn, commands, events),
Client: newSocketClient(conn, commands, events, s.verbose),
}
s.sendHello(c)
s.handleClient(c)