-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
72 lines (57 loc) · 1.51 KB
/
stack.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
package bdd
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
var curDir, _ = os.Getwd()
func getRelativeDirectory(targpath string) string {
targpath = filepath.Clean(targpath)
if fileName, err := filepath.Rel(curDir, targpath); err == nil && len(fileName) <= len(targpath) {
targpath = fileName
}
return targpath
}
// MarkStackFull fills the output stack
func MarkStackFull() {
for i := 1; ; i++ {
s := SmarkStackFunc(i)
if s == "" {
break
}
fmt.Println(s)
}
}
// MarkStack outputs the prefix stack line pos
func MarkStack(skip int, a ...interface{}) {
fmt.Println(append([]interface{}{SmarkStack(skip + 1)}, a...)...)
}
// Mark outputs the prefix of the current line position
func Mark(a ...interface{}) {
MarkStack(1, a...)
}
// Smark returns output prefix current line position
func Smark(a ...interface{}) string {
return SmarkStack(1, a...)
}
// SmarkStack outputs stack information
func SmarkStack(skip int, a ...interface{}) string {
_, fileName, line, ok := runtime.Caller(skip + 1)
if !ok {
return ""
}
fileName = getRelativeDirectory(fileName)
return fmt.Sprintf("%s:%d %s", fileName, line, fmt.Sprint(a...))
}
// SmarkStackFunc outputs stack information
func SmarkStackFunc(skip int, a ...interface{}) string {
pc, fileName, line, ok := runtime.Caller(skip + 1)
if !ok {
return ""
}
funcName := runtime.FuncForPC(pc).Name()
funcName = filepath.Base(funcName)
fileName = getRelativeDirectory(fileName)
return fmt.Sprintf("%s:%d %s %s", fileName, line, funcName, fmt.Sprint(a...))
}