-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
224 lines (207 loc) · 5 KB
/
printer.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package HHESoK
import (
"fmt"
"io"
"log"
"math"
"reflect"
"runtime"
"strings"
)
// DEBUG for turning debug logs on/off
const DEBUG = true
const MeMStat = true
const PREFIX = "->> "
type logger struct {
debug bool
}
func NewLogger(debug bool) Logger {
return &logger{
debug: debug,
}
}
type Logger interface {
PrintMessage(message string)
PrintMessages(messages ...interface{})
PrintFormatted(format string, args ...interface{})
PrintDataLen(data []uint64)
PrintHeader(header string)
PrintMemUsage(name string)
PrintSummarizedVector(name string, vec []uint64, numElements int)
PrintSummarizedMatrix(name string, mat [][]interface{}, numRows int, numElements int)
}
func (l logger) PrintMessage(message string) {
if l.debug {
fmt.Print(PREFIX)
fmt.Printf("%s", message)
fmt.Println()
}
}
func (l logger) PrintMessages(messages ...interface{}) {
if l.debug {
fmt.Print(PREFIX)
for _, message := range messages {
fmt.Print(message)
}
fmt.Println()
}
}
func (l logger) PrintFormatted(format string, args ...interface{}) {
if l.debug {
fmt.Print(PREFIX)
fmt.Printf(format, args...)
fmt.Println()
}
}
func (l logger) PrintDataLen(data []uint64) {
if l.debug {
fmt.Print(PREFIX)
fmt.Printf("Len: %d, Data: %d", len(data), data)
fmt.Println()
}
}
func (l logger) PrintHeader(header string) {
fmt.Println(fmt.Sprintf("=== ----\t\t\t %s \t\t\t---- ===", header))
}
// HandleError checks the error and throws a fatal log if the error isn't nil
func HandleError(err error) {
if err != nil {
fmt.Printf("|-> Error: %s\n", err.Error())
panic("=== Panic\n ")
}
}
// PrintMemUsage outputs the current, total and OS memory being used. As well as the
// number of garage collection cycles completed. For info on each,
// see: https://golang.org/pkg/runtime/#MemStats
func (l logger) PrintMemUsage(name string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
mb := 1e6
alloc := float64(m.Alloc) / mb
tAlloc := float64(m.TotalAlloc) / mb
mSys := float64(m.Sys) / mb
//numGC := m.NumGC
buf := new(strings.Builder)
width := 15 + 7
_, err := fmt.Fprintf(buf, "|-> %-*s", width, name)
HandleError(err)
buf.WriteByte('\t')
prettyPrint(buf, alloc, "MB")
buf.WriteByte('\t')
prettyPrint(buf, tAlloc, "MB")
buf.WriteByte('\t')
prettyPrint(buf, mSys, "MB")
if MeMStat {
fmt.Println(buf)
}
}
// Helps to print the MemStats
func prettyPrint(w io.Writer, x float64, unit string) {
// Print all numbers with 10 places before the decimal point
// and small numbers with four sig figs. Field widths are
// chosen to fit the whole part in 10 places while aligning
// the decimal point of all fractional formats.
var format string
switch y := math.Abs(x); {
case y == 0 || y >= 999.95:
format = "%10.3f %s"
case y >= 99.995:
format = "%10.3f %s"
case y >= 9.9995:
format = "%10.3f %s"
case y >= 0.99995:
format = "%10.3f %s"
case y >= 0.099995:
format = "%15.4f %s"
case y >= 0.0099995:
format = "%16.5f %s"
case y >= 0.00099995:
format = "%17.6f %s"
default:
format = "%18.7f %s"
}
_, err := fmt.Fprintf(w, format, x, unit)
HandleError(err)
}
// PrintSummarizedMatrix prints a summarized view of a matrix
func (l logger) PrintSummarizedMatrix(name string, mat [][]interface{}, numRows int, numElements int) {
const summaryLength = 5
if len(mat) == 0 || len(mat[0]) == 0 {
fmt.Print(PREFIX)
fmt.Println("Matrix is empty!")
return
}
// Get the kind of the first element in the first row
v := reflect.TypeOf(mat[0][0])
format := ""
switch v.Kind() {
case reflect.Uint64:
format = "%d "
case reflect.Float64:
format = "%.2f "
default:
log.Fatal("Unsupported type")
return
}
if l.debug {
fmt.Printf("%s:\n", name)
for i := 0; i < numRows; i++ {
if i > summaryLength {
break
}
fmt.Printf("[%d][]: {", i)
if numElements > 2*summaryLength {
for j := 0; j < summaryLength; j++ {
fmt.Printf(format, mat[i][j])
}
fmt.Printf("... ")
for j := numElements - summaryLength; j < numElements; j++ {
fmt.Printf(format, mat[i][j])
}
} else {
for j := 0; j < numElements; j++ {
fmt.Printf(format, mat[i][j])
}
}
fmt.Printf("}\n")
}
}
}
// PrintSummarizedVector prints a summarized view of a vector
func (l logger) PrintSummarizedVector(name string, vec []uint64, numElements int) {
const summaryLength = 4
if len(vec) == 0 {
fmt.Print(PREFIX)
fmt.Println("Vector is empty!")
return
}
// Get the kind of the first element in the first row
v := reflect.TypeOf(vec[0])
format := ""
switch v.Kind() {
case reflect.Uint64:
format = "%d "
case reflect.Float64:
format = "%.2f "
default:
log.Fatal("Unsupported type")
return
}
if l.debug {
fmt.Printf("[%s]: {", name)
if numElements > 2*summaryLength {
for i := 0; i < summaryLength; i++ {
fmt.Printf(format, vec[i])
}
fmt.Printf("... ")
for i := numElements - summaryLength; i < numElements; i++ {
fmt.Printf(format, vec[i])
}
} else {
for i := 0; i < numElements; i++ {
fmt.Printf(format, vec[i])
}
}
fmt.Printf("}\n")
}
}