aboutsummaryrefslogtreecommitdiffstats
path: root/blockpool/test/logger.go
blob: bcb4d4cb34b055535daebd0782b141399bbec581 (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
75
76
77
78
79
80
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() {
        var logsys = logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.LogLevel(logger.WarnLevel))
        logger.AddLogSystem(logsys)
    })
}

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 (testLogger) GetLogLevel() logger.LogLevel { return logger.DebugLevel }
func (testLogger) SetLogLevel(logger.LogLevel)  {}

func (l testLogger) LogPrint(level logger.LogLevel, msg string) {
    l.t.Logf("%s", msg)
}

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 (benchLogger) GetLogLevel() logger.LogLevel { return logger.Silence }

func (benchLogger) SetLogLevel(logger.LogLevel) {}
func (l benchLogger) LogPrint(level logger.LogLevel, msg string) {
    l.b.Logf("%s", msg)
}
func (benchLogger) Detach() {
    logger.Flush()
    logger.Reset()
}