github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/types/utils.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	tmlog "github.com/tendermint/tendermint/libs/log"
     6  	"reflect"
     7  	"regexp"
     8  	"runtime"
     9  	"time"
    10  )
    11  
    12  // Go lacks a simple and safe way to see if something is a typed nil.
    13  // See:
    14  //  - https://dave.cheney.net/2017/08/09/typed-nils-in-go-2
    15  //  - https://groups.google.com/forum/#!topic/golang-nuts/wnH302gBa4I/discussion
    16  //  - https://github.com/golang/go/issues/21538
    17  func isTypedNil(o interface{}) bool {
    18  	rv := reflect.ValueOf(o)
    19  	switch rv.Kind() {
    20  	case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice:
    21  		return rv.IsNil()
    22  	default:
    23  		return false
    24  	}
    25  }
    26  
    27  // Returns true if it has zero length.
    28  func isEmpty(o interface{}) bool {
    29  	rv := reflect.ValueOf(o)
    30  	switch rv.Kind() {
    31  	case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
    32  		return rv.Len() == 0
    33  	default:
    34  		return false
    35  	}
    36  }
    37  
    38  func TimeTrack(start time.Time, log tmlog.Logger) {
    39  	elapsed := time.Since(start)
    40  
    41  	// Skip this function, and fetch the PC and file for its parent.
    42  	pc, _, _, _ := runtime.Caller(1)
    43  
    44  	// Retrieve a function object this functions parent.
    45  	funcObj := runtime.FuncForPC(pc)
    46  
    47  	// Regex to extract just the function name (and not the module path).
    48  	runtimeFunc := regexp.MustCompile(`^.*\.(.*)$`)
    49  	name := runtimeFunc.ReplaceAllString(funcObj.Name(), "$1")
    50  
    51  	if log == nil {
    52  		log = tmlog.NewNopLogger()
    53  	}
    54  	log.Debug(fmt.Sprintf("%s took %s", name, elapsed))
    55  }