Skip to content

Commit

Permalink
Merge pull request #61 from theckman/add_logging_interface
Browse files Browse the repository at this point in the history
add a Logger interface and setter for the logging Printf calls
  • Loading branch information
samuel committed Jun 5, 2015
2 parents d0e0d8e + 24aa123 commit f575d97
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
29 changes: 22 additions & 7 deletions zk/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"errors"
"fmt"
"io"
"log"
"net"
"strconv"
"strings"
Expand All @@ -23,7 +22,10 @@ import (
"time"
)

var ErrNoServer = errors.New("zk: could not connect to a server")
var (
ErrNoServer = errors.New("zk: could not connect to a server")
DefaultLogger = defaultLogger{}
)

const (
bufferSize = 1536 * 1024
Expand All @@ -47,6 +49,10 @@ type watchPathType struct {

type Dialer func(network, address string, timeout time.Duration) (net.Conn, error)

type Logger interface {
Printf(string, ...interface{})
}

type Conn struct {
lastZxid int64
sessionID int64
Expand Down Expand Up @@ -74,6 +80,8 @@ type Conn struct {

// Debug (used by unit tests)
reconnectDelay time.Duration

logger Logger
}

type request struct {
Expand Down Expand Up @@ -160,6 +168,7 @@ func ConnectWithDialer(servers []string, sessionTimeout time.Duration, dialer Di
watchers: make(map[watchPathType][]chan Event),
passwd: emptyPassword,
timeout: int32(sessionTimeout.Nanoseconds() / 1e6),
logger: DefaultLogger,

// Debug
reconnectDelay: 0,
Expand All @@ -186,6 +195,12 @@ func (c *Conn) State() State {
return State(atomic.LoadInt32((*int32)(&c.state)))
}

// SetLogger sets the logger to be used for printing errors.
// Logger is an interface provided by this package.
func (c *Conn) SetLogger(l Logger) {
c.logger = l
}

func (c *Conn) setState(state State) {
atomic.StoreInt32((*int32)(&c.state), int32(state))
select {
Expand Down Expand Up @@ -221,7 +236,7 @@ func (c *Conn) connect() error {
return nil
}

log.Printf("Failed to connect to %s: %+v", c.servers[c.serverIndex], err)
c.logger.Printf("Failed to connect to %s: %+v", c.servers[c.serverIndex], err)
}
}

Expand Down Expand Up @@ -267,7 +282,7 @@ func (c *Conn) loop() {

// Yeesh
if err != io.EOF && err != ErrSessionExpired && !strings.Contains(err.Error(), "use of closed network connection") {
log.Println(err)
c.logger.Printf(err.Error())
}

select {
Expand Down Expand Up @@ -367,7 +382,7 @@ func (c *Conn) sendSetWatches() {
res := &setWatchesResponse{}
_, err := c.request(opSetWatches, req, res, nil)
if err != nil {
log.Printf("Failed to set previous watches: %s", err.Error())
c.logger.Printf("Failed to set previous watches: %s", err.Error())
}
}()
}
Expand Down Expand Up @@ -582,7 +597,7 @@ func (c *Conn) recvLoop(conn net.Conn) error {
} else if res.Xid == -2 {
// Ping response. Ignore.
} else if res.Xid < 0 {
log.Printf("Xid < 0 (%d) but not ping or watcher event", res.Xid)
c.logger.Printf("Xid < 0 (%d) but not ping or watcher event", res.Xid)
} else {
if res.Zxid > 0 {
c.lastZxid = res.Zxid
Expand All @@ -596,7 +611,7 @@ func (c *Conn) recvLoop(conn net.Conn) error {
c.requestsLock.Unlock()

if !ok {
log.Printf("Response for unknown request with xid %d", res.Xid)
c.logger.Printf("Response for unknown request with xid %d", res.Xid)
} else {
if res.Err != 0 {
err = res.Err.toError()
Expand Down
7 changes: 7 additions & 0 deletions zk/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zk
import (
"encoding/binary"
"errors"
"log"
"reflect"
"runtime"
"time"
Expand All @@ -14,6 +15,12 @@ var (
ErrShortBuffer = errors.New("zk: buffer too small")
)

type defaultLogger struct{}

func (defaultLogger) Printf(format string, a ...interface{}) {
log.Printf(format, a...)
}

type ACL struct {
Perms int32
Scheme string
Expand Down

0 comments on commit f575d97

Please sign in to comment.