github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/evm/json_logger.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package main
    13  
    14  import (
    15  	"encoding/json"
    16  	"io"
    17  	"math/big"
    18  	"time"
    19  
    20  	"github.com/Sberex/go-sberex/common"
    21  	"github.com/Sberex/go-sberex/common/math"
    22  	"github.com/Sberex/go-sberex/core/vm"
    23  )
    24  
    25  type JSONLogger struct {
    26  	encoder *json.Encoder
    27  	cfg     *vm.LogConfig
    28  }
    29  
    30  func NewJSONLogger(cfg *vm.LogConfig, writer io.Writer) *JSONLogger {
    31  	return &JSONLogger{json.NewEncoder(writer), cfg}
    32  }
    33  
    34  func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
    35  	return nil
    36  }
    37  
    38  // CaptureState outputs state information on the logger.
    39  func (l *JSONLogger) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
    40  	log := vm.StructLog{
    41  		Pc:         pc,
    42  		Op:         op,
    43  		Gas:        gas,
    44  		GasCost:    cost,
    45  		MemorySize: memory.Len(),
    46  		Storage:    nil,
    47  		Depth:      depth,
    48  		Err:        err,
    49  	}
    50  	if !l.cfg.DisableMemory {
    51  		log.Memory = memory.Data()
    52  	}
    53  	if !l.cfg.DisableStack {
    54  		log.Stack = stack.Data()
    55  	}
    56  	return l.encoder.Encode(log)
    57  }
    58  
    59  // CaptureFault outputs state information on the logger.
    60  func (l *JSONLogger) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
    61  	return nil
    62  }
    63  
    64  // CaptureEnd is triggered at end of execution.
    65  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
    66  	type endLog struct {
    67  		Output  string              `json:"output"`
    68  		GasUsed math.HexOrDecimal64 `json:"gasUsed"`
    69  		Time    time.Duration       `json:"time"`
    70  		Err     string              `json:"error,omitempty"`
    71  	}
    72  	if err != nil {
    73  		return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
    74  	}
    75  	return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
    76  }