-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.go
66 lines (54 loc) · 1.49 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Copyright © 2022 Shubh Karman Singh <sksingh2211@gmail.com>
All rights reserved.
This Project is under BSD-3 License Clause.
Look at License for more detail.
*/
package utils
// This Package contains some helper functions for logging.
import (
"fmt"
"log"
"runtime"
"strings"
)
func LogTrace() {
log.Print(TraceMsgOffset("", 1))
}
func LogTraceMsg(msg string) {
log.Print(TraceMsgOffset(msg, 1))
}
func LogTraceMsgOffset(msg string, callStackIdxOffset int) {
log.Print(TraceMsgOffset(msg, callStackIdxOffset+1))
}
func Trace() string {
return TraceMsgOffset("", 1)
}
func TraceMsg(msg string) string {
return TraceMsgOffset(msg, 1)
}
func TraceMsgOffset(msg string, callStackIdxOffset int) string {
var traceMsg string
callStackIdx := 2 + callStackIdxOffset
pc := make([]uintptr, 15)
n := runtime.Callers(callStackIdx, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
traceMsg += fmt.Sprintf("%s:%d %s", frame.File, frame.Line, frame.Function)
if msg != "" {
traceMsg += fmt.Sprintf(" %s ", msg)
}
traceMsg += "\n"
return traceMsg
}
func LogUnimplementedFunc() {
callStackIdx := 2
pc := make([]uintptr, 15)
n := runtime.Callers(callStackIdx, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
functionNameParts := strings.Split(frame.Function, ".")
simpleFuncName := functionNameParts[len(functionNameParts)-1]
logMsg := fmt.Sprintf("TODO - Implement function %s at %s:%d", simpleFuncName, frame.File, frame.Line)
log.Print(logMsg)
}