summaryrefslogtreecommitdiff
path: root/txnsync/emulatorLogger_test.go
blob: 7020b7512a9435ca4a3d5c5ebdc6373e9db5085e (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package txnsync

import (
	"fmt"
	"strconv"
	"strings"
	"testing"

	"github.com/algorand/go-algorand/logging"
)

// Foreground text colors
const (
	reset     = 0
	black     = 30
	red       = 31
	green     = 32
	yellow    = 33
	blue      = 34
	magenta   = 35
	cyan      = 36
	white     = 37
	hiblack   = 90
	hired     = 91
	higreen   = 92
	hiyellow  = 93
	hiblue    = 94
	himagenta = 95
	hicyan    = 96
	hiwhite   = 97
)

const escape = "\x1b"

var colors = []int{red, green, yellow, blue, magenta, cyan, hired, higreen, hiyellow, hiblue, himagenta, hicyan}
var lowColors = []int{red, green, yellow, blue, magenta, cyan}

type emulatorNodeLogger struct {
	algodlogger
	node        *emulatedNode
	longestName int
}

func makeNodeLogger(l logging.Logger, node *emulatedNode) Logger {
	return &emulatorNodeLogger{
		algodlogger: l,
		node:        node,
	}
}

type msgMode int

const (
	modeZero msgMode = iota
	modeIncoming
	modeOutgoing
)

// implement local interface Logger
func (e *emulatorNodeLogger) outgoingMessage(mstat msgStats) {
	e.printMsgStats(mstat, modeOutgoing)
}

// implement local interface Logger
func (e *emulatorNodeLogger) incomingMessage(mstat msgStats) {
	e.printMsgStats(mstat, modeIncoming)
}

func (e emulatorNodeLogger) printMsgStats(mstat msgStats, mode msgMode) {
	seq := int(mstat.sequenceNumber)
	round := mstat.round
	transactions := mstat.transactions
	offset := mstat.offsetModulator.Offset
	modulator := mstat.offsetModulator.Modulator
	bloom := mstat.bloomSize
	nextTS := mstat.nextMsgMinDelay
	// emulator peer addresses are just an int
	destIndex, _ := strconv.Atoi(mstat.peerAddress)

	destName := e.node.emulator.nodes[destIndex].name

	if e.longestName == 0 {
		for _, node := range e.node.emulator.nodes {
			if len(node.name) > e.longestName {
				e.longestName = len(node.name) + 1
			}
		}
	}

	elapsed := e.node.emulator.clock.Since().Milliseconds()
	out := fmt.Sprintf("%3d.%03d ", elapsed/1000, elapsed%1000)
	out += fmt.Sprintf("%"+fmt.Sprintf("%d", e.longestName)+"s", e.node.name)

	bfColor := hiblack
	if bloom > 0 {
		bfColor = higreen
	}
	nextTSColor := hiblack
	if nextTS > 0 {
		nextTSColor = higreen
	}
	mid := fmt.Sprintf("Round %s Txns %s Req [%3d/%3d] %s %s",
		wrapRollingColor(int(round), fmt.Sprintf("%2d", round)),
		wrapRollingColor(transactions, fmt.Sprintf("%3d", transactions)),
		offset,
		modulator,
		wrapColor(bfColor, "BF"),
		wrapColor(nextTSColor, "TS"),
	)
	if mode == modeOutgoing {
		out += wrapRollingLowColor(seq, " [ ")
		out += mid + wrapRollingLowColor(seq, " --> ") + strings.Repeat(" ", 20)
		out += wrapColor(hiblack, " ] ")
	} else {
		out += wrapColor(hiblack, " [ ")
		out += strings.Repeat(" ", 20) + wrapRollingLowColor(seq, " <-- ") + mid
		out += wrapRollingLowColor(seq, " ] ")
	}

	out += fmt.Sprintf("%"+fmt.Sprintf("%d", e.longestName)+"s", destName)
	if testing.Verbose() {
		fmt.Printf("%s\n", out)
	}
}

func wrapRollingLowColor(color int, s string) (out string) {
	return wrapColor(lowColors[color%len(lowColors)], s)
}

func wrapRollingColor(color int, s string) (out string) {
	return wrapColor(colors[color%len(colors)], s)
}
func wrapColor(color int, s string) (out string) {
	return fmt.Sprintf("%s[1;%dm%s%s[1;%dm", escape, color, s, escape, reset)
}