-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathprint.go
193 lines (160 loc) · 4.65 KB
/
print.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package overflow
import (
"fmt"
"strings"
"github.com/enescakir/emoji"
"github.com/fatih/color"
)
// a type represneting seting an obtion in the printer builder
type OverflowPrinterOption func(*OverflowPrinterBuilder)
// a type representing the accuumlated state in the builder
//
// the default setting is to print one line for each transaction with meter and all events
type OverflowPrinterBuilder struct {
// filter out some events
EventFilter OverflowEventFilter
// 0 to print no meter, 1 to print some, 2 to pritn all NB verbose
Meter int
// set to false to disable all events
Events bool
// print the emulator log, NB! Verbose
EmulatorLog bool
// print transaction id, useful to disable in tests
Id bool
Arguments bool
TransactionUrl bool
}
// print full meter verbose mode
func WithFullMeter() OverflowPrinterOption {
return func(opb *OverflowPrinterBuilder) {
opb.Meter = 2
}
}
// print meters as part of the transaction output line
func WithMeter() OverflowPrinterOption {
return func(opb *OverflowPrinterBuilder) {
opb.Meter = 1
}
}
func WithTransactionUrl() OverflowPrinterOption {
return func(opb *OverflowPrinterBuilder) {
opb.TransactionUrl = true
}
}
// do not print meter
func WithoutMeter(value int) OverflowPrinterOption {
return func(opb *OverflowPrinterBuilder) {
opb.Meter = 0
}
}
// print the emulator log. NB! Verbose
func WithEmulatorLog() OverflowPrinterOption {
return func(opb *OverflowPrinterBuilder) {
opb.EmulatorLog = true
}
}
// filter out events that are printed
func WithEventFilter(filter OverflowEventFilter) OverflowPrinterOption {
return func(opb *OverflowPrinterBuilder) {
opb.EventFilter = filter
}
}
// do not print events
func WithoutEvents() OverflowPrinterOption {
return func(opb *OverflowPrinterBuilder) {
opb.Events = false
}
}
func WithoutId() OverflowPrinterOption {
return func(opb *OverflowPrinterBuilder) {
opb.Id = false
}
}
func WithArguments() OverflowPrinterOption {
return func(opb *OverflowPrinterBuilder) {
opb.Arguments = true
}
}
// print out an result
func (o OverflowResult) Print(opbs ...OverflowPrinterOption) OverflowResult {
printOpts := &OverflowPrinterBuilder{
Events: true,
EventFilter: OverflowEventFilter{},
Meter: 1,
EmulatorLog: false,
Id: true,
Arguments: false,
TransactionUrl: false,
}
for _, opb := range opbs {
opb(printOpts)
}
messages := []string{}
nameMessage := fmt.Sprintf("Tx:%s", o.Name)
if o.Name == "inline" {
nameMessage = "Inline TX"
}
messages = append(messages, nameMessage)
if len(o.Fee) != 0 {
messages = append(messages, fmt.Sprintf("fee:%.8f gas:%d", o.Fee["amount"], o.FeeGas))
} else {
if o.ComputationUsed != 0 {
messages = append(messages, fmt.Sprintf("gas:%d", o.ComputationUsed))
}
}
if printOpts.Id {
messages = append(messages, fmt.Sprintf("id:%s", o.Id.String()))
}
icon := emoji.OkHand.String()
if o.Err != nil {
color.Red("%v Error executing transaction: %s error:%v", emoji.PileOfPoo, o.Name, o.Err)
icon = emoji.PileOfPoo.String()
}
fmt.Printf("%v %s\n", icon, strings.Join(messages, " "))
if printOpts.TransactionUrl {
fmt.Printf("https://flowscan.org/transaction/%s\n", o.Id)
}
if printOpts.Arguments {
o.PrintArguments(nil)
}
if printOpts.Events {
events := o.Events
if len(printOpts.EventFilter) != 0 {
events = events.FilterEvents(printOpts.EventFilter)
}
if len(events) != 0 {
events.Print(nil)
}
}
if printOpts.EmulatorLog && len(o.RawLog) > 0 {
fmt.Println("=== LOG ===")
for _, msg := range o.RawLog {
fmt.Println(msg.Msg)
}
}
/*
//TODO: print how a meter is computed
if printOpts.Meter == 1 && o.Meter != nil {
messages = append(messages, fmt.Sprintf("loops:%d", o.Meter.Loops()))
messages = append(messages, fmt.Sprintf("statements:%d", o.Meter.Statements()))
messages = append(messages, fmt.Sprintf("invocations:%d", o.Meter.FunctionInvocations()))
}
*/
if printOpts.Meter != 0 && o.Meter != nil {
if printOpts.Meter == 2 {
fmt.Println("=== METER ===")
fmt.Printf("LedgerInteractionUsed: %d\n", o.Meter.LedgerInteractionUsed)
if o.Meter.MemoryUsed != 0 {
fmt.Printf("Memory: %d\n", o.Meter.MemoryUsed)
memories := strings.ReplaceAll(strings.Trim(fmt.Sprintf("%+v", o.Meter.MemoryIntensities), "map[]"), " ", "\n ")
fmt.Println("Memory Intensities")
fmt.Printf(" %s\n", memories)
}
fmt.Printf("Computation: %d\n", o.Meter.ComputationUsed)
intensities := strings.ReplaceAll(strings.Trim(fmt.Sprintf("%+v", o.Meter.ComputationIntensities), "map[]"), " ", "\n ")
fmt.Println("Computation Intensities:")
fmt.Printf(" %s\n", intensities)
}
}
return o
}