Skip to content

Commit e02883c

Browse files
committed
core, log: track field length and pad to align
1 parent e588e0c commit e02883c

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

‎core/blockchain.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,8 @@ func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain
798798

799799
// Report some public statistics so the user has a clue what's going on
800800
last := blockChain[len(blockChain)-1]
801-
log.Info("Imported new block receipts", "count", stats.processed, "number", last.Number(), "hash", last.Hash(),
802-
"elapsed", common.PrettyDuration(time.Since(start)), "ignored", stats.ignored)
801+
log.Info("Imported new block receipts", "count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)),
802+
"number", last.Number(), "hash", last.Hash(), "ignored", stats.ignored)
803803

804804
return 0, nil
805805
}
@@ -1054,9 +1054,9 @@ func (st *insertStats) report(chain []*types.Block, index int) {
10541054
txs = countTransactions(chain[st.lastIndex : index+1])
10551055
)
10561056
context := []interface{}{
1057-
"blocks", st.processed, "number", end.Number(), "hash", end.Hash(), "txs", txs,
1058-
"mgas", float64(st.usedGas) / 1000000, "elapsed", common.PrettyDuration(elapsed),
1059-
"mgasps", float64(st.usedGas) * 1000 / float64(elapsed),
1057+
"blocks", st.processed, "txs", txs, "mgas", float64(st.usedGas) / 1000000,
1058+
"elapsed", common.PrettyDuration(elapsed), "mgasps", float64(st.usedGas) * 1000 / float64(elapsed),
1059+
"number", end.Number(), "hash", end.Hash(),
10601060
}
10611061
if st.queued > 0 {
10621062
context = append(context, []interface{}{"queued", st.queued}...)

‎core/headerchain.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, checkFreq int, w
330330
}
331331
// Report some public statistics so the user has a clue what's going on
332332
last := chain[len(chain)-1]
333-
log.Info("Imported new block headers", "count", stats.processed, "number", last.Number, "hash", last.Hash(),
334-
"elapsed", common.PrettyDuration(time.Since(start)), "ignored", stats.ignored)
333+
log.Info("Imported new block headers", "count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)),
334+
"number", last.Number, "hash", last.Hash(), "ignored", stats.ignored)
335335

336336
return 0, nil
337337
}

‎log/format.go‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111
"sync/atomic"
1212
"time"
13+
"unicode/utf8"
1314
)
1415

1516
const (
@@ -43,6 +44,13 @@ var locationEnabled uint32
4344
// padded to to aid in alignment.
4445
var locationLength uint32
4546

47+
// fieldPadding is a global map with maximum field value lengths seen until now
48+
// to allow padding log contexts in a bit smarter way.
49+
var fieldPadding = make(map[string]int)
50+
51+
// fieldPaddingLock is a global mutex protecting the field padding map.
52+
var fieldPaddingLock sync.RWMutex
53+
4654
type Format interface {
4755
Format(r *Record) []byte
4856
}
@@ -163,15 +171,29 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
163171
}
164172

165173
// XXX: we should probably check that all of your key bytes aren't invalid
174+
fieldPaddingLock.RLock()
175+
padding := fieldPadding[k]
176+
fieldPaddingLock.RUnlock()
177+
178+
length := utf8.RuneCountInString(v)
179+
if padding < length {
180+
padding = length
181+
182+
fieldPaddingLock.Lock()
183+
fieldPadding[k] = padding
184+
fieldPaddingLock.Unlock()
185+
}
166186
if color > 0 {
167-
fmt.Fprintf(buf, "\x1b[%dm%s\x1b[0m=%s", color, k, v)
187+
fmt.Fprintf(buf, "\x1b[%dm%s\x1b[0m=", color, k)
168188
} else {
169189
buf.WriteString(k)
170190
buf.WriteByte('=')
171-
buf.WriteString(v)
191+
}
192+
buf.WriteString(v)
193+
if i < len(ctx)-2 {
194+
buf.Write(bytes.Repeat([]byte{' '}, padding-length))
172195
}
173196
}
174-
175197
buf.WriteByte('\n')
176198
}
177199

0 commit comments

Comments
 (0)