Add new logger type so that it can be shut off.

This commit is contained in:
Andrew Gallant (Ocelot) 2012-05-16 23:57:26 -04:00
parent 424f293671
commit acb84171e5
3 changed files with 100 additions and 17 deletions

View File

@ -33,8 +33,8 @@ func (c *Conn) connect(display string) error {
authName, authData, err := readAuthority(c.host, c.display) authName, authData, err := readAuthority(c.host, c.display)
noauth := false noauth := false
if err != nil { if err != nil {
Logger.Printf("Could not get authority info: %v", err) logger.Printf("Could not get authority info: %v", err)
Logger.Println("Trying connection without authority info...") logger.Println("Trying connection without authority info...")
authName = "" authName = ""
authData = []byte{} authData = []byte{}
noauth = true noauth = true

85
nexgb/log.go Normal file
View File

@ -0,0 +1,85 @@
package xgb
import (
"log"
"os"
)
// Log controls whether XGB emits errors to stderr. By default, it is enabled.
var PrintLog = true
// log is a wrapper around a log.PrintLogger so we can control whether it should
// output anything.
type xgblog struct {
*log.Logger
}
func newLogger() xgblog {
return xgblog{log.New(os.Stderr, "XGB: ", log.Lshortfile)}
}
func (lg xgblog) Print(v ...interface{}) {
if PrintLog {
lg.Logger.Print(v...)
}
}
func (lg xgblog) Printf(format string, v ...interface{}) {
if PrintLog {
lg.Logger.Printf(format, v...)
}
}
func (lg xgblog) Println(v ...interface{}) {
if PrintLog {
lg.Logger.Println(v...)
}
}
func (lg xgblog) Fatal(v ...interface{}) {
if PrintLog {
lg.Logger.Fatal(v...)
} else {
os.Exit(1)
}
}
func (lg xgblog) Fatalf(format string, v ...interface{}) {
if PrintLog {
lg.Logger.Fatalf(format, v...)
} else {
os.Exit(1)
}
}
func (lg xgblog) Fatalln(v ...interface{}) {
if PrintLog {
lg.Logger.Fatalln(v...)
} else {
os.Exit(1)
}
}
func (lg xgblog) Panic(v ...interface{}) {
if PrintLog {
lg.Logger.Panic(v...)
} else {
panic("")
}
}
func (lg xgblog) Panicf(format string, v ...interface{}) {
if PrintLog {
lg.Logger.Panicf(format, v...)
} else {
panic("")
}
}
func (lg xgblog) Panicln(v ...interface{}) {
if PrintLog {
lg.Logger.Panicln(v...)
} else {
panic("")
}
}

View File

@ -3,14 +3,12 @@ package xgb
import ( import (
"errors" "errors"
"io" "io"
"log"
"net" "net"
"os"
"sync" "sync"
) )
var ( var (
Logger = log.New(os.Stderr, "XGB: ", 0) logger = newLogger()
// ExtLock is a lock used whenever new extensions are initialized. // ExtLock is a lock used whenever new extensions are initialized.
// It should not be used. It is exported for use in the extension // It should not be used. It is exported for use in the extension
@ -304,8 +302,8 @@ func (c *Conn) sendRequests() {
// writeBuffer is a convenience function for writing a byte slice to the wire. // writeBuffer is a convenience function for writing a byte slice to the wire.
func (c *Conn) writeBuffer(buf []byte) { func (c *Conn) writeBuffer(buf []byte) {
if _, err := c.conn.Write(buf); err != nil { if _, err := c.conn.Write(buf); err != nil {
Logger.Printf("Write error: %s", err) logger.Printf("Write error: %s", err)
Logger.Fatal("A write error is unrecoverable. Exiting...") logger.Fatal("A write error is unrecoverable. Exiting...")
} }
} }
@ -333,8 +331,8 @@ func (c *Conn) readResponses() {
err, event, seq = nil, nil, 0 err, event, seq = nil, nil, 0
if _, err := io.ReadFull(c.conn, buf); err != nil { if _, err := io.ReadFull(c.conn, buf); err != nil {
Logger.Printf("Read error: %s", err) logger.Printf("Read error: %s", err)
Logger.Fatal("A read error is unrecoverable. Exiting...") logger.Fatal("A read error is unrecoverable. Exiting...")
} }
switch buf[0] { switch buf[0] {
@ -343,7 +341,7 @@ func (c *Conn) readResponses() {
// generated) by looking it up by the error number. // generated) by looking it up by the error number.
newErrFun, ok := NewErrorFuncs[int(buf[1])] newErrFun, ok := NewErrorFuncs[int(buf[1])]
if !ok { if !ok {
Logger.Printf("BUG: Could not find error constructor function "+ logger.Printf("BUG: Could not find error constructor function "+
"for error with number %d.", buf[1]) "for error with number %d.", buf[1])
continue continue
} }
@ -362,8 +360,8 @@ func (c *Conn) readResponses() {
biggerBuf := make([]byte, byteCount) biggerBuf := make([]byte, byteCount)
copy(biggerBuf[:32], buf) copy(biggerBuf[:32], buf)
if _, err := io.ReadFull(c.conn, biggerBuf[32:]); err != nil { if _, err := io.ReadFull(c.conn, biggerBuf[32:]); err != nil {
Logger.Printf("Read error: %s", err) logger.Printf("Read error: %s", err)
Logger.Fatal("A read error is unrecoverable. Exiting...") logger.Fatal("A read error is unrecoverable. Exiting...")
} }
replyBytes = biggerBuf replyBytes = biggerBuf
} else { } else {
@ -380,7 +378,7 @@ func (c *Conn) readResponses() {
evNum := int(buf[0] & 127) evNum := int(buf[0] & 127)
newEventFun, ok := NewEventFuncs[evNum] newEventFun, ok := NewEventFuncs[evNum]
if !ok { if !ok {
Logger.Printf("BUG: Could not find event construct function "+ logger.Printf("BUG: Could not find event construct function "+
"for event with number %d.", evNum) "for event with number %d.", evNum)
continue continue
} }
@ -429,7 +427,7 @@ func (c *Conn) readResponses() {
} }
} else { // this is a reply } else { // this is a reply
if cookie.replyChan == nil { if cookie.replyChan == nil {
Logger.Printf("Reply with sequence id %d does not "+ logger.Printf("Reply with sequence id %d does not "+
"have a cookie with a valid reply channel.", seq) "have a cookie with a valid reply channel.", seq)
continue continue
} else { } else {
@ -442,12 +440,12 @@ func (c *Conn) readResponses() {
switch { switch {
// Checked requests with replies // Checked requests with replies
case cookie.replyChan != nil && cookie.errorChan != nil: case cookie.replyChan != nil && cookie.errorChan != nil:
Logger.Printf("Found cookie with sequence id %d that is "+ logger.Printf("Found cookie with sequence id %d that is "+
"expecting a reply but will never get it. Currently "+ "expecting a reply but will never get it. Currently "+
"on sequence number %d", cookie.Sequence, seq) "on sequence number %d", cookie.Sequence, seq)
// Unchecked requests with replies // Unchecked requests with replies
case cookie.replyChan != nil && cookie.pingChan != nil: case cookie.replyChan != nil && cookie.pingChan != nil:
Logger.Printf("Found cookie with sequence id %d that is "+ logger.Printf("Found cookie with sequence id %d that is "+
"expecting a reply (and not an error) but will never "+ "expecting a reply (and not an error) but will never "+
"get it. Currently on sequence number %d", "get it. Currently on sequence number %d",
cookie.Sequence, seq) cookie.Sequence, seq)
@ -470,7 +468,7 @@ func processEventOrError(everr eventOrError) (Event, Error) {
case Error: case Error:
return nil, ee return nil, ee
default: default:
Logger.Printf("Invalid event/error type: %T", everr) logger.Printf("Invalid event/error type: %T", everr)
return nil, nil return nil, nil
} }
panic("unreachable") panic("unreachable")