-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
43 lines (37 loc) · 854 Bytes
/
util.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
package evo
import (
"fmt"
"github.com/fatih/color"
"path/filepath"
"runtime"
"time"
)
func Register(registrable ...interface{}) {
for _, fn := range registrable {
var err = CallFn(fn)
if err != nil {
Panic(err)
}
}
}
func CallFn(fn interface{}) error {
if v, ok := fn.(func()); ok {
v()
return nil
} else if v, ok := fn.(func() error); ok {
return v()
}
return fmt.Errorf("invalid function %+v", fn)
}
func Trace(obj interface{}, i int) {
fmt.Println(STrace(obj, i+1))
}
func STrace(obj interface{}, i int) string {
_, file, line, _ := runtime.Caller(i)
var path = filepath.Base(filepath.Dir(file)) + "/" + filepath.Base(file)
return fmt.Sprint(time.Now().Format("2006-01-02 15:04:06 "), path+":"+fmt.Sprint(line)+":", fmt.Sprintf("%+v", obj))
}
func Panic(obj interface{}) {
color.Red(STrace(obj, 2))
panic(obj)
}