123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- // Copyright 2014 beego Author. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package utils
- import (
- "bytes"
- "fmt"
- "log"
- "reflect"
- "runtime"
- )
- var (
- dunno = []byte("???")
- centerDot = []byte("·")
- dot = []byte(".")
- )
- type pointerInfo struct {
- prev *pointerInfo
- n int
- addr uintptr
- pos int
- used []int
- }
- // Display print the data in console
- func Display(data ...interface{}) {
- display(true, data...)
- }
- // GetDisplayString return data print string
- func GetDisplayString(data ...interface{}) string {
- return display(false, data...)
- }
- func display(displayed bool, data ...interface{}) string {
- var pc, file, line, ok = runtime.Caller(2)
- if !ok {
- return ""
- }
- var buf = new(bytes.Buffer)
- fmt.Fprintf(buf, "[Debug] at %s() [%s:%d]\n", function(pc), file, line)
- fmt.Fprintf(buf, "\n[Variables]\n")
- for i := 0; i < len(data); i += 2 {
- var output = fomateinfo(len(data[i].(string))+3, data[i+1])
- fmt.Fprintf(buf, "%s = %s", data[i], output)
- }
- if displayed {
- log.Print(buf)
- }
- return buf.String()
- }
- // return data dump and format bytes
- func fomateinfo(headlen int, data ...interface{}) []byte {
- var buf = new(bytes.Buffer)
- if len(data) > 1 {
- fmt.Fprint(buf, " ")
- fmt.Fprint(buf, "[")
- fmt.Fprintln(buf)
- }
- for k, v := range data {
- var buf2 = new(bytes.Buffer)
- var pointers *pointerInfo
- var interfaces = make([]reflect.Value, 0, 10)
- printKeyValue(buf2, reflect.ValueOf(v), &pointers, &interfaces, nil, true, " ", 1)
- if k < len(data)-1 {
- fmt.Fprint(buf2, ", ")
- }
- fmt.Fprintln(buf2)
- buf.Write(buf2.Bytes())
- }
- if len(data) > 1 {
- fmt.Fprintln(buf)
- fmt.Fprint(buf, " ")
- fmt.Fprint(buf, "]")
- }
- return buf.Bytes()
- }
- // check data is golang basic type
- func isSimpleType(val reflect.Value, kind reflect.Kind, pointers **pointerInfo, interfaces *[]reflect.Value) bool {
- switch kind {
- case reflect.Bool:
- return true
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return true
- case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64:
- return true
- case reflect.Float32, reflect.Float64:
- return true
- case reflect.Complex64, reflect.Complex128:
- return true
- case reflect.String:
- return true
- case reflect.Chan:
- return true
- case reflect.Invalid:
- return true
- case reflect.Interface:
- for _, in := range *interfaces {
- if reflect.DeepEqual(in, val) {
- return true
- }
- }
- return false
- case reflect.UnsafePointer:
- if val.IsNil() {
- return true
- }
- var elem = val.Elem()
- if isSimpleType(elem, elem.Kind(), pointers, interfaces) {
- return true
- }
- var addr = val.Elem().UnsafeAddr()
- for p := *pointers; p != nil; p = p.prev {
- if addr == p.addr {
- return true
- }
- }
- return false
- }
- return false
- }
- // dump value
- func printKeyValue(buf *bytes.Buffer, val reflect.Value, pointers **pointerInfo, interfaces *[]reflect.Value, structFilter func(string, string) bool, formatOutput bool, indent string, level int) {
- var t = val.Kind()
- switch t {
- case reflect.Bool:
- fmt.Fprint(buf, val.Bool())
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- fmt.Fprint(buf, val.Int())
- case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64:
- fmt.Fprint(buf, val.Uint())
- case reflect.Float32, reflect.Float64:
- fmt.Fprint(buf, val.Float())
- case reflect.Complex64, reflect.Complex128:
- fmt.Fprint(buf, val.Complex())
- case reflect.UnsafePointer:
- fmt.Fprintf(buf, "unsafe.Pointer(0x%X)", val.Pointer())
- case reflect.Ptr:
- if val.IsNil() {
- fmt.Fprint(buf, "nil")
- return
- }
- var addr = val.Elem().UnsafeAddr()
- for p := *pointers; p != nil; p = p.prev {
- if addr == p.addr {
- p.used = append(p.used, buf.Len())
- fmt.Fprintf(buf, "0x%X", addr)
- return
- }
- }
- *pointers = &pointerInfo{
- prev: *pointers,
- addr: addr,
- pos: buf.Len(),
- used: make([]int, 0),
- }
- fmt.Fprint(buf, "&")
- printKeyValue(buf, val.Elem(), pointers, interfaces, structFilter, formatOutput, indent, level)
- case reflect.String:
- fmt.Fprint(buf, "\"", val.String(), "\"")
- case reflect.Interface:
- var value = val.Elem()
- if !value.IsValid() {
- fmt.Fprint(buf, "nil")
- } else {
- for _, in := range *interfaces {
- if reflect.DeepEqual(in, val) {
- fmt.Fprint(buf, "repeat")
- return
- }
- }
- *interfaces = append(*interfaces, val)
- printKeyValue(buf, value, pointers, interfaces, structFilter, formatOutput, indent, level+1)
- }
- case reflect.Struct:
- var t = val.Type()
- fmt.Fprint(buf, t)
- fmt.Fprint(buf, "{")
- for i := 0; i < val.NumField(); i++ {
- if formatOutput {
- fmt.Fprintln(buf)
- } else {
- fmt.Fprint(buf, " ")
- }
- var name = t.Field(i).Name
- if formatOutput {
- for ind := 0; ind < level; ind++ {
- fmt.Fprint(buf, indent)
- }
- }
- fmt.Fprint(buf, name)
- fmt.Fprint(buf, ": ")
- if structFilter != nil && structFilter(t.String(), name) {
- fmt.Fprint(buf, "ignore")
- } else {
- printKeyValue(buf, val.Field(i), pointers, interfaces, structFilter, formatOutput, indent, level+1)
- }
- fmt.Fprint(buf, ",")
- }
- if formatOutput {
- fmt.Fprintln(buf)
- for ind := 0; ind < level-1; ind++ {
- fmt.Fprint(buf, indent)
- }
- } else {
- fmt.Fprint(buf, " ")
- }
- fmt.Fprint(buf, "}")
- case reflect.Array, reflect.Slice:
- fmt.Fprint(buf, val.Type())
- fmt.Fprint(buf, "{")
- var allSimple = true
- for i := 0; i < val.Len(); i++ {
- var elem = val.Index(i)
- var isSimple = isSimpleType(elem, elem.Kind(), pointers, interfaces)
- if !isSimple {
- allSimple = false
- }
- if formatOutput && !isSimple {
- fmt.Fprintln(buf)
- } else {
- fmt.Fprint(buf, " ")
- }
- if formatOutput && !isSimple {
- for ind := 0; ind < level; ind++ {
- fmt.Fprint(buf, indent)
- }
- }
- printKeyValue(buf, elem, pointers, interfaces, structFilter, formatOutput, indent, level+1)
- if i != val.Len()-1 || !allSimple {
- fmt.Fprint(buf, ",")
- }
- }
- if formatOutput && !allSimple {
- fmt.Fprintln(buf)
- for ind := 0; ind < level-1; ind++ {
- fmt.Fprint(buf, indent)
- }
- } else {
- fmt.Fprint(buf, " ")
- }
- fmt.Fprint(buf, "}")
- case reflect.Map:
- var t = val.Type()
- var keys = val.MapKeys()
- fmt.Fprint(buf, t)
- fmt.Fprint(buf, "{")
- var allSimple = true
- for i := 0; i < len(keys); i++ {
- var elem = val.MapIndex(keys[i])
- var isSimple = isSimpleType(elem, elem.Kind(), pointers, interfaces)
- if !isSimple {
- allSimple = false
- }
- if formatOutput && !isSimple {
- fmt.Fprintln(buf)
- } else {
- fmt.Fprint(buf, " ")
- }
- if formatOutput && !isSimple {
- for ind := 0; ind <= level; ind++ {
- fmt.Fprint(buf, indent)
- }
- }
- printKeyValue(buf, keys[i], pointers, interfaces, structFilter, formatOutput, indent, level+1)
- fmt.Fprint(buf, ": ")
- printKeyValue(buf, elem, pointers, interfaces, structFilter, formatOutput, indent, level+1)
- if i != val.Len()-1 || !allSimple {
- fmt.Fprint(buf, ",")
- }
- }
- if formatOutput && !allSimple {
- fmt.Fprintln(buf)
- for ind := 0; ind < level-1; ind++ {
- fmt.Fprint(buf, indent)
- }
- } else {
- fmt.Fprint(buf, " ")
- }
- fmt.Fprint(buf, "}")
- case reflect.Chan:
- fmt.Fprint(buf, val.Type())
- case reflect.Invalid:
- fmt.Fprint(buf, "invalid")
- default:
- fmt.Fprint(buf, "unknow")
- }
- }
- // PrintPointerInfo dump pointer value
- func PrintPointerInfo(buf *bytes.Buffer, headlen int, pointers *pointerInfo) {
- var anyused = false
- var pointerNum = 0
- for p := pointers; p != nil; p = p.prev {
- if len(p.used) > 0 {
- anyused = true
- }
- pointerNum++
- p.n = pointerNum
- }
- if anyused {
- var pointerBufs = make([][]rune, pointerNum+1)
- for i := 0; i < len(pointerBufs); i++ {
- var pointerBuf = make([]rune, buf.Len()+headlen)
- for j := 0; j < len(pointerBuf); j++ {
- pointerBuf[j] = ' '
- }
- pointerBufs[i] = pointerBuf
- }
- for pn := 0; pn <= pointerNum; pn++ {
- for p := pointers; p != nil; p = p.prev {
- if len(p.used) > 0 && p.n >= pn {
- if pn == p.n {
- pointerBufs[pn][p.pos+headlen] = '└'
- var maxpos = 0
- for i, pos := range p.used {
- if i < len(p.used)-1 {
- pointerBufs[pn][pos+headlen] = '┴'
- } else {
- pointerBufs[pn][pos+headlen] = '┘'
- }
- maxpos = pos
- }
- for i := 0; i < maxpos-p.pos-1; i++ {
- if pointerBufs[pn][i+p.pos+headlen+1] == ' ' {
- pointerBufs[pn][i+p.pos+headlen+1] = '─'
- }
- }
- } else {
- pointerBufs[pn][p.pos+headlen] = '│'
- for _, pos := range p.used {
- if pointerBufs[pn][pos+headlen] == ' ' {
- pointerBufs[pn][pos+headlen] = '│'
- } else {
- pointerBufs[pn][pos+headlen] = '┼'
- }
- }
- }
- }
- }
- buf.WriteString(string(pointerBufs[pn]) + "\n")
- }
- }
- }
- // Stack get stack bytes
- func Stack(skip int, indent string) []byte {
- var buf = new(bytes.Buffer)
- for i := skip; ; i++ {
- var pc, file, line, ok = runtime.Caller(i)
- if !ok {
- break
- }
- buf.WriteString(indent)
- fmt.Fprintf(buf, "at %s() [%s:%d]\n", function(pc), file, line)
- }
- return buf.Bytes()
- }
- // return the name of the function containing the PC if possible,
- func function(pc uintptr) []byte {
- fn := runtime.FuncForPC(pc)
- if fn == nil {
- return dunno
- }
- name := []byte(fn.Name())
- // The name includes the path name to the package, which is unnecessary
- // since the file name is already included. Plus, it has center dots.
- // That is, we see
- // runtime/debug.*T·ptrmethod
- // and want
- // *T.ptrmethod
- if period := bytes.Index(name, dot); period >= 0 {
- name = name[period+1:]
- }
- name = bytes.Replace(name, centerDot, dot, -1)
- return name
- }
|