github.com/KinWaiYuen/client-go/v2@v2.5.4/internal/logutil/hex.go (about)

     1  // Copyright 2021 TiKV Authors
     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 applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // NOTE: The code in this file is based on code from the
    16  // TiDB project, licensed under the Apache License v 2.0
    17  //
    18  // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/logutil/hex.go
    19  //
    20  
    21  // Copyright 2021 PingCAP, Inc.
    22  //
    23  // Licensed under the Apache License, Version 2.0 (the "License");
    24  // you may not use this file except in compliance with the License.
    25  // You may obtain a copy of the License at
    26  //
    27  //     http://www.apache.org/licenses/LICENSE-2.0
    28  //
    29  // Unless required by applicable law or agreed to in writing, software
    30  // distributed under the License is distributed on an "AS IS" BASIS,
    31  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    32  // See the License for the specific language governing permissions and
    33  // limitations under the License.
    34  
    35  package logutil
    36  
    37  import (
    38  	"bytes"
    39  	"encoding/hex"
    40  	"fmt"
    41  	"io"
    42  	"reflect"
    43  	"strings"
    44  
    45  	"github.com/golang/protobuf/proto"
    46  )
    47  
    48  // Hex defines a fmt.Stringer for proto.Message.
    49  // We can't define the String() method on proto.Message, but we can wrap it.
    50  func Hex(msg proto.Message) fmt.Stringer {
    51  	return hexStringer{msg}
    52  }
    53  
    54  type hexStringer struct {
    55  	proto.Message
    56  }
    57  
    58  func (h hexStringer) String() string {
    59  	val := reflect.ValueOf(h.Message)
    60  	var w bytes.Buffer
    61  	prettyPrint(&w, val)
    62  	return w.String()
    63  }
    64  
    65  func prettyPrint(w io.Writer, val reflect.Value) {
    66  	tp := val.Type()
    67  	switch val.Kind() {
    68  	case reflect.Slice:
    69  		elemType := tp.Elem()
    70  		if elemType.Kind() == reflect.Uint8 {
    71  			fmt.Fprintf(w, "%s", hex.EncodeToString(val.Bytes()))
    72  		} else {
    73  			fmt.Fprintf(w, "%s", val.Interface())
    74  		}
    75  	case reflect.Struct:
    76  		fmt.Fprintf(w, "{")
    77  		for i := 0; i < val.NumField(); i++ {
    78  			fv := val.Field(i)
    79  			ft := tp.Field(i)
    80  			if strings.HasPrefix(ft.Name, "XXX_") {
    81  				continue
    82  			}
    83  			if i != 0 {
    84  				fmt.Fprintf(w, " ")
    85  			}
    86  			fmt.Fprintf(w, "%s:", ft.Name)
    87  			prettyPrint(w, fv)
    88  		}
    89  		fmt.Fprintf(w, "}")
    90  	case reflect.Ptr:
    91  		if val.IsNil() {
    92  			fmt.Fprintf(w, "%v", val.Interface())
    93  		} else {
    94  			prettyPrint(w, reflect.Indirect(val))
    95  		}
    96  	default:
    97  		fmt.Fprintf(w, "%v", val.Interface())
    98  	}
    99  }