github.com/wzzhu/tensor@v0.9.24/debug.go (about) 1 // +build debug 2 3 package tensor 4 5 import ( 6 "fmt" 7 "log" 8 "os" 9 "reflect" 10 "runtime/debug" 11 "strings" 12 "sync/atomic" 13 "unsafe" 14 ) 15 16 var TABCOUNT uint32 17 18 var TRACK = false 19 20 const DEBUG = true 21 22 var _logger_ = log.New(os.Stderr, "", 0) 23 var replacement = "\n" 24 25 func tabcount() int { 26 return int(atomic.LoadUint32(&TABCOUNT)) 27 } 28 29 func enterLoggingContext() { 30 atomic.AddUint32(&TABCOUNT, 1) 31 tabcount := tabcount() 32 _logger_.SetPrefix(strings.Repeat("\t", tabcount)) 33 replacement = "\n" + strings.Repeat("\t", tabcount) 34 } 35 36 func leaveLoggingContext() { 37 tabcount := tabcount() 38 tabcount-- 39 40 if tabcount < 0 { 41 atomic.StoreUint32(&TABCOUNT, 0) 42 tabcount = 0 43 } else { 44 atomic.StoreUint32(&TABCOUNT, uint32(tabcount)) 45 } 46 _logger_.SetPrefix(strings.Repeat("\t", tabcount)) 47 replacement = "\n" + strings.Repeat("\t", tabcount) 48 } 49 50 func logf(format string, others ...interface{}) { 51 if DEBUG { 52 // format = strings.Replace(format, "\n", replacement, -1) 53 s := fmt.Sprintf(format, others...) 54 s = strings.Replace(s, "\n", replacement, -1) 55 _logger_.Println(s) 56 // _logger_.Printf(format, others...) 57 } 58 } 59 60 var stats = new(debug.GCStats) 61 62 func loggc() { 63 debug.ReadGCStats(stats) 64 log.Printf("NUMGC: %v", stats.NumGC) 65 } 66 67 func init() { 68 debug.SetPanicOnFault(true) 69 debug.SetTraceback("all") 70 } 71 72 type rtype struct { 73 size uintptr 74 ptrdata uintptr // number of bytes in the type that can contain pointers 75 hash uint32 // hash of type; avoids computation in hash tables 76 tflag uint8 // extra type information flags 77 align uint8 // alignment of variable with this type 78 fieldAlign uint8 // alignment of struct field with this type 79 kind uint8 // enumeration for C 80 alg uintptr // algorithm table 81 gcdata uintptr // garbage collection data 82 str int32 // string form 83 ptrToThis int32 // type for pointer to this type, may be zero 84 } 85 86 func (t *rtype) Format(s fmt.State, c rune) { 87 fmt.Fprintf(s, "size: %d pointers: %d, hash: 0x%x, flag: %d, align: %d, kind: %d", t.size, t.ptrdata, t.hash, t.tflag, t.align, t.kind) 88 } 89 90 func logRtype(t *reflect.Type) { 91 iface := *(*[2]uintptr)(unsafe.Pointer(t)) 92 rt := (*rtype)(unsafe.Pointer(iface[1])) 93 log.Printf("TYPE INFO: %v(%p) - %v", *t, t, rt) 94 }