-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
62 lines (52 loc) · 1.59 KB
/
path.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
package utils
import (
"fmt"
"os"
"runtime"
"strings"
)
type callerInfo struct{
Caller *runtime.Func
PathSplit []string
Line int
}
func getFilePathSplit(iSkipLevel int) *callerInfo {
fpcs := make([]uintptr, 1)
// Skip 2 levels to get the direct caller
// Skip 3 levels to get the caller's caller
// etc.
n := runtime.Callers(iSkipLevel, fpcs)
if n == 0 {
fmt.Println("MSG: NO CALLER")
}
caller := runtime.FuncForPC(fpcs[0] - 1)
if caller == nil {
fmt.Println("MSG CALLER WAS NIL")
}
// Print the file name and line number
path, line := caller.FileLine(fpcs[0] - 1)
callerInfo := &callerInfo{
caller,
strings.Split(path, string(os.PathSeparator)),
line,
}
return callerInfo
}
/*
iBackTraceLevel:
set to 1, returns just the caller's filepath (without the file name)
set to n>1, returns the caller's filepath (without the file name) and every folder above recursively for n-1 times
*/
func GetCallerPaths(iBackTraceLevel int) []string{
callerInfo := getFilePathSplit(3) //3 to return the caller of this function (2 to return this function itself)
callerFilePath := strings.Join(callerInfo.PathSplit[:len(callerInfo.PathSplit)-1], string(os.PathSeparator)) //-1 to exclude file name
filePaths := []string{callerFilePath}
for i:=1; i< iBackTraceLevel; i++{
fatherPathSplit := strings.Split(filePaths[len(filePaths)-1], string(os.PathSeparator))
if len(fatherPathSplit[len(fatherPathSplit)-1]) == 0{ //we got to the system root folder
break
}
filePaths = append(filePaths, strings.Join( fatherPathSplit[:len(fatherPathSplit)-1] , string(os.PathSeparator)))
}
return filePaths
}