github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgDebug/print.go (about)

     1  package kmgDebug
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"reflect"
     8  
     9  	"github.com/bronze1man/kmg/kmgReflect"
    10  )
    11  
    12  //提供一个漂亮的调试显示接口
    13  // TODO 根据不同类型使用不同的显示方法
    14  func Println(objList ...interface{}) {
    15  	s := Sprintln(objList...)
    16  	os.Stdout.WriteString(s)
    17  	return
    18  }
    19  
    20  func Sprintln(objList ...interface{}) string {
    21  	outList := make([]interface{}, len(objList)+1)
    22  	outList[0] = "[kmgDebug.Println]"
    23  	for i := range objList {
    24  		if kmgReflect.IsNil(reflect.ValueOf(objList[i])) {
    25  			outList[i+1] = "nil"
    26  			continue
    27  		}
    28  		switch obj := objList[i].(type) {
    29  		case []byte:
    30  			outList[i+1] = fmt.Sprintf("%#v", obj)
    31  		default:
    32  			b, err := json.MarshalIndent(objList[i], "", " ")
    33  			if err != nil {
    34  				outList[i+1] = "[Println]error:" + err.Error()
    35  				continue
    36  			}
    37  			outList[i+1] = string(b)
    38  		}
    39  
    40  	}
    41  	return fmt.Sprintln(outList...)
    42  }