uquic/integrationtests/tools/testlog/testlog.go
2019-04-01 01:54:24 +09:00

55 lines
1.1 KiB
Go

package testlog
import (
"bytes"
"flag"
"log"
"os"
"sync"
"github.com/lucas-clemente/quic-go/internal/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const logBufSize = 100 * 1 << 20 // initial size of the log buffer: 100 MB
var (
logFileName string // the log file set in the ginkgo flags
logBufOnce sync.Once
logBuf *bytes.Buffer
)
// read the logfile command line flag
// to set call ginkgo -- -logfile=log.txt
func init() {
flag.StringVar(&logFileName, "logfile", "", "log file")
}
var _ = BeforeEach(func() {
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
if Debug() {
logBufOnce.Do(func() {
logBuf = bytes.NewBuffer(make([]byte, 0, logBufSize))
})
utils.DefaultLogger.SetLogLevel(utils.LogLevelDebug)
log.SetOutput(logBuf)
}
})
var _ = AfterEach(func() {
if Debug() {
logFile, err := os.Create(logFileName)
Expect(err).ToNot(HaveOccurred())
logFile.Write(logBuf.Bytes())
logFile.Close()
logBuf.Reset()
}
})
// Debug says if this test is being logged
func Debug() bool {
return len(logFileName) > 0
}