-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker_debug.go
54 lines (44 loc) · 1.16 KB
/
tracker_debug.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
//go:build trackdebug
package tracker
import (
"fmt"
"runtime"
"github.com/google/uuid"
)
func getPackage() string {
// we get the callers as uintptrs - but we just need 1
fpcs := make([]uintptr, 1)
// skip 4 levels to get to the caller of whoever called getPackage()
n := runtime.Callers(4, fpcs)
if n == 0 {
return ""
}
// get the info of the actual function that's in the pointer
fun := runtime.FuncForPC(fpcs[0] - 1)
if fun == nil {
return ""
}
name := fun.Name()
return name
}
// Go starts a tracked go routine and injects a tracker that needs to be used. At a minimum use a select to listen to its Done() channel
func (t Tracker) GoRef(ref string, function func(tkr Tracker)) { // Always call before go routine creation, also always call defer done
t.wgAdd()
source := getPackage()
routineid := uuid.New().String()
if t.Logging {
fmt.Printf("Start %s-%s from %s\n", routineid, ref, source)
}
go func() {
if t.deferFunc != nil {
defer (*t.deferFunc)()
} else if globalDeferFunc != nil {
defer (*globalDeferFunc)()
}
function(t)
t.wgDone()
if t.Logging {
fmt.Printf("Stop %s-%s from %s\n", routineid, ref, source)
}
}()
}