mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
all: make logs a bit easier on the eye to digest (#22665)
This commit is contained in:
parent
3d21631145
commit
aedfea681b
8 changed files with 193 additions and 19 deletions
|
|
@ -64,7 +64,7 @@ func (u URL) String() string {
|
|||
func (u URL) TerminalString() string {
|
||||
url := u.String()
|
||||
if len(url) > 32 {
|
||||
return url[:31] + "…"
|
||||
return url[:31] + ".."
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
|
@ -76,10 +76,9 @@ func (u URL) MarshalJSON() ([]byte, error) {
|
|||
|
||||
// Cmp compares x and y and returns:
|
||||
//
|
||||
// -1 if x < y
|
||||
// 0 if x == y
|
||||
// +1 if x > y
|
||||
//
|
||||
// -1 if x < y
|
||||
// 0 if x == y
|
||||
// +1 if x > y
|
||||
func (u URL) Cmp(url URL) int {
|
||||
if u.Scheme == url.Scheme {
|
||||
return strings.Compare(u.Path, url.Path)
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ func (h Hash) Cmp(other Hash) int {
|
|||
func (h Hash) IsZero() bool { return h == Hash{} }
|
||||
|
||||
// Get the string representation of the underlying hash
|
||||
func (h Hash) Str() string { return string(h[:]) }
|
||||
func (h Hash) Str() string { return string(h[:]) }
|
||||
|
||||
// Bytes gets the byte representation of the underlying hash.
|
||||
func (h Hash) Bytes() []byte { return h[:] }
|
||||
|
|
@ -116,12 +116,12 @@ func (h Hash) Bytes() []byte { return h[:] }
|
|||
func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) }
|
||||
|
||||
// Hex converts a hash to a hex string.
|
||||
func (h Hash) Hex() string { return hexutil.Encode(h[:]) }
|
||||
func (h Hash) Hex() string { return hexutil.Encode(h[:]) }
|
||||
|
||||
// TerminalString implements log.TerminalStringer, formatting a string for console
|
||||
// output during logging.
|
||||
func (h Hash) TerminalString() string {
|
||||
return fmt.Sprintf("%x…%x", h[:3], h[29:])
|
||||
return fmt.Sprintf("%x..%x", h[:3], h[29:])
|
||||
}
|
||||
|
||||
// String implements the stringer interface and is used also by the logger when
|
||||
|
|
|
|||
|
|
@ -440,7 +440,7 @@ func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error {
|
|||
// Make sure that both the block as well at its state trie exists
|
||||
block := bc.GetBlockByHash(hash)
|
||||
if block == nil {
|
||||
return fmt.Errorf("non existent block [%x…]", hash[:4])
|
||||
return fmt.Errorf("non existent block [%x..]", hash[:4])
|
||||
}
|
||||
if _, err := trie.NewSecure(block.Root(), bc.stateCache.TrieDB()); err != nil {
|
||||
return err
|
||||
|
|
@ -1055,7 +1055,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
|
|||
if blockChain[i].NumberU64() != blockChain[i-1].NumberU64()+1 || blockChain[i].ParentHash() != blockChain[i-1].Hash() {
|
||||
log.Error("Non contiguous receipt insert", "number", blockChain[i].Number(), "hash", blockChain[i].Hash(), "parent", blockChain[i].ParentHash(),
|
||||
"prevnumber", blockChain[i-1].Number(), "prevhash", blockChain[i-1].Hash())
|
||||
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(),
|
||||
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(),
|
||||
blockChain[i-1].Hash().Bytes()[:4], i, blockChain[i].NumberU64(), blockChain[i].Hash().Bytes()[:4], blockChain[i].ParentHash().Bytes()[:4])
|
||||
}
|
||||
}
|
||||
|
|
@ -1075,7 +1075,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
|
|||
blockHash, blockNumber := block.Hash(), block.NumberU64()
|
||||
// Short circuit if the owner header is unknown
|
||||
if !bc.HasHeader(blockHash, blockNumber) {
|
||||
return i, fmt.Errorf("containing header #%d [%x…] unknown", blockNumber, blockHash.Bytes()[:4])
|
||||
return i, fmt.Errorf("containing header #%d [%x..] unknown", blockNumber, blockHash.Bytes()[:4])
|
||||
}
|
||||
// Skip if the entire data is already known
|
||||
if bc.HasBlock(blockHash, blockNumber) {
|
||||
|
|
@ -1422,7 +1422,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
|
|||
log.Error("Non contiguous block insert", "number", chain[i].Number(), "hash", chain[i].Hash(),
|
||||
"parent", chain[i].ParentHash(), "prevnumber", chain[i-1].Number(), "prevhash", chain[i-1].Hash())
|
||||
|
||||
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(),
|
||||
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(),
|
||||
chain[i-1].Hash().Bytes()[:4], i, chain[i].NumberU64(), chain[i].Hash().Bytes()[:4], chain[i].ParentHash().Bytes()[:4])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1221,13 +1221,13 @@ func TestBlockchainHeaderchainReorgConsistency(t *testing.T) {
|
|||
t.Fatalf("block %d: failed to insert into chain: %v", i, err)
|
||||
}
|
||||
if chain.CurrentBlock().Hash() != chain.CurrentHeader().Hash() {
|
||||
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])
|
||||
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])
|
||||
}
|
||||
if _, err := chain.InsertChain(forks[i : i+1]); err != nil {
|
||||
t.Fatalf(" fork %d: failed to insert into chain: %v", i, err)
|
||||
}
|
||||
if chain.CurrentBlock().Hash() != chain.CurrentHeader().Hash() {
|
||||
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])
|
||||
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -356,7 +356,7 @@ func (c *ChainIndexer) processSection(section uint64, lastHead common.Hash) (com
|
|||
}
|
||||
header := GetHeader(c.chainDb, hash, number)
|
||||
if header == nil {
|
||||
return common.Hash{}, fmt.Errorf("block #%d [%x…] not found", number, hash[:4])
|
||||
return common.Hash{}, fmt.Errorf("block #%d [%x..] not found", number, hash[:4])
|
||||
} else if header.ParentHash != lastHead {
|
||||
return common.Hash{}, errors.New("chain reorged during section processing")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -205,7 +205,7 @@ func (hc *HeaderChain) ValidateHeaderChain(chain []*types.Header, checkFreq int)
|
|||
log.Error("Non contiguous header insert", "number", chain[i].Number, "hash", chain[i].Hash(),
|
||||
"parent", chain[i].ParentHash, "prevnumber", chain[i-1].Number, "prevhash", chain[i-1].Hash())
|
||||
|
||||
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,
|
||||
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,
|
||||
chain[i-1].Hash().Bytes()[:4], i, chain[i].Number, chain[i].Hash().Bytes()[:4], chain[i].ParentHash[:4])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
106
log/format.go
106
log/format.go
|
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
|
@ -327,11 +328,20 @@ func formatLogfmtValue(value interface{}, term bool) string {
|
|||
return "nil"
|
||||
}
|
||||
|
||||
if t, ok := value.(time.Time); ok {
|
||||
switch v := value.(type) {
|
||||
case time.Time:
|
||||
// Performance optimization: No need for escaping since the provided
|
||||
// timeFormat doesn't have any escape characters, and escaping is
|
||||
// expensive.
|
||||
return t.Format(timeFormat)
|
||||
return v.Format(timeFormat)
|
||||
|
||||
case *big.Int:
|
||||
// Big ints get consumed by the Stringer clause so we need to handle
|
||||
// them earlier on.
|
||||
if v == nil {
|
||||
return "<nil>"
|
||||
}
|
||||
return formatLogfmtBigInt(v)
|
||||
}
|
||||
if term {
|
||||
if s, ok := value.(TerminalStringer); ok {
|
||||
|
|
@ -347,8 +357,24 @@ func formatLogfmtValue(value interface{}, term bool) string {
|
|||
return strconv.FormatFloat(float64(v), floatFormat, 3, 64)
|
||||
case float64:
|
||||
return strconv.FormatFloat(v, floatFormat, 3, 64)
|
||||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||
case int8, uint8:
|
||||
return fmt.Sprintf("%d", value)
|
||||
case int:
|
||||
return FormatLogfmtInt64(int64(v))
|
||||
case int16:
|
||||
return FormatLogfmtInt64(int64(v))
|
||||
case int32:
|
||||
return FormatLogfmtInt64(int64(v))
|
||||
case int64:
|
||||
return FormatLogfmtInt64(v)
|
||||
case uint:
|
||||
return FormatLogfmtUint64(uint64(v))
|
||||
case uint16:
|
||||
return FormatLogfmtUint64(uint64(v))
|
||||
case uint32:
|
||||
return FormatLogfmtUint64(uint64(v))
|
||||
case uint64:
|
||||
return FormatLogfmtUint64(v)
|
||||
case string:
|
||||
return escapeString(v)
|
||||
default:
|
||||
|
|
@ -356,6 +382,80 @@ func formatLogfmtValue(value interface{}, term bool) string {
|
|||
}
|
||||
}
|
||||
|
||||
// FormatLogfmtInt64 formats a potentially big number in a friendlier split format.
|
||||
func FormatLogfmtInt64(n int64) string {
|
||||
if n < 0 {
|
||||
return formatLogfmtUint64(uint64(-n), true)
|
||||
}
|
||||
return formatLogfmtUint64(uint64(n), false)
|
||||
}
|
||||
|
||||
// FormatLogfmtUint64 formats a potentially big number in a friendlier split format.
|
||||
func FormatLogfmtUint64(n uint64) string {
|
||||
return formatLogfmtUint64(n, false)
|
||||
}
|
||||
|
||||
func formatLogfmtUint64(n uint64, neg bool) string {
|
||||
// Small numbers are fine as is
|
||||
if n < 100000 {
|
||||
if neg {
|
||||
return strconv.Itoa(-int(n))
|
||||
} else {
|
||||
return strconv.Itoa(int(n))
|
||||
}
|
||||
}
|
||||
// Large numbers should be split
|
||||
const maxLength = 26
|
||||
|
||||
var (
|
||||
out = make([]byte, maxLength)
|
||||
i = maxLength - 1
|
||||
comma = 0
|
||||
)
|
||||
for ; n > 0; i-- {
|
||||
if comma == 3 {
|
||||
comma = 0
|
||||
out[i] = ','
|
||||
} else {
|
||||
comma++
|
||||
out[i] = '0' + byte(n%10)
|
||||
n /= 10
|
||||
}
|
||||
}
|
||||
if neg {
|
||||
out[i] = '-'
|
||||
i--
|
||||
}
|
||||
return string(out[i+1:])
|
||||
}
|
||||
|
||||
var big1000 = big.NewInt(1000)
|
||||
|
||||
// formatLogfmtBigInt formats a potentially gigantic number in a friendlier split
|
||||
// format.
|
||||
func formatLogfmtBigInt(n *big.Int) string {
|
||||
// Most number don't need fancy handling, just downcast
|
||||
if n.IsUint64() {
|
||||
return FormatLogfmtUint64(n.Uint64())
|
||||
}
|
||||
if n.IsInt64() {
|
||||
return FormatLogfmtInt64(n.Int64())
|
||||
}
|
||||
// Ok, huge number needs huge effort
|
||||
groups := make([]string, 0, 8) // random initial size to cover most cases
|
||||
for n.Cmp(big1000) >= 0 {
|
||||
_, mod := n.DivMod(n, big1000, nil)
|
||||
groups = append(groups, fmt.Sprintf("%03d", mod))
|
||||
}
|
||||
groups = append(groups, n.String())
|
||||
|
||||
last := len(groups) - 1
|
||||
for i := 0; i < len(groups)/2; i++ {
|
||||
groups[i], groups[last-i] = groups[last-i], groups[i]
|
||||
}
|
||||
return strings.Join(groups, ",")
|
||||
}
|
||||
|
||||
// escapeString checks if the provided string needs escaping/quoting, and
|
||||
// calls strconv.Quote if needed
|
||||
func escapeString(s string) string {
|
||||
|
|
|
|||
75
log/format_test.go
Normal file
75
log/format_test.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPrettyInt64(t *testing.T) {
|
||||
tests := []struct {
|
||||
n int64
|
||||
s string
|
||||
}{
|
||||
{0, "0"},
|
||||
{10, "10"},
|
||||
{-10, "-10"},
|
||||
{100, "100"},
|
||||
{-100, "-100"},
|
||||
{1000, "1000"},
|
||||
{-1000, "-1000"},
|
||||
{10000, "10000"},
|
||||
{-10000, "-10000"},
|
||||
{99999, "99999"},
|
||||
{-99999, "-99999"},
|
||||
{100000, "100,000"},
|
||||
{-100000, "-100,000"},
|
||||
{1000000, "1,000,000"},
|
||||
{-1000000, "-1,000,000"},
|
||||
{math.MaxInt64, "9,223,372,036,854,775,807"},
|
||||
{math.MinInt64, "-9,223,372,036,854,775,808"},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
if have := FormatLogfmtInt64(tt.n); have != tt.s {
|
||||
t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrettyUint64(t *testing.T) {
|
||||
tests := []struct {
|
||||
n uint64
|
||||
s string
|
||||
}{
|
||||
{0, "0"},
|
||||
{10, "10"},
|
||||
{100, "100"},
|
||||
{1000, "1000"},
|
||||
{10000, "10000"},
|
||||
{99999, "99999"},
|
||||
{100000, "100,000"},
|
||||
{1000000, "1,000,000"},
|
||||
{math.MaxUint64, "18,446,744,073,709,551,615"},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
if have := FormatLogfmtUint64(tt.n); have != tt.s {
|
||||
t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var sink string
|
||||
|
||||
func BenchmarkPrettyInt64Logfmt(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
sink = FormatLogfmtInt64(rand.Int63())
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPrettyUint64Logfmt(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
sink = FormatLogfmtUint64(rand.Uint64())
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue