Skip to content

Commit

Permalink
Add new logger type so that it can be shut off.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Gallant (Ocelot) authored and Andrew Gallant (Ocelot) committed May 17, 2012
1 parent f4ec34d commit cb3b697
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 17 deletions.
4 changes: 2 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (c *Conn) connect(display string) error {
authName, authData, err := readAuthority(c.host, c.display)
noauth := false
if err != nil {
Logger.Printf("Could not get authority info: %v", err)
Logger.Println("Trying connection without authority info...")
logger.Printf("Could not get authority info: %v", err)
logger.Println("Trying connection without authority info...")
authName = ""
authData = []byte{}
noauth = true
Expand Down
85 changes: 85 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -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("")
}
}
28 changes: 13 additions & 15 deletions xgb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package xgb
import (
"errors"
"io"
"log"
"net"
"os"
"sync"
)

var (
Logger = log.New(os.Stderr, "XGB: ", 0)
logger = newLogger()

// ExtLock is a lock used whenever new extensions are initialized.
// It should not be used. It is exported for use in the extension
Expand Down Expand Up @@ -304,8 +302,8 @@ func (c *Conn) sendRequests() {
// writeBuffer is a convenience function for writing a byte slice to the wire.
func (c *Conn) writeBuffer(buf []byte) {
if _, err := c.conn.Write(buf); err != nil {
Logger.Printf("Write error: %s", err)
Logger.Fatal("A write error is unrecoverable. Exiting...")
logger.Printf("Write error: %s", err)
logger.Fatal("A write error is unrecoverable. Exiting...")
}
}

Expand Down Expand Up @@ -333,8 +331,8 @@ func (c *Conn) readResponses() {
err, event, seq = nil, nil, 0

if _, err := io.ReadFull(c.conn, buf); err != nil {
Logger.Printf("Read error: %s", err)
Logger.Fatal("A read error is unrecoverable. Exiting...")
logger.Printf("Read error: %s", err)
logger.Fatal("A read error is unrecoverable. Exiting...")
}

switch buf[0] {
Expand All @@ -343,7 +341,7 @@ func (c *Conn) readResponses() {
// generated) by looking it up by the error number.
newErrFun, ok := NewErrorFuncs[int(buf[1])]
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])
continue
}
Expand All @@ -362,8 +360,8 @@ func (c *Conn) readResponses() {
biggerBuf := make([]byte, byteCount)
copy(biggerBuf[:32], buf)
if _, err := io.ReadFull(c.conn, biggerBuf[32:]); err != nil {
Logger.Printf("Read error: %s", err)
Logger.Fatal("A read error is unrecoverable. Exiting...")
logger.Printf("Read error: %s", err)
logger.Fatal("A read error is unrecoverable. Exiting...")
}
replyBytes = biggerBuf
} else {
Expand All @@ -380,7 +378,7 @@ func (c *Conn) readResponses() {
evNum := int(buf[0] & 127)
newEventFun, ok := NewEventFuncs[evNum]
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)
continue
}
Expand Down Expand Up @@ -429,7 +427,7 @@ func (c *Conn) readResponses() {
}
} else { // this is a reply
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)
continue
} else {
Expand All @@ -442,12 +440,12 @@ func (c *Conn) readResponses() {
switch {
// Checked requests with replies
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 "+
"on sequence number %d", cookie.Sequence, seq)
// Unchecked requests with replies
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 "+
"get it. Currently on sequence number %d",
cookie.Sequence, seq)
Expand All @@ -470,7 +468,7 @@ func processEventOrError(everr eventOrError) (Event, Error) {
case Error:
return nil, ee
default:
Logger.Printf("Invalid event/error type: %T", everr)
logger.Printf("Invalid event/error type: %T", everr)
return nil, nil
}
panic("unreachable")
Expand Down

0 comments on commit cb3b697

Please sign in to comment.