json_logger.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "encoding/json"
  19. "io"
  20. "math/big"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/common/math"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. )
  26. type JSONLogger struct {
  27. encoder *json.Encoder
  28. cfg *vm.LogConfig
  29. }
  30. func NewJSONLogger(cfg *vm.LogConfig, writer io.Writer) *JSONLogger {
  31. return &JSONLogger{json.NewEncoder(writer), cfg}
  32. }
  33. func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
  34. return nil
  35. }
  36. // CaptureState outputs state information on the logger.
  37. 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 {
  38. log := vm.StructLog{
  39. Pc: pc,
  40. Op: op,
  41. Gas: gas,
  42. GasCost: cost,
  43. MemorySize: memory.Len(),
  44. Storage: nil,
  45. Depth: depth,
  46. Err: err,
  47. }
  48. if !l.cfg.DisableMemory {
  49. log.Memory = memory.Data()
  50. }
  51. if !l.cfg.DisableStack {
  52. log.Stack = stack.Data()
  53. }
  54. return l.encoder.Encode(log)
  55. }
  56. // CaptureFault outputs state information on the logger.
  57. 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 {
  58. return nil
  59. }
  60. // CaptureEnd is triggered at end of execution.
  61. func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
  62. type endLog struct {
  63. Output string `json:"output"`
  64. GasUsed math.HexOrDecimal64 `json:"gasUsed"`
  65. Time time.Duration `json:"time"`
  66. Err string `json:"error,omitempty"`
  67. }
  68. if err != nil {
  69. return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
  70. }
  71. return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
  72. }