-
Notifications
You must be signed in to change notification settings - Fork 225
/
utils.go
44 lines (38 loc) · 1.25 KB
/
utils.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
package logger
import (
"fmt"
"runtime"
"time"
)
// LogAndMeasureExecutionTime logs that `functionName` has
// started. The user is expected to defer `onEnd`, which
// will then log that the function has ended, as well as
// the time duration the function had ran.
func LogAndMeasureExecutionTime(log *Logger, functionName string) (onEnd func()) {
start := time.Now()
log.Debugf("%s start", functionName)
return func() {
log.Debugf("%s end. Took: %s", functionName, time.Since(start))
}
}
// LogMemoryStats logs memory stats for `functionName`
func LogMemoryStats(log *Logger, functionName string) {
log.Debug(NewLogClosure(func() string {
stats := runtime.MemStats{}
runtime.ReadMemStats(&stats)
return fmt.Sprintf("%s: used memory: %d bytes, total: %d bytes", functionName,
stats.Alloc, stats.HeapIdle-stats.HeapReleased+stats.HeapInuse)
}))
}
// LogClosure is a closure that can be printed with %s to be used to
// generate expensive-to-create data for a detailed log level and avoid doing
// the work if the data isn't printed.
type LogClosure func() string
func (c LogClosure) String() string {
return c()
}
// NewLogClosure casts a function to a LogClosure.
// See LogClosure for details.
func NewLogClosure(c func() string) LogClosure {
return c
}