forked from moredhel/env_logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.go
52 lines (42 loc) · 1013 Bytes
/
timing.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
package env_logger
import (
"fmt"
"sync"
"time"
)
var timers map[string]time.Time = make(map[string]time.Time)
var timersMu sync.Mutex
func Timer(idkey string) string {
timersMu.Lock()
defer timersMu.Unlock()
timers[idkey] = time.Now()
return idkey
}
// Print time since the last call to the Time function with the same name
func TimerEnd(idkey string) string {
timersMu.Lock()
defer timersMu.Unlock()
if t, ok := timers[idkey]; ok {
res := fmt.Sprint(time.Since(t))
delete(timers, idkey)
return res
}
return "unknown timer"
}
// Print time since the last call to the Time function with the same name
func TimerEndValue(idkey string) (time.Duration, error) {
timersMu.Lock()
defer timersMu.Unlock()
if t, ok := timers[idkey]; ok {
res := time.Since(t)
delete(timers, idkey)
return res, nil
}
return 0, fmt.Errorf("unknown timer")
}
func (e *Entry) Timer(idkey string) string {
return Timer(idkey)
}
func (e *Entry) TimerEnd(idkey string) string {
return TimerEnd(idkey)
}