2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
|
|
|
|
|
|
2018-01-03 10:38:53 +08:00
|
|
|
|
// 其他工具包
|
2017-11-23 10:21:28 +08:00
|
|
|
|
package gutil
|
|
|
|
|
|
|
2018-04-29 21:33:47 +08:00
|
|
|
|
import (
|
2018-07-24 22:00:51 +08:00
|
|
|
|
"fmt"
|
2018-07-31 21:05:02 +08:00
|
|
|
|
"bytes"
|
2018-09-14 13:56:01 +08:00
|
|
|
|
"encoding/json"
|
2018-09-16 10:51:02 +08:00
|
|
|
|
"reflect"
|
|
|
|
|
|
"gitee.com/johng/gf/g/util/gconv"
|
2018-10-18 13:43:00 +08:00
|
|
|
|
"runtime"
|
|
|
|
|
|
"gitee.com/johng/gf/g/os/glog"
|
2018-04-29 21:33:47 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2018-07-24 22:00:51 +08:00
|
|
|
|
// 格式化打印变量(类似于PHP-vardump)
|
2018-08-11 21:09:42 +08:00
|
|
|
|
func Dump(i...interface{}) {
|
|
|
|
|
|
for _, v := range i {
|
2018-09-14 13:56:01 +08:00
|
|
|
|
if b, ok := v.([]byte); ok {
|
|
|
|
|
|
fmt.Print(string(b))
|
2018-08-11 21:09:42 +08:00
|
|
|
|
} else {
|
2018-09-16 10:51:02 +08:00
|
|
|
|
// 主要针对 map[interface{}]* 进行处理,json无法进行encode,
|
|
|
|
|
|
// 这里强制对所有map进行反射处理转换
|
|
|
|
|
|
refValue := reflect.ValueOf(v)
|
|
|
|
|
|
if refValue.Kind() == reflect.Map {
|
|
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
|
|
keys := refValue.MapKeys()
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
|
|
key := gconv.String(k.Interface())
|
|
|
|
|
|
m[key] = refValue.MapIndex(k).Interface()
|
|
|
|
|
|
}
|
|
|
|
|
|
v = m
|
|
|
|
|
|
}
|
|
|
|
|
|
// json encode并打印到终端
|
2018-09-14 13:56:01 +08:00
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
|
|
encoder := json.NewEncoder(buffer)
|
|
|
|
|
|
encoder.SetEscapeHTML(false)
|
|
|
|
|
|
encoder.SetIndent("", "\t")
|
|
|
|
|
|
if err := encoder.Encode(v); err == nil {
|
|
|
|
|
|
fmt.Print(buffer.String())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
fmt.Errorf("%s", err.Error())
|
|
|
|
|
|
}
|
2018-08-11 21:09:42 +08:00
|
|
|
|
}
|
2018-09-17 09:52:24 +08:00
|
|
|
|
//fmt.Println()
|
2018-07-24 22:00:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-18 13:43:00 +08:00
|
|
|
|
// 打印完整的调用回溯信息
|
|
|
|
|
|
func PrintBacktrace() {
|
|
|
|
|
|
index := 1
|
|
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
|
|
if _, cfile, cline, ok := runtime.Caller(i); ok {
|
|
|
|
|
|
buffer.WriteString(fmt.Sprintf(`%d. %s:%d%s`, index, cfile, cline, "\n"))
|
|
|
|
|
|
index++
|
|
|
|
|
|
} else {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
glog.Header(false).Print(buffer.String())
|
|
|
|
|
|
}
|
|
|
|
|
|
|