Skip to content

Commit

Permalink
feat: add string buffer for dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
mortal123 authored and HanWang233 committed Jul 13, 2022
1 parent 09a43ca commit 1f16c0a
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions core/vm/dump_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type ParityLogContext struct {

type ParityLogger struct {
context *ParityLogContext
sb *strings.Builder
encoder *json.Encoder
activePrecompiles []common.Address
file *os.File
Expand All @@ -110,15 +111,18 @@ func NewParityLogger(ctx *ParityLogContext, blockNumber uint64, perFolder, perFi
if err != nil {
return nil, err
}

l := &ParityLogger{context: ctx, encoder: json.NewEncoder(file), file: file}
sb := &strings.Builder{}
l := &ParityLogger{context: ctx, sb: sb, encoder: json.NewEncoder(sb), file: file}
if l.context == nil {
l.context = &ParityLogContext{}
}
return l, nil
}

func (l *ParityLogger) Close() error {
if _, err := l.file.WriteString(l.sb.String()); err != nil {
return err
}
return l.file.Close()
}

Expand Down Expand Up @@ -362,7 +366,8 @@ func ReceiptDumpLogger(blockNumber uint64, perFolder, perFile uint64, receipts t
return err
}

encoder := json.NewEncoder(file)
sb := &strings.Builder{}
encoder := json.NewEncoder(sb)
for _, receipt := range receipts {
for _, log := range receipt.Logs {
err := encoder.Encode(log)
Expand All @@ -371,12 +376,16 @@ func ReceiptDumpLogger(blockNumber uint64, perFolder, perFile uint64, receipts t
}
}
}
if _, err := file.WriteString(sb.String()); err != nil {
return err
}
return nil
}

type TxLogger struct {
blockNumber uint64
blockHash common.Hash
sb *strings.Builder
file *os.File
encoder *json.Encoder
signer types.Signer
Expand All @@ -387,11 +396,13 @@ func NewTxLogger(signer types.Signer, blockHash common.Hash, blockNumber uint64,
if err != nil {
return nil, err
}
sb := &strings.Builder{}
return &TxLogger{
blockNumber: blockNumber,
blockHash: blockHash,
file: file,
encoder: json.NewEncoder(file),
sb: sb,
encoder: json.NewEncoder(sb),
signer: signer,
}, nil
}
Expand Down Expand Up @@ -431,6 +442,9 @@ func (t *TxLogger) Dump(index int, tx *types.Transaction, receipt *types.Receipt
}

func (t *TxLogger) Close() error {
if _, err := t.file.WriteString(t.sb.String()); err != nil {
return err
}
return t.file.Close()
}

Expand Down

0 comments on commit 1f16c0a

Please sign in to comment.