forked from mumax/3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
106 lines (90 loc) · 2.09 KB
/
log.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
package util
// Logging and error reporting utility functions
import (
"fmt"
"log"
"runtime"
"sync"
"time"
)
func Fatal(msg ...interface{}) {
log.Fatal(msg...)
}
func Fatalf(format string, msg ...interface{}) {
log.Fatalf(format, msg...)
}
// If err != nil, trigger log.Fatal(msg, err)
func FatalErr(err interface{}) {
_, file, line, _ := runtime.Caller(1)
if err != nil {
log.Fatal(file, ":", line, err)
}
}
// Panics if err is not nil. Signals a bug.
func PanicErr(err error) {
if err != nil {
log.Panic(err)
}
}
// Logs the error of non-nil, plus message
func LogErr(err error, msg ...interface{}) {
if err != nil {
log.Println(append(msg, err)...)
}
}
func Log(msg ...interface{}) {
log.Println(msg...)
}
// Panics with "illegal argument" if test is false.
func Argument(test bool) {
if !test {
log.Panic("illegal argument")
}
}
// Panics with msg if test is false
func AssertMsg(test bool, msg interface{}) {
if !test {
log.Panic(msg)
}
}
// Panics with "assertion failed" if test is false.
func Assert(test bool) {
if !test {
log.Panic("assertion failed")
}
}
// Hack to avoid cyclic dependency on engine.
var (
progress_ func(int, int, string) = PrintProgress
progLock sync.Mutex
)
// Set progress bar to progress/total and display msg
// if GUI is up and running.
func Progress(progress, total int, msg string) {
progLock.Lock()
defer progLock.Unlock()
if progress_ != nil {
progress_(progress, total, msg)
}
}
var (
lastPct = -1 // last progress percentage shown
lastProgT time.Time // last time we showed progress percentage
)
func PrintProgress(prog, total int, msg string) {
pct := (prog * 100) / total
if pct != lastPct { // only print percentage if changed
if (time.Since(lastProgT) > time.Second) || pct == 100 { // only print percentage once/second unless finished
fmt.Println("//", msg, pct, "%")
lastPct = pct
lastProgT = time.Now()
}
}
}
// Sets the function to be used internally by Progress.
// Avoids cyclic dependency on engine.
func SetProgress(f func(int, int, string)) {
progLock.Lock()
defer progLock.Unlock()
progress_ = f
}