From bdd15a278fcd69e569172dc5899b9f4c79881776 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 23 Oct 2023 13:56:00 +0200 Subject: [PATCH] wip --- cmd/geth/config.go | 17 +++--- cmd/geth/field_padding.json | 111 ++++++++++++++++++++++++++++++++++++ cmd/geth/main.go | 9 +++ eth/api_debug.go | 4 -- log/format.go | 41 ++++--------- 5 files changed, 138 insertions(+), 44 deletions(-) create mode 100644 cmd/geth/field_padding.json diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 212d1e4468..c53a1f6888 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -46,7 +46,6 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/naoina/toml" "github.com/urfave/cli/v2" - "time" ) var ( @@ -229,14 +228,14 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { utils.Fatalf("failed to register catalyst service: %v", err) } } - go func() { - for i := 0; ; i++ { - path := fmt.Sprintf("%s/fieldPaddings-%d.json", stack.InstanceDir(), i) - err := os.WriteFile(path, log.FieldPaddings(), 0777) - log.Info(fmt.Sprintf("Wrote fieldpaddings to %v", path), "err", err) - time.Sleep(60 * time.Minute) - } - }() + //go func() { + // for i := 0; ; i++ { + // path := fmt.Sprintf("%s/fieldPaddings-%d.json", stack.InstanceDir(), i) + // err := os.WriteFile(path, log.FieldPaddings(), 0777) + // log.Info(fmt.Sprintf("Wrote fieldpaddings to %v", path), "err", err) + // time.Sleep(60 * time.Minute) + // } + //}() return stack, backend } diff --git a/cmd/geth/field_padding.json b/cmd/geth/field_padding.json new file mode 100644 index 0000000000..4a38bf47a9 --- /dev/null +++ b/cmd/geth/field_padding.json @@ -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 +} \ No newline at end of file diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 4b26de05a9..d8aecda4b0 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -18,6 +18,8 @@ package main import ( + _ "embed" + "encoding/json" "fmt" "os" "sort" @@ -198,7 +200,13 @@ var ( var app = flags.NewApp("the go-ethereum command line interface") +//go:embed field_padding.json +var fieldPadding []byte + func init() { + var fp map[string]int + json.Unmarshal(fieldPadding, fp) + log.SetFieldPadding(fp) // Initialize the CLI app and start Geth app.Action = geth app.Copyright = "Copyright 2013-2023 The go-ethereum Authors" @@ -259,6 +267,7 @@ func init() { prompt.Stdin.Close() // Resets terminal mode. return nil } + } func main() { diff --git a/eth/api_debug.go b/eth/api_debug.go index 9c93de9c43..dc9f568146 100644 --- a/eth/api_debug.go +++ b/eth/api_debug.go @@ -443,7 +443,3 @@ func (api *DebugAPI) GetTrieFlushInterval() (string, error) { } return api.eth.blockchain.GetTrieFlushInterval().String(), nil } - -func (api *DebugAPI) FieldPaddings() (string, error) { - return string(log.FieldPaddings()), nil -} diff --git a/log/format.go b/log/format.go index 01f4101c23..c76df2134f 100644 --- a/log/format.go +++ b/log/format.go @@ -8,7 +8,6 @@ import ( "reflect" "strconv" "strings" - "sync" "sync/atomic" "time" "unicode/utf8" @@ -55,17 +54,10 @@ var locationLength atomic.Uint32 // to allow padding log contexts in a bit smarter way. var fieldPadding = make(map[string]int) -// fieldPaddingLock is a global mutex protecting the field padding map. -var fieldPaddingLock sync.RWMutex - type Format interface { 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 // the given function to perform record formatting. func FormatFunc(f func(*Record) []byte) Format { @@ -78,6 +70,12 @@ func (f formatFunc) Format(r *Record) []byte { 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 // own types to have custom shortened serialization formats when printed to the // screen. @@ -181,20 +179,6 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) { } else { 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 { fmt.Fprintf(buf, "\x1b[%dm%s\x1b[0m=", color, k) } else { @@ -202,8 +186,10 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) { buf.WriteByte('=') } buf.WriteString(v) - if i < len(ctx)-2 && padding > length { - buf.Write(bytes.Repeat([]byte{' '}, padding-length)) + if i < len(ctx)-2 { + if length, padding := utf8.RuneCountInString(v), fieldPadding[k]; padding > length { + buf.Write(bytes.Repeat([]byte{' '}, padding-length)) + } } } buf.WriteByte('\n') @@ -215,13 +201,6 @@ func JSONFormat() Format { 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, // records will be pretty-printed. If lineSeparated is true, records // will be logged with a new line between each record.