Skip to content

Commit 12094b6

Browse files
committed
1 parent 05994a9 commit 12094b6

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

errors.go

+25
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313
"errors"
1414
"fmt"
1515
"io"
16+
"log"
17+
"os"
18+
"sync"
1619
)
1720

1821
var (
@@ -24,8 +27,30 @@ var (
2427
errPktSync = errors.New("Commands out of sync. You can't run this command now")
2528
errPktSyncMul = errors.New("Commands out of sync. Did you run multiple statements at once?")
2629
errPktTooLarge = errors.New("Packet for query is too large. You can change this value on the server by adjusting the 'max_allowed_packet' variable.")
30+
errBusyBuffer = errors.New("Busy buffer")
31+
errNoLogger = errors.New("logger is nil")
32+
33+
errLog Logger = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
34+
errLogLock = &sync.Mutex{}
2735
)
2836

37+
// Logger is used to log critical error messages.
38+
type Logger interface {
39+
Print(v ...interface{})
40+
}
41+
42+
// SetLogger is used to set the logger for critical errors.
43+
// The initial logger is stderr.
44+
func SetLogger(logger Logger) error {
45+
if logger == nil {
46+
return errNoLogger
47+
}
48+
errLogLock.Lock()
49+
errLog = logger
50+
errLogLock.Unlock()
51+
return nil
52+
}
53+
2954
// MySQLError is an error type which represents a single MySQL error
3055
type MySQLError struct {
3156
Number uint16

errors_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2+
//
3+
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
// You can obtain one at http://mozilla.org/MPL/2.0/.
8+
9+
package mysql
10+
11+
import (
12+
"bytes"
13+
"log"
14+
"testing"
15+
)
16+
17+
func TestSetLogger(t *testing.T) {
18+
previous := errLog
19+
defer func() {
20+
errLog = previous
21+
}()
22+
const expected = "prefix: test\n"
23+
buffer := bytes.NewBuffer(make([]byte, 0, 64))
24+
logger := log.New(buffer, "prefix: ", 0)
25+
SetLogger(logger)
26+
errLog.Print("test")
27+
if actual := buffer.String(); actual != expected {
28+
t.Errorf("expected %q, got %q", expected, actual)
29+
}
30+
}

packets.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
228228
data := mc.buf.takeSmallBuffer(pktLen + 4)
229229
if data == nil {
230230
// can not take the buffer. Something must be wrong with the connection
231-
errLog.Print("Busy buffer")
231+
errLog.Print(errBusyBuffer.Error())
232232
return driver.ErrBadConn
233233
}
234234

@@ -299,7 +299,7 @@ func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
299299
data := mc.buf.takeSmallBuffer(pktLen + 4)
300300
if data == nil {
301301
// can not take the buffer. Something must be wrong with the connection
302-
errLog.Print("Busy buffer")
302+
errLog.Print(errBusyBuffer.Error())
303303
return driver.ErrBadConn
304304
}
305305

@@ -320,7 +320,7 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
320320
data := mc.buf.takeSmallBuffer(4 + 1)
321321
if data == nil {
322322
// can not take the buffer. Something must be wrong with the connection
323-
errLog.Print("Busy buffer")
323+
errLog.Print(errBusyBuffer.Error())
324324
return driver.ErrBadConn
325325
}
326326

@@ -339,7 +339,7 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
339339
data := mc.buf.takeBuffer(pktLen + 4)
340340
if data == nil {
341341
// can not take the buffer. Something must be wrong with the connection
342-
errLog.Print("Busy buffer")
342+
errLog.Print(errBusyBuffer.Error())
343343
return driver.ErrBadConn
344344
}
345345

@@ -360,7 +360,7 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
360360
data := mc.buf.takeSmallBuffer(4 + 1 + 4)
361361
if data == nil {
362362
// can not take the buffer. Something must be wrong with the connection
363-
errLog.Print("Busy buffer")
363+
errLog.Print(errBusyBuffer.Error())
364364
return driver.ErrBadConn
365365
}
366366

@@ -751,7 +751,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
751751
}
752752
if data == nil {
753753
// can not take the buffer. Something must be wrong with the connection
754-
errLog.Print("Busy buffer")
754+
errLog.Print(errBusyBuffer.Error())
755755
return driver.ErrBadConn
756756
}
757757

utils.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,19 @@ import (
1616
"errors"
1717
"fmt"
1818
"io"
19-
"log"
2019
"net/url"
21-
"os"
2220
"strings"
2321
"time"
2422
)
2523

2624
var (
27-
errLog *log.Logger // Error Logger
2825
tlsConfigRegister map[string]*tls.Config // Register for custom tls.Configs
2926

3027
errInvalidDSNUnescaped = errors.New("Invalid DSN: Did you forget to escape a param value?")
3128
errInvalidDSNAddr = errors.New("Invalid DSN: Network Address not terminated (missing closing brace)")
3229
)
3330

3431
func init() {
35-
errLog = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
3632
tlsConfigRegister = make(map[string]*tls.Config)
3733
}
3834

@@ -678,5 +674,5 @@ func appendLengthEncodedInteger(b []byte, n uint64) []byte {
678674
return append(b, 0xfd, byte(n), byte(n>>8), byte(n>>16))
679675
}
680676
return append(b, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
681-
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
677+
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
682678
}

0 commit comments

Comments
 (0)