Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,20 @@ func shouldUseColor() bool {
}

func configureLogging() {
base := &log.TextFormatter{DisableQuote: true}
switch normalizeColorMode(colorMode) {
case colorModeAlways:
log.SetFormatter(&log.TextFormatter{ForceColors: true})
base.ForceColors = true
log.SetFormatter(base)
case colorModeNever:
log.SetFormatter(&log.TextFormatter{DisableColors: true})
base.DisableColors = true
log.SetFormatter(base)
case colorModeAuto:
// Use logrus default auto behavior.
// Use logrus default auto behavior with DisableQuote enabled.
log.SetFormatter(base)
default:
log.SetFormatter(&log.TextFormatter{ForceColors: true})
base.ForceColors = true
log.SetFormatter(base)
}
}

Expand Down
30 changes: 29 additions & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main
import (
"bytes"
"context"
"crypto/sha256"
"database/sql"
"flag"
"fmt"
Expand Down Expand Up @@ -361,6 +362,23 @@ func (t *tester) addSuccess(testSuite *XUnitTestSuite, startTime *time.Time, cnt
})
}

func formatSQLForLog(sql string) string {
const maxLen = 1024
if len(sql) <= maxLen {
return sql
}
const headLen = 256
const tailLen = 256
sum := sha256.Sum256([]byte(sql))
head := sql[:headLen]
tail := sql[len(sql)-tailLen:]
head = strings.ReplaceAll(head, "\n", " ")
head = strings.ReplaceAll(head, "\r", " ")
tail = strings.ReplaceAll(tail, "\n", " ")
tail = strings.ReplaceAll(tail, "\r", " ")
return fmt.Sprintf("len=%d sha256=%x head=%q tail=%q", len(sql), sum, head, tail)
}

func (t *tester) Run() error {
t.preProcess()
defer t.postProcess()
Expand Down Expand Up @@ -443,7 +461,7 @@ func (t *tester) Run() error {
if t.enableConcurrent {
concurrentQueue = append(concurrentQueue, q)
} else if err = t.execute(q); err != nil {
err = errors.Annotate(err, fmt.Sprintf("sql:%v", q.Query))
err = errors.Annotate(err, fmt.Sprintf("sql:%s", formatSQLForLog(q.Query)))
t.addFailure(&testSuite, &err, testCnt)
return err
}
Expand Down Expand Up @@ -976,6 +994,16 @@ func dumpToByteRows(rows *sql.Rows) ([]*byteRows, error) {
return nil, errors.Trace(err)
}

for i, col := range tmp {
if col == nil {
continue
}
// Defensive copy to avoid driver buffer reuse corrupting large results.
copied := make([]byte, len(col))
copy(copied, col)
tmp[i] = copied
}

data = append(data, byteRow{tmp})
}

Expand Down