Forráskód Böngészése

Added debug and error logging
Fixed Issue #1

Julien Schmidt 13 éve
szülő
commit
8a6f64253f
4 módosított fájl, 27 hozzáadás és 10 törlés
  1. 2 2
      connection.go
  2. 0 5
      driver.go
  3. 12 2
      packets.go
  4. 13 1
      utils.go

+ 2 - 2
connection.go

@@ -60,11 +60,11 @@ func (mc *mysqlConn) handleParams() (e error) {
 
 		// TLS-Encryption
 		case "tls":
-			debug("TLS-Encryption not implemented yet")
+			dbgLog.Print("TLS-Encryption not implemented yet")
 
 		// Compression
 		case "compress":
-			debug("TLS-Encryption not implemented yet")
+			dbgLog.Print("Compression not implemented yet")
 
 		// We don't want to set keepalive as system var
 		case "keepalive":

+ 0 - 5
driver.go

@@ -12,14 +12,9 @@ import (
 	"database/sql"
 	"database/sql/driver"
 	"errors"
-	"fmt"
 	"net"
 )
 
-func debug(msg string) {
-	fmt.Println("DEBUG MySQL: " + msg)
-}
-
 type mysqlDriver struct{}
 
 // Open new Connection.

+ 12 - 2
packets.go

@@ -57,6 +57,7 @@ func (mc *mysqlConn) readPacket() (data []byte, e error) {
 	}
 	
 	if e != nil || n != int(pktLen) {
+		errLog.Print(e)
 		e = driver.ErrBadConn
 		return
 	}
@@ -84,6 +85,10 @@ func (mc *mysqlConn) writePacket(data []byte) (e error) {
 	// Write packet
 	n, e := mc.netConn.Write(pktData)
 	if e != nil || n != len(pktData) {
+		if e == nil {
+			e = errors.New("Length of send data does not match packet length")
+		}
+		errLog.Print(e)
 		e = driver.ErrBadConn
 		return
 	}
@@ -99,6 +104,10 @@ func (mc *mysqlConn) readNumber(n uint8) (num uint64, e error) {
 
 	nr, err := io.ReadFull(mc.netConn, buf)
 	if err != nil || nr != int(n) {
+		if e == nil {
+			e = errors.New("Length of read data does not match header length")
+		}
+		errLog.Print(e)
 		e = driver.ErrBadConn
 		return
 	}
@@ -289,14 +298,14 @@ func (mc *mysqlConn) writeCommandPacket(command commandType, args ...interface{}
 	// Commands with 1 arg unterminated string
 	case COM_QUERY, COM_STMT_PREPARE:
 		if len(args) != 1 {
-			return fmt.Errorf("Invalid arguments count (Got: %d Need: 1)", len(args))
+			return fmt.Errorf("Invalid arguments count (Got: %d Has: 1)", len(args))
 		}
 		data = append(data, []byte(args[0].(string))...)
 
 	// Commands with 1 arg 32 bit uint
 	case COM_STMT_CLOSE:
 		if len(args) != 1 {
-			return fmt.Errorf("Invalid arguments count (Got: %d Need: 1)", len(args))
+			return fmt.Errorf("Invalid arguments count (Got: %d Has: 1)", len(args))
 		}
 		data = append(data, uint32ToBytes(args[0].(uint32))...)
 	default:
@@ -421,6 +430,7 @@ The order of packets for a result set is:
 func (mc *mysqlConn) readResultSetHeaderPacket() (fieldCount int, e error) {
 	data, e := mc.readPacket()
 	if e != nil {
+		errLog.Print(e)
 		e = driver.ErrBadConn
 		return
 	}

+ 13 - 1
utils.go

@@ -12,15 +12,24 @@ import (
 	"bytes"
 	"crypto/sha1"
 	"io"
+	"log"
 	"math"
+	"os"
 	"regexp"
 	"strconv"
 	"strings"
 )
 
-var dsnPattern *regexp.Regexp
+// Logger
+var (
+	errLog *log.Logger
+	dbgLog *log.Logger
+)
 
 func init() {
+	errLog = log.New(os.Stderr, "[MySQL] ", log.LstdFlags)
+	dbgLog = log.New(os.Stdout, "[MySQL] ", log.LstdFlags)
+	
 	dsnPattern = regexp.MustCompile(
 		`^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
 			`(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
@@ -28,6 +37,9 @@ func init() {
 			`(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
 }
 
+// Data Source Name Parser
+var dsnPattern *regexp.Regexp
+
 func parseDSN(dsn string) *config {
 	cfg := new(config)
 	cfg.params = make(map[string]string)