hid: clean up/finalize logging

This commit is contained in:
Přemysl Eric Janouch 2018-08-06 20:31:22 +02:00
parent fb648c37be
commit f8bcfe447c
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 26 additions and 27 deletions

View File

@ -952,6 +952,12 @@ func ircNotifyRoommates(c *client, message string) {
// --- Clients (continued) ----------------------------------------------------- // --- Clients (continued) -----------------------------------------------------
func (c *client) printDebug(format string, args ...interface{}) {
if debugMode {
printDebug("(%s) %s", c.address, fmt.Sprintf(format, args...))
}
}
func ircAppendClientModes(m uint, mode []byte) []byte { func ircAppendClientModes(m uint, mode []byte) []byte {
if 0 != m&ircUserModeInvisible { if 0 != m&ircUserModeInvisible {
mode = append(mode, 'i') mode = append(mode, 'i')
@ -998,6 +1004,8 @@ func (c *client) send(line string) {
bytes = bytes[:ircMaxMessageLength] bytes = bytes[:ircMaxMessageLength]
} }
c.printDebug("<- %s", bytes)
// TODO: Kill the connection above some "SendQ" threshold (careful!) // TODO: Kill the connection above some "SendQ" threshold (careful!)
c.sendQ = append(c.sendQ, bytes...) c.sendQ = append(c.sendQ, bytes...)
c.sendQ = append(c.sendQ, "\r\n"...) c.sendQ = append(c.sendQ, "\r\n"...)
@ -1049,13 +1057,12 @@ func (c *client) unregister(reason string) {
// Close the connection and forget about the client. // Close the connection and forget about the client.
func (c *client) kill(reason string) { func (c *client) kill(reason string) {
if reason == "" { if reason == "" {
reason = "Client exited" c.unregister("Client exited")
} else {
c.unregister(reason)
} }
c.unregister(reason)
// TODO: Log the address; seems like we always have c.address. c.printDebug("client destroyed (%s)", reason)
// In fact, do it in most debug logs, could be a method of client.
printDebug("client destroyed")
// Try to send a "close notify" alert if the TLS object is ready, // Try to send a "close notify" alert if the TLS object is ready,
// otherwise just tear down the transport. // otherwise just tear down the transport.
@ -2948,14 +2955,14 @@ func ircProcessMessage(c *client, msg *message, raw string) {
// Handle the results from initializing the client's connection. // Handle the results from initializing the client's connection.
func (c *client) onPrepared(host string, isTLS bool) { func (c *client) onPrepared(host string, isTLS bool) {
c.printDebug("client resolved to %s, TLS %t", host, isTLS)
if !isTLS { if !isTLS {
c.conn = c.transport.(connCloseWriter) c.conn = c.transport.(connCloseWriter)
} else if tlsConf != nil { } else if tlsConf != nil {
c.tls = tls.Server(c.transport, tlsConf) c.tls = tls.Server(c.transport, tlsConf)
c.conn = c.tls c.conn = c.tls
} else { } else {
printDebug("could not initialize TLS for %s: TLS support disabled", c.printDebug("could not initialize TLS: disabled")
c.address)
c.kill("TLS support disabled") c.kill("TLS support disabled")
return return
} }
@ -2988,10 +2995,10 @@ func (c *client) onRead(data []byte, readErr error) {
// XXX: And since it accepts LF, we miscalculate receivedBytes within. // XXX: And since it accepts LF, we miscalculate receivedBytes within.
c.recvQ = c.recvQ[advance:] c.recvQ = c.recvQ[advance:]
line := string(token) line := string(token)
printDebug("-> %s", line) c.printDebug("-> %s", line)
if msg := ircParseMessage(line); msg == nil { if msg := ircParseMessage(line); msg == nil {
printDebug("error: invalid line") c.printDebug("error: invalid line")
} else { } else {
ircProcessMessage(c, msg, line) ircProcessMessage(c, msg, line)
} }
@ -3001,18 +3008,17 @@ func (c *client) onRead(data []byte, readErr error) {
c.reading = false c.reading = false
if readErr != io.EOF { if readErr != io.EOF {
printDebug("%s", readErr) c.printDebug("%s", readErr)
c.kill(readErr.Error()) c.kill(readErr.Error())
} else if c.closing { } else if c.closing {
// Disregarding whether a clean shutdown has happened or not. // Disregarding whether a clean shutdown has happened or not.
printDebug("client finished shutdown") c.printDebug("client finished shutdown")
c.kill("") c.kill("")
} else { } else {
printDebug("client EOF") c.printDebug("client EOF")
c.closeLink("") c.closeLink("")
} }
} else if len(c.recvQ) > 8192 { } else if len(c.recvQ) > 8192 {
printDebug("client recvQ overrun")
c.closeLink("recvQ overrun") c.closeLink("recvQ overrun")
// tls.Conn doesn't have the CloseRead method (and it needs to be able // tls.Conn doesn't have the CloseRead method (and it needs to be able
@ -3037,7 +3043,7 @@ func (c *client) onWrite(written int, writeErr error) {
c.writing = false c.writing = false
if writeErr != nil { if writeErr != nil {
printDebug("%s", writeErr) c.printDebug("%s", writeErr)
c.kill(writeErr.Error()) c.kill(writeErr.Error())
} else if len(c.sendQ) > 0 { } else if len(c.sendQ) > 0 {
c.flushSendQ() c.flushSendQ()
@ -3076,12 +3082,7 @@ func accept(ln net.Listener) {
} }
func prepare(client *client) { func prepare(client *client) {
conn := client.transport conn, host := client.transport, client.hostname
host, _, err := net.SplitHostPort(conn.RemoteAddr().String())
if err != nil {
// In effect, we require TCP/UDP, as they have port numbers.
exitFatal("%s", err)
}
// The Cgo resolver doesn't pthread_cancel getnameinfo threads, so not // The Cgo resolver doesn't pthread_cancel getnameinfo threads, so not
// bothering with pointless contexts. // bothering with pointless contexts.
@ -3163,12 +3164,10 @@ func processOneEvent() {
break break
} }
printDebug("accepted client connection")
// In effect, we require TCP/UDP, as they have port numbers.
address := conn.RemoteAddr().String() address := conn.RemoteAddr().String()
host, port, err := net.SplitHostPort(address) host, port, err := net.SplitHostPort(address)
if err != nil { if err != nil {
// In effect, we require TCP/UDP, as they have port numbers.
exitFatal("%s", err) exitFatal("%s", err)
} }
@ -3183,26 +3182,25 @@ func processOneEvent() {
// TODO: Make this configurable and more fine-grained. // TODO: Make this configurable and more fine-grained.
antiflood: newFloodDetector(10*time.Second, 20), antiflood: newFloodDetector(10*time.Second, 20),
} }
clients[c] = true clients[c] = true
c.printDebug("new client")
go prepare(c) go prepare(c)
// The TLS autodetection in prepare needs to have a timeout. // The TLS autodetection in prepare needs to have a timeout.
c.setKillTimer() c.setKillTimer()
case ev := <-prepared: case ev := <-prepared:
printDebug("client is ready: %s", ev.host)
if _, ok := clients[ev.client]; ok { if _, ok := clients[ev.client]; ok {
ev.client.onPrepared(ev.host, ev.isTLS) ev.client.onPrepared(ev.host, ev.isTLS)
} }
case ev := <-reads: case ev := <-reads:
printDebug("received data from client")
if _, ok := clients[ev.client]; ok { if _, ok := clients[ev.client]; ok {
ev.client.onRead(ev.data, ev.err) ev.client.onRead(ev.data, ev.err)
} }
case ev := <-writes: case ev := <-writes:
printDebug("sent data to client")
if _, ok := clients[ev.client]; ok { if _, ok := clients[ev.client]; ok {
ev.client.onWrite(ev.written, ev.err) ev.client.onWrite(ev.written, ev.err)
} }
@ -3392,6 +3390,7 @@ func ircSetupListenFDs() error {
return err return err
} }
listeners = append(listeners, ln) listeners = append(listeners, ln)
printStatus("listening on %s", address)
} }
if len(listeners) == 0 { if len(listeners) == 0 {
return errors.New("network setup failed: no ports to listen on") return errors.New("network setup failed: no ports to listen on")
@ -3405,7 +3404,7 @@ func ircSetupListenFDs() error {
// --- Main -------------------------------------------------------------------- // --- Main --------------------------------------------------------------------
func main() { func main() {
flag.BoolVar(&debugMode, "debug", false, "run in debug mode") flag.BoolVar(&debugMode, "debug", false, "run in verbose debug mode")
version := flag.Bool("version", false, "show version and exit") version := flag.Bool("version", false, "show version and exit")
writeDefaultCfg := flag.Bool("writedefaultcfg", false, writeDefaultCfg := flag.Bool("writedefaultcfg", false,
"write a default configuration file and exit") "write a default configuration file and exit")