github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/core/wavm/logger.go (about)

     1  package wavm
     2  
     3  import (
     4  	"io"
     5  	"math/big"
     6  	"time"
     7  
     8  	"github.com/vntchain/go-vnt/common"
     9  	"github.com/vntchain/go-vnt/common/math"
    10  	"github.com/vntchain/go-vnt/core/vm"
    11  	"github.com/vntchain/go-vnt/core/vm/interface"
    12  )
    13  
    14  type WasmLogger struct {
    15  	cfg       vm.LogConfig
    16  	logs      []StructLog
    17  	debugLogs []DebugLog
    18  	output    []byte
    19  	err       error
    20  }
    21  
    22  type StructLog struct {
    23  	Pc         uint64                      `json:"pc"`
    24  	Op         vm.OPCode                   `json:"op"`
    25  	Gas        uint64                      `json:"gas"`
    26  	GasCost    uint64                      `json:"gasCost"`
    27  	Memory     []byte                      `json:"-"`
    28  	MemorySize int                         `json:"-"`
    29  	Stack      []*big.Int                  `json:"-"`
    30  	Storage    map[common.Hash]common.Hash `json:"-"`
    31  	Depth      int                         `json:"depth"`
    32  	Err        error                       `json:"error"`
    33  }
    34  
    35  type DebugLog struct {
    36  	PrintMsg string `json:"printMsg"`
    37  }
    38  
    39  // overrides for gencodec
    40  type structLogMarshaling struct {
    41  	Gas         math.HexOrDecimal64
    42  	GasCost     math.HexOrDecimal64
    43  	OpName      string `json:"opName"` // adds call to OpName() in MarshalJSON
    44  	ErrorString string `json:"error"`  // adds call to ErrorString() in MarshalJSON
    45  }
    46  
    47  func (s *StructLog) OpName() string {
    48  	return s.Op.String()
    49  }
    50  
    51  func (s *StructLog) ErrorString() string {
    52  	if s.Err != nil {
    53  		return s.Err.Error()
    54  	}
    55  	return ""
    56  }
    57  
    58  func NewWasmLogger(cfg *vm.LogConfig) *WasmLogger {
    59  	logger := &WasmLogger{}
    60  	if cfg != nil {
    61  		logger.cfg = *cfg
    62  	}
    63  	return logger
    64  }
    65  
    66  func (l *WasmLogger) CaptureStart(from common.Address, to common.Address, call bool, input []byte, gas uint64, value *big.Int) error {
    67  	return nil
    68  }
    69  func (l *WasmLogger) CaptureState(env vm.VM, pc uint64, op vm.OPCode, gas, cost uint64, contract inter.Contract, depth int, err error) error {
    70  	// check if already accumulated the specified number of logs
    71  	if l.cfg.Limit != 0 && l.cfg.Limit <= len(l.logs) {
    72  		return vm.ErrTraceLimitReached
    73  	}
    74  
    75  	// create a new snaptshot of the VM.
    76  	log := StructLog{pc, op, gas, cost, nil, 0, nil, nil, depth, err}
    77  
    78  	l.logs = append(l.logs, log)
    79  	return nil
    80  }
    81  func (l *WasmLogger) CaptureLog(env vm.VM, msg string) error {
    82  	log := DebugLog{msg}
    83  	l.debugLogs = append(l.debugLogs, log)
    84  	return nil
    85  }
    86  func (l *WasmLogger) CaptureFault(env vm.VM, pc uint64, op vm.OPCode, gas, cost uint64, contract inter.Contract, depth int, err error) error {
    87  	return nil
    88  }
    89  func (l *WasmLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
    90  	return nil
    91  }
    92  
    93  // Error returns the VM error captured by the trace.
    94  func (l *WasmLogger) Error() error { return l.err }
    95  
    96  // Output returns the VM return value captured by the trace.
    97  func (l *WasmLogger) Output() []byte { return l.output }
    98  
    99  // WasmLogger returns the captured log entries.
   100  func (l *WasmLogger) StructLogs() []StructLog { return l.logs }
   101  
   102  // DebugLogs returns the captured debug log entries.
   103  func (l *WasmLogger) DebugLogs() []DebugLog { return l.debugLogs }
   104  
   105  // WriteTrace writes a formatted trace to the given writer
   106  func WriteTrace(writer io.Writer, logs []StructLog) {
   107  
   108  }