diff --git a/cmd/geth/logtestcmd.go b/cmd/geth/logtestcmd.go new file mode 100644 index 0000000000..2cb3e05d58 --- /dev/null +++ b/cmd/geth/logtestcmd.go @@ -0,0 +1,82 @@ +// Copyright 2023 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package main + +import ( + "fmt" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/log" + "github.com/holiman/uint256" + "github.com/urfave/cli/v2" + "math" + "math/big" + "time" +) + +// logTest is an entry point which spits out some logs. This is used by testing +// to verify expected outputs +func logTest(ctx *cli.Context) error { + + { // big.Int + ba, _ := new(big.Int).SetString("111222333444555678999", 10) // "111,222,333,444,555,678,999" + bb, _ := new(big.Int).SetString("-111222333444555678999", 10) // "-111,222,333,444,555,678,999" + bc, _ := new(big.Int).SetString("11122233344455567899900", 10) // "11,122,233,344,455,567,899,900" + bd, _ := new(big.Int).SetString("-11122233344455567899900", 10) // "-11,122,233,344,455,567,899,900" + log.Info("big.Int", "111,222,333,444,555,678,999", ba) + log.Info("-big.Int", "-111,222,333,444,555,678,999", bb) + log.Info("big.Int", "11,122,233,344,455,567,899,900", bc) + log.Info("-big.Int", "-11,122,233,344,455,567,899,900", bd) + } + { //uint256 + ua, _ := uint256.FromDecimal("111222333444555678999") + ub, _ := uint256.FromDecimal("11122233344455567899900") + log.Info("uint256", "111,222,333,444,555,678,999", ua) + log.Info("uint256", "11,122,233,344,455,567,899,900", ub) + } + { // int64 + log.Info("int64", "1,000,000", int64(1000000)) + log.Info("int64", "-1,000,000", int64(-1000000)) + log.Info("int64", "9,223,372,036,854,775,807", math.MaxInt64) + log.Info("int64", "-9,223,372,036,854,775,808", math.MinInt64) + } + { // uint64 + log.Info("uint64", "1,000,000", uint64(1000000)) + log.Info("uint64", "18,446,744,073,709,551,615", uint64(math.MaxUint64)) + } + { // Special characters + log.Info("Special chars in value", "key", "special \r\n\t chars") + log.Info("Special chars in key", "special \n\t chars", "value") + + log.Info("nospace", "nospace", "nospace") + log.Info("with space", "with nospace", "with nospace") + + log.Info("Bash escapes in value", "key", "\u001b[1G\u001b[K\u001b[1A") + log.Info("Bash escapes in key", "\u001b[1G\u001b[K\u001b[1A", "value") + + log.Info("Bash escapes in message \u001b[1G\u001b[K\u001b[1A end", "key", "value") + + colored := fmt.Sprintf("\u001B[%dmColored\u001B[0m[", 35) + log.Info(colored, colored, colored) + } + { // Custom Stringer() - type + log.Info("Custom Stringer value", "2562047h47m16.854s", common.PrettyDuration(time.Duration(9223372036854775807))) + } + { // Lazy eval + log.Info("Lazy evaluation of value", "key", log.Lazy{Fn: func() interface{} { return "lazy value" }}) + } + return nil +} diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 521517c599..b3b940ca93 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -19,7 +19,6 @@ package main import ( "fmt" - "math/big" "os" "sort" "strconv" @@ -46,7 +45,6 @@ import ( _ "github.com/ethereum/go-ethereum/eth/tracers/js" _ "github.com/ethereum/go-ethereum/eth/tracers/native" - "github.com/holiman/uint256" "github.com/urfave/cli/v2" ) @@ -473,28 +471,3 @@ func unlockAccounts(ctx *cli.Context, stack *node.Node) { unlockAccount(ks, account, i, passwords) } } - -// logTest is an entry point which spits out some logs. This is used by testing -// to verify expected outputs -func logTest(ctx *cli.Context) error { - ba, _ := new(big.Int).SetString("111222333444555678999", 10) // "111,222,333,444,555,678,999" - bb, _ := new(big.Int).SetString("-111222333444555678999", 10) // "-111,222,333,444,555,678,999" - bc, _ := new(big.Int).SetString("11122233344455567899900", 10) // "11,122,233,344,455,567,899,900" - bd, _ := new(big.Int).SetString("-11122233344455567899900", 10) // "-11,122,233,344,455,567,899,900" - - ua, _ := uint256.FromDecimal("111222333444555678999") - ub, _ := uint256.FromDecimal("11122233344455567899900") - - log.Info("Output testing started", - "big.Int", ba, - "-big.Int", bb, - "big.Int", bc, - "-big.Int", bd) - log.Info("Testing uint256", - "uint256.Int", ua, - "uint256.Int", ub) - log.Info("Special chars", - "special \r\n\t chars", "special \r\n\t chars", - ) - return nil -} diff --git a/cmd/geth/testdata/logging/logtest-logfmt.txt b/cmd/geth/testdata/logging/logtest-logfmt.txt index ed5ee847c6..4703ebd921 100644 --- a/cmd/geth/testdata/logging/logtest-logfmt.txt +++ b/cmd/geth/testdata/logging/logtest-logfmt.txt @@ -1,3 +1,22 @@ -t=2023-10-18T11:29:46+0200 lvl=info msg="Output testing started" big.Int=111,222,333,444,555,678,999 -big.Int=-111,222,333,444,555,678,999 big.Int=11,122,233,344,455,567,899,900 -big.Int=-11,122,233,344,455,567,899,900 -t=2023-10-18T11:29:46+0200 lvl=info msg="Testing uint256" uint256.Int=111,222,333,444,555,678,999 uint256.Int=11,122,233,344,455,567,899,900 -t=2023-10-18T11:29:46+0200 lvl=info msg="Special chars" "special \r\n\t chars"="special \r\n\t chars" \ No newline at end of file +t=2023-10-18T14:10:59+0200 lvl=info msg=big.Int 111,222,333,444,555,678,999=111,222,333,444,555,678,999 +t=2023-10-18T14:10:59+0200 lvl=info msg=-big.Int -111,222,333,444,555,678,999=-111,222,333,444,555,678,999 +t=2023-10-18T14:10:59+0200 lvl=info msg=big.Int 11,122,233,344,455,567,899,900=11,122,233,344,455,567,899,900 +t=2023-10-18T14:10:59+0200 lvl=info msg=-big.Int -11,122,233,344,455,567,899,900=-11,122,233,344,455,567,899,900 +t=2023-10-18T14:10:59+0200 lvl=info msg=uint256 111,222,333,444,555,678,999=111,222,333,444,555,678,999 +t=2023-10-18T14:10:59+0200 lvl=info msg=uint256 11,122,233,344,455,567,899,900=11,122,233,344,455,567,899,900 +t=2023-10-18T14:10:59+0200 lvl=info msg=int64 1,000,000=1,000,000 +t=2023-10-18T14:10:59+0200 lvl=info msg=int64 -1,000,000=-1,000,000 +t=2023-10-18T14:10:59+0200 lvl=info msg=int64 9,223,372,036,854,775,807=9,223,372,036,854,775,807 +t=2023-10-18T14:10:59+0200 lvl=info msg=int64 -9,223,372,036,854,775,808=-9,223,372,036,854,775,808 +t=2023-10-18T14:10:59+0200 lvl=info msg=uint64 1,000,000=1,000,000 +t=2023-10-18T14:10:59+0200 lvl=info msg=uint64 18,446,744,073,709,551,615=18,446,744,073,709,551,615 +t=2023-10-18T14:10:59+0200 lvl=info msg="Special chars in value" key="special \r\n\t chars" +t=2023-10-18T14:10:59+0200 lvl=info msg="Special chars in key" "special \n\t chars"=value +t=2023-10-18T14:10:59+0200 lvl=info msg=nospace nospace=nospace +t=2023-10-18T14:10:59+0200 lvl=info msg="with space" "with nospace"="with nospace" +t=2023-10-18T14:10:59+0200 lvl=info msg="Bash escapes in value" key="\x1b[1G\x1b[K\x1b[1A" +t=2023-10-18T14:10:59+0200 lvl=info msg="Bash escapes in key" "\x1b[1G\x1b[K\x1b[1A"=value +t=2023-10-18T14:10:59+0200 lvl=info msg="Bash escapes in message \x1b[1G\x1b[K\x1b[1A end" key=value +t=2023-10-18T14:10:59+0200 lvl=info msg="\x1b[35mColored\x1b[0m[" "\x1b[35mColored\x1b[0m["="\x1b[35mColored\x1b[0m[" +t=2023-10-18T14:10:59+0200 lvl=info msg="Custom Stringer value" 2562047h47m16.854s=2562047h47m16.854s +t=2023-10-18T14:10:59+0200 lvl=info msg="Lazy evaluation of value" key="lazy value" diff --git a/cmd/geth/testdata/logging/logtest-terminal.txt b/cmd/geth/testdata/logging/logtest-terminal.txt index 8c82701715..5618968788 100644 --- a/cmd/geth/testdata/logging/logtest-terminal.txt +++ b/cmd/geth/testdata/logging/logtest-terminal.txt @@ -1,3 +1,22 @@ -INFO [XXXXXXXXXXXXXXXXXX] Output testing started big.Int=111,222,333,444,555,678,999 -big.Int=-111,222,333,444,555,678,999 big.Int=11,122,233,344,455,567,899,900 -big.Int=-11,122,233,344,455,567,899,900 -INFO [XXXXXXXXXXXXXXXXXX] Testing uint256 uint256.Int=111,222,333,444,555,678,999 uint256.Int=11,122,233,344,455,567,899,900 -INFO [XXXXXXXXXXXXXXXXXX] Special chars "special \r\n\t chars"="special \r\n\t chars" +INFO [10-18|14:11:31.106] big.Int 111,222,333,444,555,678,999=111,222,333,444,555,678,999 +INFO [10-18|14:11:31.106] -big.Int -111,222,333,444,555,678,999=-111,222,333,444,555,678,999 +INFO [10-18|14:11:31.106] big.Int 11,122,233,344,455,567,899,900=11,122,233,344,455,567,899,900 +INFO [10-18|14:11:31.106] -big.Int -11,122,233,344,455,567,899,900=-11,122,233,344,455,567,899,900 +INFO [10-18|14:11:31.106] uint256 111,222,333,444,555,678,999=111,222,333,444,555,678,999 +INFO [10-18|14:11:31.106] uint256 11,122,233,344,455,567,899,900=11,122,233,344,455,567,899,900 +INFO [10-18|14:11:31.106] int64 1,000,000=1,000,000 +INFO [10-18|14:11:31.106] int64 -1,000,000=-1,000,000 +INFO [10-18|14:11:31.106] int64 9,223,372,036,854,775,807=9,223,372,036,854,775,807 +INFO [10-18|14:11:31.106] int64 -9,223,372,036,854,775,808=-9,223,372,036,854,775,808 +INFO [10-18|14:11:31.106] uint64 1,000,000=1,000,000 +INFO [10-18|14:11:31.106] uint64 18,446,744,073,709,551,615=18,446,744,073,709,551,615 +INFO [10-18|14:11:31.106] Special chars in value key="special \r\n\t chars" +INFO [10-18|14:11:31.106] Special chars in key "special \n\t chars"=value +INFO [10-18|14:11:31.106] nospace nospace=nospace +INFO [10-18|14:11:31.106] with space "with nospace"="with nospace" +INFO [10-18|14:11:31.106] Bash escapes in value key="\x1b[1G\x1b[K\x1b[1A" +INFO [10-18|14:11:31.106] Bash escapes in key "\x1b[1G\x1b[K\x1b[1A"=value +INFO [10-18|14:11:31.106] "Bash escapes in message \x1b[1G\x1b[K\x1b[1A end" key=value +INFO [10-18|14:11:31.106] "\x1b[35mColored\x1b[0m[" "\x1b[35mColored\x1b[0m["="\x1b[35mColored\x1b[0m[" +INFO [10-18|14:11:31.106] Custom Stringer value 2562047h47m16.854s=2562047h47m16.854s +INFO [10-18|14:11:31.106] Lazy evaluation of value key="lazy value" diff --git a/log/format_test.go b/log/format_test.go index e08c1d1a4a..41e1809c38 100644 --- a/log/format_test.go +++ b/log/format_test.go @@ -1,105 +1,10 @@ package log import ( - "fmt" - "math" - "math/big" "math/rand" - "strings" "testing" - - "github.com/holiman/uint256" ) -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) - } - } -} - -func TestPrettyBigInt(t *testing.T) { - tests := []struct { - int string - s string - }{ - {"111222333444555678999", "111,222,333,444,555,678,999"}, - {"-111222333444555678999", "-111,222,333,444,555,678,999"}, - {"11122233344455567899900", "11,122,233,344,455,567,899,900"}, - {"-11122233344455567899900", "-11,122,233,344,455,567,899,900"}, - } - - for _, tt := range tests { - v, _ := new(big.Int).SetString(tt.int, 10) - if have := formatLogfmtBigInt(v); have != tt.s { - t.Errorf("invalid output %s, want %s", have, tt.s) - } - } -} - -func TestPrettyUint256(t *testing.T) { - tests := []struct { - int string - s string - }{ - {"111222333444555678999", "111,222,333,444,555,678,999"}, - {"11122233344455567899900", "11,122,233,344,455,567,899,900"}, - } - - for _, tt := range tests { - v := new(uint256.Int) - v.SetFromDecimal(tt.int) - if have := formatLogfmtUint256(v); have != tt.s { - t.Errorf("invalid output %s, want %s", have, tt.s) - } - } -} - var sink string func BenchmarkPrettyInt64Logfmt(b *testing.B) { @@ -115,47 +20,3 @@ func BenchmarkPrettyUint64Logfmt(b *testing.B) { sink = FormatLogfmtUint64(rand.Uint64()) } } - -func TestSanitation(t *testing.T) { - msg := "\u001b[1G\u001b[K\u001b[1A" - msg2 := "\u001b \u0000" - msg3 := "NiceMessage" - msg4 := "Space Message" - msg5 := "Enter\nMessage" - - for i, tt := range []struct { - msg string - want string - }{ - { - msg: msg, - want: fmt.Sprintf("] %q %q=%q\n", msg, msg, msg), - }, - { - msg: msg2, - want: fmt.Sprintf("] %q %q=%q\n", msg2, msg2, msg2), - }, - { - msg: msg3, - want: fmt.Sprintf("] %s %s=%s\n", msg3, msg3, msg3), - }, - { - msg: msg4, - want: fmt.Sprintf("] %s %q=%q\n", msg4, msg4, msg4), - }, - { - msg: msg5, - want: fmt.Sprintf("] %s %q=%q\n", msg5, msg5, msg5), - }, - } { - var ( - logger = New() - out = new(strings.Builder) - ) - logger.SetHandler(LvlFilterHandler(LvlInfo, StreamHandler(out, TerminalFormat(false)))) - logger.Info(tt.msg, tt.msg, tt.msg) - if have := out.String()[24:]; tt.want != have { - t.Fatalf("test %d: want / have: \n%v\n%v", i, tt.want, have) - } - } -}