github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/common/tracedbinary/binary.go (about)

     1  // Copyright 2017-2021 the LinuxBoot Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package tracedbinary
     6  
     7  import (
     8  	"encoding/binary"
     9  	"fmt"
    10  	"io"
    11  	"path/filepath"
    12  	"reflect"
    13  	"runtime"
    14  )
    15  
    16  type ByteOrder = binary.ByteOrder
    17  
    18  var (
    19  	LittleEndian = binary.LittleEndian
    20  )
    21  
    22  func Read(r io.Reader, order ByteOrder, data interface{}) error {
    23  	err := binary.Read(r, order, data)
    24  	v := reflect.Indirect(reflect.ValueOf(data))
    25  	switch {
    26  	case v.Kind() != reflect.Slice || v.Len() < 16:
    27  		fmt.Printf("%s: binary.Read(%T, %s, %T) -> %v; data == %v\n", caller(), r, order, data, err, v.Interface())
    28  	case v.Kind() == reflect.Slice:
    29  		fmt.Printf("%s: binary.Read(%T, %s, %T) -> %v; len(data) == %v\n", caller(), r, order, data, err, v.Len())
    30  	default:
    31  		fmt.Printf("%s: binary.Read(%T, %s, %T) -> %v\n", caller(), r, order, data, err)
    32  	}
    33  	return err
    34  }
    35  
    36  func Write(w io.Writer, order ByteOrder, data interface{}) error {
    37  	err := binary.Write(w, order, data)
    38  	fmt.Printf("%s: binary.Read(%T, %s, %T) -> %v\n", caller(), w, order, data, err)
    39  	return err
    40  }
    41  
    42  func Size(v interface{}) int {
    43  	r := binary.Size(v)
    44  	fmt.Printf("%s: binary.Size(%T) -> %v\n", caller(), v, r)
    45  	return r
    46  }
    47  
    48  func caller() string {
    49  	_, file, line, _ := runtime.Caller(2)
    50  	return fmt.Sprintf("%s:%d", filepath.Base(file), line)
    51  }