This commit is contained in:
Martin Holst Swende 2023-10-23 13:56:00 +02:00
parent 0986d4c1c9
commit bdd15a278f
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
5 changed files with 138 additions and 44 deletions

View file

@ -46,7 +46,6 @@ import (
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/naoina/toml" "github.com/naoina/toml"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"time"
) )
var ( var (
@ -229,14 +228,14 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
utils.Fatalf("failed to register catalyst service: %v", err) utils.Fatalf("failed to register catalyst service: %v", err)
} }
} }
go func() { //go func() {
for i := 0; ; i++ { // for i := 0; ; i++ {
path := fmt.Sprintf("%s/fieldPaddings-%d.json", stack.InstanceDir(), i) // path := fmt.Sprintf("%s/fieldPaddings-%d.json", stack.InstanceDir(), i)
err := os.WriteFile(path, log.FieldPaddings(), 0777) // err := os.WriteFile(path, log.FieldPaddings(), 0777)
log.Info(fmt.Sprintf("Wrote fieldpaddings to %v", path), "err", err) // log.Info(fmt.Sprintf("Wrote fieldpaddings to %v", path), "err", err)
time.Sleep(60 * time.Minute) // time.Sleep(60 * time.Minute)
} // }
}() //}()
return stack, backend return stack, backend
} }

111
cmd/geth/field_padding.json Normal file
View file

@ -0,0 +1,111 @@
{
"ETH": 2,
"accounts": 20,
"addfrom": 14,
"age": 8,
"ancestor": 10,
"anncount": 3,
"annpeers": 2,
"at": 14,
"auth": 5,
"b": 2,
"backend": 5,
"bcastcount": 3,
"bcastpeers": 2,
"blocks": 2,
"bodies": 20,
"cache": 7,
"cap": 10,
"chain": 9,
"checks": 3,
"clean": 9,
"codes": 18,
"conn": 16,
"cors": 9,
"count": 4,
"crc32": 10,
"database": 28,
"dbversion": 5,
"dirty": 10,
"downloaded": 10,
"dropfrom": 14,
"duration": 12,
"elapsed": 13,
"endpoint": 9,
"err": 40,
"eta": 14,
"executable": 4,
"finalized": 14,
"from": 2,
"frozen": 10,
"gasFeeCap": 13,
"gasTipCap": 13,
"gcnodes": 9,
"gcsize": 7,
"gctime": 13,
"handles": 7,
"hash": 14,
"have": 4,
"head": 14,
"headers": 20,
"id": 16,
"in": 14,
"ip": 14,
"latency": 2,
"latest": 10,
"left": 10,
"limit": 9,
"livenodes": 9,
"livesize": 9,
"method": 17,
"mgas": 7,
"mgasps": 7,
"needed": 7,
"new": 10,
"newHead": 10,
"nodes": 19,
"nonce": 5,
"number": 10,
"old": 10,
"oldest": 10,
"parent": 14,
"path": 19,
"peer": 8,
"pending": 5,
"percentage": 2,
"plaintxs": 2,
"protocol": 3,
"provided": 4,
"queued": 4,
"readonly": 5,
"receipts": 20,
"rejected": 3,
"reqid": 25,
"root": 14,
"safe": 14,
"scheme": 4,
"seq": 17,
"size": 9,
"slots": 23,
"snapdiffs": 9,
"stales": 2,
"state": 9,
"storage": 9,
"synced": 7,
"tail": 10,
"target": 10,
"tcp": 5,
"td": 30,
"to": 3,
"total": 2,
"tried": 2,
"triedirty": 9,
"tx": 14,
"txs": 4,
"type": 9,
"udp": 5,
"updated": 4,
"url": 22,
"waited": 8,
"want": 14
}

View file

@ -18,6 +18,8 @@
package main package main
import ( import (
_ "embed"
"encoding/json"
"fmt" "fmt"
"os" "os"
"sort" "sort"
@ -198,7 +200,13 @@ var (
var app = flags.NewApp("the go-ethereum command line interface") var app = flags.NewApp("the go-ethereum command line interface")
//go:embed field_padding.json
var fieldPadding []byte
func init() { func init() {
var fp map[string]int
json.Unmarshal(fieldPadding, fp)
log.SetFieldPadding(fp)
// Initialize the CLI app and start Geth // Initialize the CLI app and start Geth
app.Action = geth app.Action = geth
app.Copyright = "Copyright 2013-2023 The go-ethereum Authors" app.Copyright = "Copyright 2013-2023 The go-ethereum Authors"
@ -259,6 +267,7 @@ func init() {
prompt.Stdin.Close() // Resets terminal mode. prompt.Stdin.Close() // Resets terminal mode.
return nil return nil
} }
} }
func main() { func main() {

View file

@ -443,7 +443,3 @@ func (api *DebugAPI) GetTrieFlushInterval() (string, error) {
} }
return api.eth.blockchain.GetTrieFlushInterval().String(), nil return api.eth.blockchain.GetTrieFlushInterval().String(), nil
} }
func (api *DebugAPI) FieldPaddings() (string, error) {
return string(log.FieldPaddings()), nil
}

View file

@ -8,7 +8,6 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"sync"
"sync/atomic" "sync/atomic"
"time" "time"
"unicode/utf8" "unicode/utf8"
@ -55,17 +54,10 @@ var locationLength atomic.Uint32
// to allow padding log contexts in a bit smarter way. // to allow padding log contexts in a bit smarter way.
var fieldPadding = make(map[string]int) var fieldPadding = make(map[string]int)
// fieldPaddingLock is a global mutex protecting the field padding map.
var fieldPaddingLock sync.RWMutex
type Format interface { type Format interface {
Format(r *Record) []byte Format(r *Record) []byte
} }
func init() {
// json.Unmarshal([]byte("{\n \"ETH\": 2,\n \"LES\": 1,\n \"accounts\": 2,\n \"age\": 9,\n \"backend\": 5,\n \"blocks\": 1,\n \"cap\": 10,\n \"clean\": 9,\n \"dangling\": 1,\n \"dbversion\": 5,\n \"dirty\": 9,\n \"duration\": 10,\n \"elapsed\": 11,\n \"err\": 31,\n \"fees\": 7,\n \"gas\": 5,\n \"gcnodes\": 1,\n \"gcsize\": 5,\n \"gctime\": 2,\n \"hash\": 14,\n \"id\": 18,\n \"ip\": 9,\n \"limit\": 9,\n \"livenodes\": 1,\n \"livesize\": 5,\n \"mgas\": 5,\n \"mgasps\": 6,\n \"network\": 4,\n \"nodes\": 2,\n \"nonce\": 1,\n \"number\": 2,\n \"reason\": 8,\n \"reqid\": 2,\n \"root\": 14,\n \"scheme\": 4,\n \"seq\": 17,\n \"size\": 7,\n \"slots\": 1,\n \"snapdiffs\": 7,\n \"storage\": 7,\n \"tcp\": 1,\n \"td\": 1,\n \"threshold\": 1,\n \"time\": 11,\n \"total\": 2,\n \"triedirty\": 7,\n \"txs\": 1,\n \"udp\": 1,\n \"url\": 13,\n \"value\": 3,\n \"withdrawals\": 1\n}"), fieldPadding)
}
// FormatFunc returns a new Format object which uses // FormatFunc returns a new Format object which uses
// the given function to perform record formatting. // the given function to perform record formatting.
func FormatFunc(f func(*Record) []byte) Format { func FormatFunc(f func(*Record) []byte) Format {
@ -78,6 +70,12 @@ func (f formatFunc) Format(r *Record) []byte {
return f(r) return f(r)
} }
// SetFieldPadding configures the log library to use the given field-paddings.
// This method should preferrably be invoked during package init.
func SetFieldPadding(fp map[string]int) {
fieldPadding = fp
}
// TerminalStringer is an analogous interface to the stdlib stringer, allowing // TerminalStringer is an analogous interface to the stdlib stringer, allowing
// own types to have custom shortened serialization formats when printed to the // own types to have custom shortened serialization formats when printed to the
// screen. // screen.
@ -181,20 +179,6 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
} else { } else {
k = escapeString(k) k = escapeString(k)
} }
// XXX: we should probably check that all of your key bytes aren't invalid
fieldPaddingLock.RLock()
padding := fieldPadding[k]
fieldPaddingLock.RUnlock()
length := utf8.RuneCountInString(v)
if padding < length && length <= termCtxMaxPadding {
padding = length
fieldPaddingLock.Lock()
fieldPadding[k] = padding
fieldPaddingLock.Unlock()
}
if color > 0 { if color > 0 {
fmt.Fprintf(buf, "\x1b[%dm%s\x1b[0m=", color, k) fmt.Fprintf(buf, "\x1b[%dm%s\x1b[0m=", color, k)
} else { } else {
@ -202,8 +186,10 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
buf.WriteByte('=') buf.WriteByte('=')
} }
buf.WriteString(v) buf.WriteString(v)
if i < len(ctx)-2 && padding > length { if i < len(ctx)-2 {
buf.Write(bytes.Repeat([]byte{' '}, padding-length)) if length, padding := utf8.RuneCountInString(v), fieldPadding[k]; padding > length {
buf.Write(bytes.Repeat([]byte{' '}, padding-length))
}
} }
} }
buf.WriteByte('\n') buf.WriteByte('\n')
@ -215,13 +201,6 @@ func JSONFormat() Format {
return JSONFormatEx(false, true) return JSONFormatEx(false, true)
} }
func FieldPaddings() []byte {
fieldPaddingLock.RLock()
output, _ := json.MarshalIndent(fieldPadding, "", " ")
fieldPaddingLock.RUnlock()
return output
}
// JSONFormatOrderedEx formats log records as JSON arrays. If pretty is true, // JSONFormatOrderedEx formats log records as JSON arrays. If pretty is true,
// records will be pretty-printed. If lineSeparated is true, records // records will be pretty-printed. If lineSeparated is true, records
// will be logged with a new line between each record. // will be logged with a new line between each record.