aboutsummaryrefslogtreecommitdiffstats
path: root/blockpool/test/logger.go
blob: 2828ffc83ec9ea8816d1fe74515f2286b8c2229f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package test

import (
    "log"
    "os"
    "sync"
    "testing"

    "github.com/ethereum/go-ethereum/logger"
)

// logging in tests

var once sync.Once

/* usage:
func TestFunc(t *testing.T) {
    test.LogInit()
    // test
}
*/
func LogInit() {
    once.Do(func() {
        logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.LogLevel(logger.DebugDetailLevel))
    })
}

type testLogger struct{ t *testing.T }

/* usage:
func TestFunc(t *testing.T) {
    defer test.Testlog.Detach()
    // test
}
*/
func Testlog(t *testing.T) testLogger {
    logger.Reset()
    l := testLogger{t}
    logger.AddLogSystem(l)
    return l
}

func (l testLogger) LogPrint(msg logger.LogMsg) {
    l.t.Log(msg.String())
}

func (testLogger) Detach() {
    logger.Flush()
    logger.Reset()
}

type benchLogger struct{ b *testing.B }

/* usage:
func BenchmarkFunc(b *testing.B) {
    defer test.Benchlog.Detach()
    // test
}
*/
func Benchlog(b *testing.B) benchLogger {
    logger.Reset()
    l := benchLogger{b}
    logger.AddLogSystem(l)
    return l
}

func (l benchLogger) LogPrint(msg logger.LogMsg) {
    l.b.Log(msg.String())
}

func (benchLogger) Detach() {
    logger.Flush()
    logger.Reset()
}