cleaned up debug logging

This commit is contained in:
Henry
2015-03-25 21:37:41 +01:00
parent 8bb78fd8f4
commit 6b9621e160
5 changed files with 19 additions and 28 deletions

View File

@ -3,14 +3,17 @@ package goSam
import (
"bufio"
"fmt"
"log"
"net"
"github.com/cryptix/go/debug"
)
var ConnDebug = false
// A Client represents a single Connection to the SAM bridge
type Client struct {
SamConn net.Conn
verbose bool
rd *bufio.Reader
}
// NewDefaultClient creates a new client, connecting to the default host:port at localhost:7656
@ -24,19 +27,16 @@ func NewClient(addr string) (*Client, error) {
if err != nil {
return nil, err
}
if ConnDebug {
conn = debug.WrapConn(conn)
}
c := &Client{
SamConn: conn,
verbose: false,
rd: bufio.NewReader(conn),
}
return c, c.hello()
}
// ToggleVerbose switches logging on or off.
// (also passed to new clients inside Dial.)
func (c *Client) ToggleVerbose() {
c.verbose = !c.verbose
}
// send the initial handshake command and check that the reply is ok
func (c *Client) hello() (err error) {
var r *Reply
@ -63,22 +63,12 @@ func (c *Client) sendCmd(cmd string) (r *Reply, err error) {
return
}
if c.verbose {
log.Printf(">Send>'%s'\n", cmd)
}
reader := bufio.NewReader(c.SamConn)
line, err := reader.ReadString('\n')
line, err := c.rd.ReadString('\n')
if err != nil {
return
}
if c.verbose {
log.Printf("<Rcvd<'%s'\n", line)
}
r, err = parseReply(line)
return
return parseReply(line)
}
// Close the underlying socket to SAM