-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add connection logging to help with debugging
- Loading branch information
1 parent
095ece7
commit 86bf30a
Showing
5 changed files
with
232 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package mssql | ||
|
||
import ( | ||
"encoding/hex" | ||
"net" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type connLogger struct { | ||
conn net.Conn | ||
readKind, writeKind string | ||
readCount, writeCount int | ||
logger Logger | ||
} | ||
|
||
var _ net.Conn = &connLogger{} | ||
|
||
func newConnLogger(conn net.Conn, kind string, logger Logger) net.Conn { | ||
if len(kind) > 0 && !strings.HasPrefix(kind, " ") { | ||
kind = " " + kind | ||
} | ||
|
||
cl := &connLogger{ | ||
conn: conn, | ||
readKind: "R" + kind, | ||
writeKind: "W" + kind, | ||
logger: logger, | ||
} | ||
|
||
return cl | ||
} | ||
|
||
func (cl *connLogger) Read(p []byte) (n int, err error) { | ||
n, err = cl.conn.Read(p) | ||
|
||
if n > 0 { | ||
dump := hex.Dump(p) | ||
cl.logger.Printf("%s %d\n%s", cl.readKind, cl.readCount, dump) | ||
cl.readCount += n | ||
} | ||
|
||
return | ||
} | ||
|
||
func (cl *connLogger) Write(p []byte) (n int, err error) { | ||
n, err = cl.conn.Write(p) | ||
|
||
if n > 0 { | ||
dump := hex.Dump(p) | ||
cl.logger.Printf("%s %d\n%s", cl.writeKind, cl.writeCount, dump) | ||
cl.writeCount += n | ||
} | ||
|
||
return | ||
} | ||
|
||
func (cl *connLogger) Close() (err error) { | ||
return cl.conn.Close() | ||
} | ||
|
||
func (cl *connLogger) LocalAddr() net.Addr { | ||
return cl.conn.LocalAddr() | ||
} | ||
|
||
func (cl *connLogger) RemoteAddr() net.Addr { | ||
return cl.conn.RemoteAddr() | ||
} | ||
|
||
func (cl *connLogger) SetDeadline(t time.Time) error { | ||
return cl.conn.SetDeadline(t) | ||
} | ||
|
||
func (cl *connLogger) SetReadDeadline(t time.Time) error { | ||
return cl.conn.SetReadDeadline(t) | ||
} | ||
|
||
func (cl *connLogger) SetWriteDeadline(t time.Time) error { | ||
return cl.conn.SetWriteDeadline(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package mssql | ||
|
||
import ( | ||
"net" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestConnLoggerOperations(t *testing.T) { | ||
clt := &connLoggerTest{} | ||
cl := newConnLogger(clt, "test", nullLogger{}) | ||
packet := append(make([]byte, 0, 10), 1, 2, 3, 4, 5) | ||
n, err := cl.Read(packet) | ||
if n != 10 || err != nil { | ||
t.Error("Unexpected return value from call to Read()") | ||
} | ||
|
||
n, err = cl.Write(packet) | ||
if n != 5 || err != nil { | ||
t.Error("Unexpected return value from call to Write()") | ||
} | ||
|
||
if cl.Close() != nil { | ||
t.Error("Unexpected return value from call to Close()") | ||
} | ||
|
||
if cl.LocalAddr() == nil { | ||
t.Error("Unexpected return value from call to LocalAddr()") | ||
} | ||
|
||
if cl.RemoteAddr() == nil { | ||
t.Error("Unexpected return value from call to RemoteAddr()") | ||
} | ||
|
||
if cl.SetDeadline(time.Now()) != nil { | ||
t.Error("Unexpected return value from call to SetDeadline()") | ||
} | ||
|
||
if cl.SetReadDeadline(time.Now()) != nil { | ||
t.Error("Unexpected return value from call to SetReadDeadline()") | ||
} | ||
|
||
if cl.SetWriteDeadline(time.Now()) != nil { | ||
t.Error("Unexpected return value from call to SetWriteDeadline()") | ||
} | ||
|
||
if atomic.LoadInt32(&clt.calls) != 8 { | ||
t.Error("Unexpected number of calls recorded") | ||
} | ||
} | ||
|
||
type connLoggerTest struct { | ||
calls int32 | ||
} | ||
|
||
var _ net.Conn = &connLoggerTest{} | ||
|
||
type addressTest struct { | ||
} | ||
|
||
var _ net.Addr = &addressTest{} | ||
|
||
type nullLogger struct { | ||
} | ||
|
||
var _ Logger = nullLogger{} | ||
|
||
func (n nullLogger) Printf(format string, v ...interface{}) { | ||
} | ||
|
||
func (n nullLogger) Println(v ...interface{}) { | ||
} | ||
|
||
func (a *addressTest) Network() string { | ||
return "test" | ||
} | ||
|
||
func (a *addressTest) String() string { | ||
return "test" | ||
} | ||
|
||
func (cl *connLoggerTest) Read(p []byte) (int, error) { | ||
atomic.AddInt32(&cl.calls, 1) | ||
return cap(p), nil | ||
} | ||
|
||
func (cl *connLoggerTest) Write(p []byte) (int, error) { | ||
atomic.AddInt32(&cl.calls, 1) | ||
return len(p), nil | ||
} | ||
|
||
func (cl *connLoggerTest) Close() error { | ||
atomic.AddInt32(&cl.calls, 1) | ||
return nil | ||
} | ||
|
||
func (cl *connLoggerTest) LocalAddr() net.Addr { | ||
atomic.AddInt32(&cl.calls, 1) | ||
return &addressTest{} | ||
} | ||
|
||
func (cl *connLoggerTest) RemoteAddr() net.Addr { | ||
atomic.AddInt32(&cl.calls, 1) | ||
return &addressTest{} | ||
} | ||
|
||
func (cl *connLoggerTest) SetDeadline(t time.Time) error { | ||
atomic.AddInt32(&cl.calls, 1) | ||
return nil | ||
} | ||
|
||
func (cl *connLoggerTest) SetReadDeadline(t time.Time) error { | ||
atomic.AddInt32(&cl.calls, 1) | ||
return nil | ||
} | ||
|
||
func (cl *connLoggerTest) SetWriteDeadline(t time.Time) error { | ||
atomic.AddInt32(&cl.calls, 1) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters