logger_json.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 vm
  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. )
  25. type JSONLogger struct {
  26. encoder *json.Encoder
  27. cfg *LogConfig
  28. }
  29. // NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects
  30. // into the provided stream.
  31. func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger {
  32. l := &JSONLogger{json.NewEncoder(writer), cfg}
  33. if l.cfg == nil {
  34. l.cfg = &LogConfig{}
  35. }
  36. return l
  37. }
  38. func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
  39. return nil
  40. }
  41. // CaptureState outputs state information on the logger.
  42. func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
  43. log := StructLog{
  44. Pc: pc,
  45. Op: op,
  46. Gas: gas,
  47. GasCost: cost,
  48. MemorySize: memory.Len(),
  49. Storage: nil,
  50. Depth: depth,
  51. RefundCounter: env.StateDB.GetRefund(),
  52. Err: err,
  53. }
  54. if !l.cfg.DisableMemory {
  55. log.Memory = memory.Data()
  56. }
  57. if !l.cfg.DisableStack {
  58. log.Stack = stack.Data()
  59. }
  60. return l.encoder.Encode(log)
  61. }
  62. // CaptureFault outputs state information on the logger.
  63. func (l *JSONLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
  64. return nil
  65. }
  66. // CaptureEnd is triggered at end of execution.
  67. func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
  68. type endLog struct {
  69. Output string `json:"output"`
  70. GasUsed math.HexOrDecimal64 `json:"gasUsed"`
  71. Time time.Duration `json:"time"`
  72. Err string `json:"error,omitempty"`
  73. }
  74. if err != nil {
  75. return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
  76. }
  77. return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
  78. }