forked from quic-go/quic-go
Merge pull request #1843 from lucas-clemente/in-memory-logging
log to memory in integration tests
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
package testlog
|
package testlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/lucas-clemente/quic-go/internal/utils"
|
"github.com/lucas-clemente/quic-go/internal/utils"
|
||||||
|
|
||||||
@@ -11,9 +13,38 @@ import (
|
|||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const logBufSize = 100 * 1 << 20 // initial size of the log buffer: 100 MB
|
||||||
|
|
||||||
|
type syncedBuffer struct {
|
||||||
|
mutex sync.Mutex
|
||||||
|
|
||||||
|
*bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *syncedBuffer) Write(p []byte) (int, error) {
|
||||||
|
b.mutex.Lock()
|
||||||
|
n, err := b.Buffer.Write(p)
|
||||||
|
b.mutex.Unlock()
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *syncedBuffer) Bytes() []byte {
|
||||||
|
b.mutex.Lock()
|
||||||
|
p := b.Buffer.Bytes()
|
||||||
|
b.mutex.Unlock()
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *syncedBuffer) Reset() {
|
||||||
|
b.mutex.Lock()
|
||||||
|
b.Buffer.Reset()
|
||||||
|
b.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logFileName string // the log file set in the ginkgo flags
|
logFileName string // the log file set in the ginkgo flags
|
||||||
logFile *os.File
|
logBufOnce sync.Once
|
||||||
|
logBuf *syncedBuffer
|
||||||
)
|
)
|
||||||
|
|
||||||
// read the logfile command line flag
|
// read the logfile command line flag
|
||||||
@@ -25,18 +56,22 @@ func init() {
|
|||||||
var _ = BeforeEach(func() {
|
var _ = BeforeEach(func() {
|
||||||
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
|
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
|
||||||
|
|
||||||
if len(logFileName) > 0 {
|
if Debug() {
|
||||||
var err error
|
logBufOnce.Do(func() {
|
||||||
logFile, err = os.Create(logFileName)
|
logBuf = &syncedBuffer{Buffer: bytes.NewBuffer(make([]byte, 0, logBufSize))}
|
||||||
Expect(err).ToNot(HaveOccurred())
|
})
|
||||||
log.SetOutput(logFile)
|
|
||||||
utils.DefaultLogger.SetLogLevel(utils.LogLevelDebug)
|
utils.DefaultLogger.SetLogLevel(utils.LogLevelDebug)
|
||||||
|
log.SetOutput(logBuf)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
var _ = AfterEach(func() {
|
var _ = AfterEach(func() {
|
||||||
if len(logFileName) > 0 {
|
if Debug() {
|
||||||
_ = logFile.Close()
|
logFile, err := os.Create(logFileName)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
logFile.Write(logBuf.Bytes())
|
||||||
|
logFile.Close()
|
||||||
|
logBuf.Reset()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user