github.com/vipernet-xyz/tm@v0.34.24/libs/log/lazy.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  
     6  	tmbytes "github.com/vipernet-xyz/tm/libs/bytes"
     7  )
     8  
     9  type LazySprintf struct {
    10  	format string
    11  	args   []interface{}
    12  }
    13  
    14  // NewLazySprintf defers fmt.Sprintf until the Stringer interface is invoked.
    15  // This is particularly useful for avoiding calling Sprintf when debugging is not
    16  // active.
    17  func NewLazySprintf(format string, args ...interface{}) *LazySprintf {
    18  	return &LazySprintf{format, args}
    19  }
    20  
    21  func (l *LazySprintf) String() string {
    22  	return fmt.Sprintf(l.format, l.args...)
    23  }
    24  
    25  type LazyBlockHash struct {
    26  	block hashable
    27  }
    28  
    29  type hashable interface {
    30  	Hash() tmbytes.HexBytes
    31  }
    32  
    33  // NewLazyBlockHash defers block Hash until the Stringer interface is invoked.
    34  // This is particularly useful for avoiding calling Sprintf when debugging is not
    35  // active.
    36  func NewLazyBlockHash(block hashable) *LazyBlockHash {
    37  	return &LazyBlockHash{block}
    38  }
    39  
    40  func (l *LazyBlockHash) String() string {
    41  	return l.block.Hash().String()
    42  }