Skip to content

Commit 697528e

Browse files
committed
all: make logs a bit easier on the eye to digest (ethereum#22665)
1 parent 273e114 commit 697528e

8 files changed

+189
-13
lines changed

accounts/url.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (u URL) String() string {
6464
func (u URL) TerminalString() string {
6565
url := u.String()
6666
if len(url) > 32 {
67-
return url[:31] + ""
67+
return url[:31] + ".."
6868
}
6969
return url
7070
}

common/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (h Hash) Hex() string { return hexutil.Encode(h[:]) }
9999
// TerminalString implements log.TerminalStringer, formatting a string for console
100100
// output during logging.
101101
func (h Hash) TerminalString() string {
102-
return fmt.Sprintf("%x%x", h[:3], h[29:])
102+
return fmt.Sprintf("%x..%x", h[:3], h[29:])
103103
}
104104

105105
// String implements the stringer interface and is used also by the logger when

core/blockchain.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error {
440440
// Make sure that both the block as well at its state trie exists
441441
block := bc.GetBlockByHash(hash)
442442
if block == nil {
443-
return fmt.Errorf("non existent block [%x]", hash[:4])
443+
return fmt.Errorf("non existent block [%x..]", hash[:4])
444444
}
445445
if _, err := trie.NewSecure(block.Root(), bc.stateCache.TrieDB()); err != nil {
446446
return err
@@ -1055,7 +1055,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
10551055
if blockChain[i].NumberU64() != blockChain[i-1].NumberU64()+1 || blockChain[i].ParentHash() != blockChain[i-1].Hash() {
10561056
log.Error("Non contiguous receipt insert", "number", blockChain[i].Number(), "hash", blockChain[i].Hash(), "parent", blockChain[i].ParentHash(),
10571057
"prevnumber", blockChain[i-1].Number(), "prevhash", blockChain[i-1].Hash())
1058-
return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x], item %d is #%d [%x] (parent [%x])", i-1, blockChain[i-1].NumberU64(),
1058+
return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x..], item %d is #%d [%x..] (parent [%x..])", i-1, blockChain[i-1].NumberU64(),
10591059
blockChain[i-1].Hash().Bytes()[:4], i, blockChain[i].NumberU64(), blockChain[i].Hash().Bytes()[:4], blockChain[i].ParentHash().Bytes()[:4])
10601060
}
10611061
}
@@ -1075,7 +1075,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
10751075
blockHash, blockNumber := block.Hash(), block.NumberU64()
10761076
// Short circuit if the owner header is unknown
10771077
if !bc.HasHeader(blockHash, blockNumber) {
1078-
return i, fmt.Errorf("containing header #%d [%x] unknown", blockNumber, blockHash.Bytes()[:4])
1078+
return i, fmt.Errorf("containing header #%d [%x..] unknown", blockNumber, blockHash.Bytes()[:4])
10791079
}
10801080
// Skip if the entire data is already known
10811081
if bc.HasBlock(blockHash, blockNumber) {
@@ -1422,7 +1422,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
14221422
log.Error("Non contiguous block insert", "number", chain[i].Number(), "hash", chain[i].Hash(),
14231423
"parent", chain[i].ParentHash(), "prevnumber", chain[i-1].Number(), "prevhash", chain[i-1].Hash())
14241424

1425-
return 0, nil, nil, fmt.Errorf("non contiguous insert: item %d is #%d [%x], item %d is #%d [%x] (parent [%x])", i-1, chain[i-1].NumberU64(),
1425+
return 0, nil, nil, fmt.Errorf("non contiguous insert: item %d is #%d [%x..], item %d is #%d [%x..] (parent [%x..])", i-1, chain[i-1].NumberU64(),
14261426
chain[i-1].Hash().Bytes()[:4], i, chain[i].NumberU64(), chain[i].Hash().Bytes()[:4], chain[i].ParentHash().Bytes()[:4])
14271427
}
14281428
}

core/blockchain_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1221,13 +1221,13 @@ func TestBlockchainHeaderchainReorgConsistency(t *testing.T) {
12211221
t.Fatalf("block %d: failed to insert into chain: %v", i, err)
12221222
}
12231223
if chain.CurrentBlock().Hash() != chain.CurrentHeader().Hash() {
1224-
t.Errorf("block %d: current block/header mismatch: block #%d [%x], header #%d [%x]", i, chain.CurrentBlock().Number(), chain.CurrentBlock().Hash().Bytes()[:4], chain.CurrentHeader().Number, chain.CurrentHeader().Hash().Bytes()[:4])
1224+
t.Errorf("block %d: current block/header mismatch: block #%d [%x..], header #%d [%x..]", i, chain.CurrentBlock().Number(), chain.CurrentBlock().Hash().Bytes()[:4], chain.CurrentHeader().Number, chain.CurrentHeader().Hash().Bytes()[:4])
12251225
}
12261226
if _, err := chain.InsertChain(forks[i : i+1]); err != nil {
12271227
t.Fatalf(" fork %d: failed to insert into chain: %v", i, err)
12281228
}
12291229
if chain.CurrentBlock().Hash() != chain.CurrentHeader().Hash() {
1230-
t.Errorf(" fork %d: current block/header mismatch: block #%d [%x], header #%d [%x]", i, chain.CurrentBlock().Number(), chain.CurrentBlock().Hash().Bytes()[:4], chain.CurrentHeader().Number, chain.CurrentHeader().Hash().Bytes()[:4])
1230+
t.Errorf(" fork %d: current block/header mismatch: block #%d [%x..], header #%d [%x..]", i, chain.CurrentBlock().Number(), chain.CurrentBlock().Hash().Bytes()[:4], chain.CurrentHeader().Number, chain.CurrentHeader().Hash().Bytes()[:4])
12311231
}
12321232
}
12331233
}

core/chain_indexer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func (c *ChainIndexer) processSection(section uint64, lastHead common.Hash) (com
356356
}
357357
header := GetHeader(c.chainDb, hash, number)
358358
if header == nil {
359-
return common.Hash{}, fmt.Errorf("block #%d [%x] not found", number, hash[:4])
359+
return common.Hash{}, fmt.Errorf("block #%d [%x..] not found", number, hash[:4])
360360
} else if header.ParentHash != lastHead {
361361
return common.Hash{}, errors.New("chain reorged during section processing")
362362
}

core/headerchain.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (hc *HeaderChain) ValidateHeaderChain(chain []*types.Header, checkFreq int)
205205
log.Error("Non contiguous header insert", "number", chain[i].Number, "hash", chain[i].Hash(),
206206
"parent", chain[i].ParentHash, "prevnumber", chain[i-1].Number, "prevhash", chain[i-1].Hash())
207207

208-
return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x], item %d is #%d [%x] (parent [%x])", i-1, chain[i-1].Number,
208+
return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x..], item %d is #%d [%x..] (parent [%x..])", i-1, chain[i-1].Number,
209209
chain[i-1].Hash().Bytes()[:4], i, chain[i].Number, chain[i].Hash().Bytes()[:4], chain[i].ParentHash[:4])
210210
}
211211
}

log/format.go

+104-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"math/big"
78
"reflect"
89
"strconv"
910
"strings"
@@ -285,11 +286,20 @@ func formatLogfmtValue(value interface{}, term bool) string {
285286
return "nil"
286287
}
287288

288-
if t, ok := value.(time.Time); ok {
289+
switch v := value.(type) {
290+
case time.Time:
289291
// Performance optimization: No need for escaping since the provided
290292
// timeFormat doesn't have any escape characters, and escaping is
291293
// expensive.
292-
return t.Format(timeFormat)
294+
return v.Format(timeFormat)
295+
296+
case *big.Int:
297+
// Big ints get consumed by the Stringer clause so we need to handle
298+
// them earlier on.
299+
if v == nil {
300+
return "<nil>"
301+
}
302+
return formatLogfmtBigInt(v)
293303
}
294304
if term {
295305
if s, ok := value.(TerminalStringer); ok {
@@ -305,15 +315,106 @@ func formatLogfmtValue(value interface{}, term bool) string {
305315
return strconv.FormatFloat(float64(v), floatFormat, 3, 64)
306316
case float64:
307317
return strconv.FormatFloat(v, floatFormat, 3, 64)
308-
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
318+
case int8, uint8:
309319
return fmt.Sprintf("%d", value)
320+
case int:
321+
return FormatLogfmtInt64(int64(v))
322+
case int16:
323+
return FormatLogfmtInt64(int64(v))
324+
case int32:
325+
return FormatLogfmtInt64(int64(v))
326+
case int64:
327+
return FormatLogfmtInt64(v)
328+
case uint:
329+
return FormatLogfmtUint64(uint64(v))
330+
case uint16:
331+
return FormatLogfmtUint64(uint64(v))
332+
case uint32:
333+
return FormatLogfmtUint64(uint64(v))
334+
case uint64:
335+
return FormatLogfmtUint64(v)
310336
case string:
311337
return escapeString(v)
312338
default:
313339
return escapeString(fmt.Sprintf("%+v", value))
314340
}
315341
}
316342

343+
344+
// FormatLogfmtInt64 formats a potentially big number in a friendlier split format.
345+
func FormatLogfmtInt64(n int64) string {
346+
if n < 0 {
347+
return formatLogfmtUint64(uint64(-n), true)
348+
}
349+
return formatLogfmtUint64(uint64(n), false)
350+
}
351+
352+
// FormatLogfmtUint64 formats a potentially big number in a friendlier split format.
353+
func FormatLogfmtUint64(n uint64) string {
354+
return formatLogfmtUint64(n, false)
355+
}
356+
357+
func formatLogfmtUint64(n uint64, neg bool) string {
358+
// Small numbers are fine as is
359+
if n < 100000 {
360+
if neg {
361+
return strconv.Itoa(-int(n))
362+
} else {
363+
return strconv.Itoa(int(n))
364+
}
365+
}
366+
// Large numbers should be split
367+
const maxLength = 26
368+
369+
var (
370+
out = make([]byte, maxLength)
371+
i = maxLength - 1
372+
comma = 0
373+
)
374+
for ; n > 0; i-- {
375+
if comma == 3 {
376+
comma = 0
377+
out[i] = ','
378+
} else {
379+
comma++
380+
out[i] = '0' + byte(n%10)
381+
n /= 10
382+
}
383+
}
384+
if neg {
385+
out[i] = '-'
386+
i--
387+
}
388+
return string(out[i+1:])
389+
}
390+
391+
var big1000 = big.NewInt(1000)
392+
393+
// formatLogfmtBigInt formats a potentially gigantic number in a friendlier split
394+
// format.
395+
func formatLogfmtBigInt(n *big.Int) string {
396+
// Most number don't need fancy handling, just downcast
397+
if n.IsUint64() {
398+
return FormatLogfmtUint64(n.Uint64())
399+
}
400+
if n.IsInt64() {
401+
return FormatLogfmtInt64(n.Int64())
402+
}
403+
// Ok, huge number needs huge effort
404+
groups := make([]string, 0, 8) // random initial size to cover most cases
405+
for n.Cmp(big1000) >= 0 {
406+
_, mod := n.DivMod(n, big1000, nil)
407+
groups = append(groups, fmt.Sprintf("%03d", mod))
408+
}
409+
groups = append(groups, n.String())
410+
411+
last := len(groups) - 1
412+
for i := 0; i < len(groups)/2; i++ {
413+
groups[i], groups[last-i] = groups[last-i], groups[i]
414+
}
415+
return strings.Join(groups, ",")
416+
}
417+
317418
// escapeString checks if the provided string needs escaping/quoting, and
318419
// calls strconv.Quote if needed
319420
func escapeString(s string) string {

log/format_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package log
2+
3+
import (
4+
"math"
5+
"math/rand"
6+
"testing"
7+
)
8+
9+
func TestPrettyInt64(t *testing.T) {
10+
tests := []struct {
11+
n int64
12+
s string
13+
}{
14+
{0, "0"},
15+
{10, "10"},
16+
{-10, "-10"},
17+
{100, "100"},
18+
{-100, "-100"},
19+
{1000, "1000"},
20+
{-1000, "-1000"},
21+
{10000, "10000"},
22+
{-10000, "-10000"},
23+
{99999, "99999"},
24+
{-99999, "-99999"},
25+
{100000, "100,000"},
26+
{-100000, "-100,000"},
27+
{1000000, "1,000,000"},
28+
{-1000000, "-1,000,000"},
29+
{math.MaxInt64, "9,223,372,036,854,775,807"},
30+
{math.MinInt64, "-9,223,372,036,854,775,808"},
31+
}
32+
for i, tt := range tests {
33+
if have := FormatLogfmtInt64(tt.n); have != tt.s {
34+
t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
35+
}
36+
}
37+
}
38+
39+
func TestPrettyUint64(t *testing.T) {
40+
tests := []struct {
41+
n uint64
42+
s string
43+
}{
44+
{0, "0"},
45+
{10, "10"},
46+
{100, "100"},
47+
{1000, "1000"},
48+
{10000, "10000"},
49+
{99999, "99999"},
50+
{100000, "100,000"},
51+
{1000000, "1,000,000"},
52+
{math.MaxUint64, "18,446,744,073,709,551,615"},
53+
}
54+
for i, tt := range tests {
55+
if have := FormatLogfmtUint64(tt.n); have != tt.s {
56+
t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
57+
}
58+
}
59+
}
60+
61+
var sink string
62+
63+
func BenchmarkPrettyInt64Logfmt(b *testing.B) {
64+
b.ReportAllocs()
65+
for i := 0; i < b.N; i++ {
66+
sink = FormatLogfmtInt64(rand.Int63())
67+
}
68+
}
69+
70+
func BenchmarkPrettyUint64Logfmt(b *testing.B) {
71+
b.ReportAllocs()
72+
for i := 0; i < b.N; i++ {
73+
sink = FormatLogfmtUint64(rand.Uint64())
74+
}
75+
}

0 commit comments

Comments
 (0)