hid: move off of the log package

We don't spam with useless messages without -debug any longer.
This commit is contained in:
Přemysl Eric Janouch 2018-08-06 19:50:53 +02:00
parent bb0113021a
commit fb648c37be
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 32 additions and 34 deletions

View File

@ -27,7 +27,6 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"log/syslog" "log/syslog"
"net" "net"
"os" "os"
@ -259,9 +258,7 @@ func findTildeHome(username string) string {
} else if v, ok := os.LookupEnv("HOME"); ok { } else if v, ok := os.LookupEnv("HOME"); ok {
return v return v
} }
if debugMode { printDebug("failed to expand the home directory for %s", username)
log.Printf("failed to expand the home directory for %s", username)
}
return "~" + username return "~" + username
} }
@ -396,10 +393,10 @@ func callSimpleConfigWriteDefault(pathHint string, table []simpleConfigItem) {
path, err := simpleConfigWriteDefault( path, err := simpleConfigWriteDefault(
pathHint, strings.Join(prologLines, "\n"), table) pathHint, strings.Join(prologLines, "\n"), table)
if err != nil { if err != nil {
log.Fatalln(err) exitFatal("%s", err)
} }
log.Printf("configuration written to `%s'\n", path) printStatus("configuration written to `%s'", path)
} }
// --- Configuration ----------------------------------------------------------- // --- Configuration -----------------------------------------------------------
@ -891,10 +888,10 @@ var (
// Forcefully tear down all connections. // Forcefully tear down all connections.
func forceQuit(reason string) { func forceQuit(reason string) {
if !quitting { if !quitting {
log.Fatalln("forceQuit called without initiateQuit") exitFatal("forceQuit called without initiateQuit")
} }
log.Printf("forced shutdown (%s)\n", reason) printStatus("forced shutdown (%s)", reason)
for c := range clients { for c := range clients {
// initiateQuit has already unregistered the client. // initiateQuit has already unregistered the client.
c.kill("Shutting down") c.kill("Shutting down")
@ -903,10 +900,10 @@ func forceQuit(reason string) {
// Initiate a clean shutdown of the whole daemon. // Initiate a clean shutdown of the whole daemon.
func initiateQuit() { func initiateQuit() {
log.Println("shutting down") printStatus("shutting down")
for _, ln := range listeners { for _, ln := range listeners {
if err := ln.Close(); err != nil { if err := ln.Close(); err != nil {
log.Println(err) printError("%s", err)
} }
} }
for c := range clients { for c := range clients {
@ -1057,7 +1054,8 @@ func (c *client) kill(reason string) {
c.unregister(reason) c.unregister(reason)
// TODO: Log the address; seems like we always have c.address. // TODO: Log the address; seems like we always have c.address.
log.Println("client destroyed") // 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.
@ -2956,7 +2954,7 @@ func (c *client) onPrepared(host string, isTLS bool) {
c.tls = tls.Server(c.transport, tlsConf) c.tls = tls.Server(c.transport, tlsConf)
c.conn = c.tls c.conn = c.tls
} else { } else {
log.Printf("could not initialize TLS for %s: TLS support disabled\n", printDebug("could not initialize TLS for %s: TLS support disabled",
c.address) c.address)
c.kill("TLS support disabled") c.kill("TLS support disabled")
return return
@ -2990,10 +2988,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)
log.Printf("-> %s\n", line) printDebug("-> %s", line)
if msg := ircParseMessage(line); msg == nil { if msg := ircParseMessage(line); msg == nil {
log.Println("error: invalid line") printDebug("error: invalid line")
} else { } else {
ircProcessMessage(c, msg, line) ircProcessMessage(c, msg, line)
} }
@ -3003,18 +3001,18 @@ func (c *client) onRead(data []byte, readErr error) {
c.reading = false c.reading = false
if readErr != io.EOF { if readErr != io.EOF {
log.Println(readErr) 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.
log.Println("client finished shutdown") printDebug("client finished shutdown")
c.kill("TODO") c.kill("")
} else { } else {
log.Println("client EOF") printDebug("client EOF")
c.closeLink("") c.closeLink("")
} }
} else if len(c.recvQ) > 8192 { } else if len(c.recvQ) > 8192 {
log.Println("client recvQ overrun") 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
@ -3039,7 +3037,7 @@ func (c *client) onWrite(written int, writeErr error) {
c.writing = false c.writing = false
if writeErr != nil { if writeErr != nil {
log.Println(writeErr) 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()
@ -3047,7 +3045,7 @@ func (c *client) onWrite(written int, writeErr error) {
if c.reading { if c.reading {
c.conn.CloseWrite() c.conn.CloseWrite()
} else { } else {
c.kill("TODO") c.kill("")
} }
} }
} }
@ -3066,9 +3064,9 @@ func accept(ln net.Listener) {
return return
} }
if op, ok := err.(net.Error); !ok || !op.Temporary() { if op, ok := err.(net.Error); !ok || !op.Temporary() {
log.Fatalln(err) exitFatal("%s", err)
} else { } else {
log.Println(err) printError("%s", err)
} }
} else { } else {
// TCP_NODELAY is set by default on TCPConns. // TCP_NODELAY is set by default on TCPConns.
@ -3082,7 +3080,7 @@ func prepare(client *client) {
host, _, err := net.SplitHostPort(conn.RemoteAddr().String()) host, _, err := net.SplitHostPort(conn.RemoteAddr().String())
if err != nil { if err != nil {
// In effect, we require TCP/UDP, as they have port numbers. // In effect, we require TCP/UDP, as they have port numbers.
log.Fatalln(err) 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
@ -3091,7 +3089,7 @@ func prepare(client *client) {
go func() { go func() {
defer close(ch) defer close(ch)
if names, err := net.LookupAddr(host); err != nil { if names, err := net.LookupAddr(host); err != nil {
log.Println(err) printError("%s", err)
} else { } else {
ch <- names[0] ch <- names[0]
} }
@ -3111,7 +3109,7 @@ func prepare(client *client) {
isTLS := false isTLS := false
if sysconn, err := conn.(syscall.Conn).SyscallConn(); err != nil { if sysconn, err := conn.(syscall.Conn).SyscallConn(); err != nil {
// This is just for the TLS detection and doesn't need to be fatal. // This is just for the TLS detection and doesn't need to be fatal.
log.Println(err) printError("%s", err)
} else { } else {
isTLS = detectTLS(sysconn) isTLS = detectTLS(sysconn)
} }
@ -3160,18 +3158,18 @@ func processOneEvent() {
case conn := <-conns: case conn := <-conns:
if maxConnections > 0 && len(clients) >= maxConnections { if maxConnections > 0 && len(clients) >= maxConnections {
log.Println("connection limit reached, refusing connection") printDebug("connection limit reached, refusing connection")
conn.Close() conn.Close()
break break
} }
log.Println("accepted client connection") printDebug("accepted client connection")
// In effect, we require TCP/UDP, as they have port numbers. // 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 {
log.Fatalln(err) exitFatal("%s", err)
} }
c := &client{ c := &client{
@ -3192,19 +3190,19 @@ func processOneEvent() {
c.setKillTimer() c.setKillTimer()
case ev := <-prepared: case ev := <-prepared:
log.Println("client is ready:", ev.host) 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:
log.Println("received data from client") 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:
log.Println("sent data to client") 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)
} }
@ -3434,7 +3432,7 @@ func main() {
config = make(simpleConfig) config = make(simpleConfig)
config.loadDefaults(configTable) config.loadDefaults(configTable)
if err := config.updateFromFile(); err != nil && !os.IsNotExist(err) { if err := config.updateFromFile(); err != nil && !os.IsNotExist(err) {
log.Println("error loading configuration", err) printError("error loading configuration: %s", err)
os.Exit(1) os.Exit(1)
} }
@ -3450,7 +3448,7 @@ func main() {
ircSetupListenFDs, ircSetupListenFDs,
} { } {
if err := fn(); err != nil { if err := fn(); err != nil {
log.Fatalln(err) exitFatal("%s", err)
} }
} }