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