json_logger.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. package main
  17. import (
  18. "encoding/json"
  19. "io"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/math"
  23. "github.com/ethereum/go-ethereum/core/vm"
  24. )
  25. type JSONLogger struct {
  26. encoder *json.Encoder
  27. }
  28. func NewJSONLogger(writer io.Writer) *JSONLogger {
  29. return &JSONLogger{json.NewEncoder(writer)}
  30. }
  31. // CaptureState outputs state information on the logger.
  32. 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 {
  33. return l.encoder.Encode(vm.StructLog{
  34. Pc: pc,
  35. Op: op,
  36. Gas: gas + cost,
  37. GasCost: cost,
  38. Memory: memory.Data(),
  39. Stack: stack.Data(),
  40. Storage: nil,
  41. Depth: depth,
  42. Err: err,
  43. })
  44. }
  45. // CaptureEnd is triggered at end of execution.
  46. func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration) error {
  47. type endLog struct {
  48. Output string `json:"output"`
  49. GasUsed math.HexOrDecimal64 `json:"gasUsed"`
  50. Time time.Duration `json:"time"`
  51. }
  52. return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t})
  53. }