logger_json.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(env *EVM, from, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
  39. }
  40. func (l *JSONLogger) CaptureFault(*EVM, uint64, OpCode, uint64, uint64, *ScopeContext, int, error) {}
  41. // CaptureState outputs state information on the logger.
  42. func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
  43. memory := scope.Memory
  44. stack := scope.Stack
  45. log := StructLog{
  46. Pc: pc,
  47. Op: op,
  48. Gas: gas,
  49. GasCost: cost,
  50. MemorySize: memory.Len(),
  51. Depth: depth,
  52. RefundCounter: env.StateDB.GetRefund(),
  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. if !l.cfg.DisableReturnData {
  62. log.ReturnData = rData
  63. }
  64. l.encoder.Encode(log)
  65. }
  66. // CaptureEnd is triggered at end of execution.
  67. func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err 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. l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
  76. }
  77. l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
  78. }