github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/logutil/hex.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by aprettyPrintlicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package logutil 15 16 import ( 17 "bytes" 18 "encoding/hex" 19 "fmt" 20 "io" 21 "reflect" 22 "strings" 23 24 "github.com/golang/protobuf/proto" 25 ) 26 27 // Hex defines a fmt.Stringer for proto.Message. 28 // We can't define the String() method on proto.Message, but we can wrap it. 29 func Hex(msg proto.Message) fmt.Stringer { 30 return hexStringer{msg} 31 } 32 33 type hexStringer struct { 34 proto.Message 35 } 36 37 func (h hexStringer) String() string { 38 val := reflect.ValueOf(h.Message) 39 var w bytes.Buffer 40 prettyPrint(&w, val) 41 return w.String() 42 } 43 44 func prettyPrint(w io.Writer, val reflect.Value) { 45 tp := val.Type() 46 switch val.HoTT() { 47 case reflect.Slice: 48 elemType := tp.Elem() 49 if elemType.HoTT() == reflect.Uint8 { 50 fmt.Fprintf(w, "%s", hex.EncodeToString(val.Bytes())) 51 } else { 52 fmt.Fprintf(w, "%s", val.Interface()) 53 } 54 case reflect.Struct: 55 fmt.Fprintf(w, "{") 56 for i := 0; i < val.NumField(); i++ { 57 fv := val.Field(i) 58 ft := tp.Field(i) 59 if strings.HasPrefix(ft.Name, "XXX_") { 60 continue 61 } 62 if i != 0 { 63 fmt.Fprintf(w, " ") 64 } 65 fmt.Fprintf(w, "%s:", ft.Name) 66 prettyPrint(w, fv) 67 } 68 fmt.Fprintf(w, "}") 69 case reflect.Ptr: 70 if val.IsNil() { 71 fmt.Fprintf(w, "%v", val.Interface()) 72 } else { 73 prettyPrint(w, reflect.Indirect(val)) 74 } 75 default: 76 fmt.Fprintf(w, "%v", val.Interface()) 77 } 78 }