-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
100 lines (82 loc) · 2.08 KB
/
util.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
package main
import (
"fmt"
"log"
"os"
"runtime"
"strings"
)
// LogWriterInterface is the abstraction of the Log Writer
type LogWriterInterface interface {
Write(bytes []byte) (int, error)
}
// LogWriter represents the implementation of the log writer
type LogWriter struct {
}
func (writer LogWriter) Write(bytes []byte) (int, error) {
return fmt.Print(string(bytes))
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func stringHasParent(a string, list []string) bool {
for _, b := range list {
if strings.HasPrefix(a, b) {
return true
}
}
return false
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
// UsageWriter implements the memory writing process
type UsageWriter struct {
logEnabled bool
logFile string
}
// PrintMemUsage prings the os memory usage in logs of file
func (usageWriter *UsageWriter) PrintMemUsage() {
if !usageWriter.logEnabled {
return
}
var m runtime.MemStats
var filename = usageWriter.logFile
memoryFile, error := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if error != nil {
panic(error)
}
runtime.ReadMemStats(&m)
if _, error := memoryFile.Write([]byte(fmt.Sprintf("Alloc = %v MiB", bToMb(m.Alloc)))); error != nil {
log.Fatal(error)
}
if _, error := memoryFile.Write([]byte(fmt.Sprintf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc)))); error != nil {
log.Fatal(error)
}
if _, error := memoryFile.Write([]byte(fmt.Sprintf("\tSys = %v MiB", bToMb(m.Sys)))); error != nil {
log.Fatal(error)
}
if _, error := memoryFile.Write([]byte(fmt.Sprintf("\tNumGC = %v\n", m.NumGC))); error != nil {
log.Fatal(error)
}
if error := memoryFile.Close(); error != nil {
log.Fatal(error)
}
}
// NewUsageWriter is the constructor for the UsageWriter object
func NewUsageWriter(logEnabled bool, logFile string) *UsageWriter {
return &UsageWriter{logEnabled, logFile}
}
type arrayFlags []string
func (i *arrayFlags) String() string {
return "String"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}