mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
refactor logging
This commit is contained in:
parent
f65a19af15
commit
e67c3bfd62
1 changed files with 22 additions and 22 deletions
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
|
@ -14,6 +13,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/core/vm"
|
"github.com/ethereum/go-ethereum/core/vm"
|
||||||
"github.com/ethereum/go-ethereum/eth/tracers"
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"gopkg.in/natefinch/lumberjack.v2"
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -44,10 +44,9 @@ type supplyTxCallstack struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type supply struct {
|
type supply struct {
|
||||||
delta supplyInfo
|
delta supplyInfo
|
||||||
txCallstack []supplyTxCallstack // Callstack for current transaction
|
txCallstack []supplyTxCallstack // Callstack for current transaction
|
||||||
loggerOutputFile *lumberjack.Logger
|
logger *lumberjack.Logger
|
||||||
logger *log.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type supplyTracerConfig struct {
|
type supplyTracerConfig struct {
|
||||||
|
|
@ -62,28 +61,21 @@ func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) {
|
||||||
return nil, fmt.Errorf("failed to parse config: %v", err)
|
return nil, fmt.Errorf("failed to parse config: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Path == "" {
|
if config.Path == "" {
|
||||||
return nil, errors.New("supply tracer output path is required")
|
return nil, errors.New("supply tracer output path is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store traces in a rotating file
|
// Store traces in a rotating file
|
||||||
loggerOutputFile := &lumberjack.Logger{
|
logger := &lumberjack.Logger{
|
||||||
Filename: filepath.Join(config.Path, "supply.jsonl"),
|
Filename: filepath.Join(config.Path, "supply.jsonl"),
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.MaxSize > 0 {
|
if config.MaxSize > 0 {
|
||||||
loggerOutputFile.MaxSize = config.MaxSize
|
logger.MaxSize = config.MaxSize
|
||||||
}
|
}
|
||||||
|
|
||||||
logger := log.New(loggerOutputFile, "", 0)
|
|
||||||
|
|
||||||
supplyInfo := newSupplyInfo()
|
|
||||||
|
|
||||||
t := &supply{
|
t := &supply{
|
||||||
delta: supplyInfo,
|
delta: newSupplyInfo(),
|
||||||
loggerOutputFile: loggerOutputFile,
|
logger: logger,
|
||||||
logger: logger,
|
|
||||||
}
|
}
|
||||||
return &tracing.Hooks{
|
return &tracing.Hooks{
|
||||||
OnBlockStart: t.OnBlockStart,
|
OnBlockStart: t.OnBlockStart,
|
||||||
|
|
@ -138,8 +130,7 @@ func (s *supply) OnBlockStart(ev tracing.BlockEvent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *supply) OnBlockEnd(err error) {
|
func (s *supply) OnBlockEnd(err error) {
|
||||||
out, _ := json.Marshal(s.delta)
|
s.write(s.delta)
|
||||||
s.logger.Println(string(out))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *supply) OnGenesisBlock(b *types.Block, alloc types.GenesisAlloc) {
|
func (s *supply) OnGenesisBlock(b *types.Block, alloc types.GenesisAlloc) {
|
||||||
|
|
@ -154,8 +145,7 @@ func (s *supply) OnGenesisBlock(b *types.Block, alloc types.GenesisAlloc) {
|
||||||
s.delta.Delta.Add(s.delta.Delta, account.Balance)
|
s.delta.Delta.Add(s.delta.Delta, account.Balance)
|
||||||
}
|
}
|
||||||
|
|
||||||
out, _ := json.Marshal(s.delta)
|
s.write(s.delta)
|
||||||
s.logger.Println(string(out))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *supply) OnBalanceChange(a common.Address, prevBalance, newBalance *big.Int, reason tracing.BalanceChangeReason) {
|
func (s *supply) OnBalanceChange(a common.Address, prevBalance, newBalance *big.Int, reason tracing.BalanceChangeReason) {
|
||||||
|
|
@ -241,7 +231,17 @@ func (s *supply) OnExit(depth int, output []byte, gasUsed uint64, err error, rev
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *supply) OnTerminate() {
|
func (s *supply) OnTerminate() {
|
||||||
if err := s.loggerOutputFile.Close(); err != nil {
|
if err := s.logger.Close(); err != nil {
|
||||||
log.Printf("failed to close supply tracer log file: %v", err)
|
log.Warn("failed to close supply tracer log file", "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *supply) write(data any) {
|
||||||
|
out, _ := json.Marshal(data)
|
||||||
|
if _, err := s.logger.Write(out); err != nil {
|
||||||
|
log.Warn("failed to write to supply tracer log file", "error", err)
|
||||||
|
}
|
||||||
|
if _, err := s.logger.Write([]byte{'\n'}); err != nil {
|
||||||
|
log.Warn("failed to write to supply tracer log file", "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue