Skip to content

Commit c90857a

Browse files
committed
cmd/geth: additional log output test
1 parent f66e099 commit c90857a

File tree

5 files changed

+126
-172
lines changed

5 files changed

+126
-172
lines changed

cmd/geth/logtestcmd.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2023 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"github.com/ethereum/go-ethereum/common"
22+
"github.com/ethereum/go-ethereum/log"
23+
"github.com/holiman/uint256"
24+
"github.com/urfave/cli/v2"
25+
"math"
26+
"math/big"
27+
"time"
28+
)
29+
30+
// logTest is an entry point which spits out some logs. This is used by testing
31+
// to verify expected outputs
32+
func logTest(ctx *cli.Context) error {
33+
34+
{ // big.Int
35+
ba, _ := new(big.Int).SetString("111222333444555678999", 10) // "111,222,333,444,555,678,999"
36+
bb, _ := new(big.Int).SetString("-111222333444555678999", 10) // "-111,222,333,444,555,678,999"
37+
bc, _ := new(big.Int).SetString("11122233344455567899900", 10) // "11,122,233,344,455,567,899,900"
38+
bd, _ := new(big.Int).SetString("-11122233344455567899900", 10) // "-11,122,233,344,455,567,899,900"
39+
log.Info("big.Int", "111,222,333,444,555,678,999", ba)
40+
log.Info("-big.Int", "-111,222,333,444,555,678,999", bb)
41+
log.Info("big.Int", "11,122,233,344,455,567,899,900", bc)
42+
log.Info("-big.Int", "-11,122,233,344,455,567,899,900", bd)
43+
}
44+
{ //uint256
45+
ua, _ := uint256.FromDecimal("111222333444555678999")
46+
ub, _ := uint256.FromDecimal("11122233344455567899900")
47+
log.Info("uint256", "111,222,333,444,555,678,999", ua)
48+
log.Info("uint256", "11,122,233,344,455,567,899,900", ub)
49+
}
50+
{ // int64
51+
log.Info("int64", "1,000,000", int64(1000000))
52+
log.Info("int64", "-1,000,000", int64(-1000000))
53+
log.Info("int64", "9,223,372,036,854,775,807", math.MaxInt64)
54+
log.Info("int64", "-9,223,372,036,854,775,808", math.MinInt64)
55+
}
56+
{ // uint64
57+
log.Info("uint64", "1,000,000", uint64(1000000))
58+
log.Info("uint64", "18,446,744,073,709,551,615", uint64(math.MaxUint64))
59+
}
60+
{ // Special characters
61+
log.Info("Special chars in value", "key", "special \r\n\t chars")
62+
log.Info("Special chars in key", "special \n\t chars", "value")
63+
64+
log.Info("nospace", "nospace", "nospace")
65+
log.Info("with space", "with nospace", "with nospace")
66+
67+
log.Info("Bash escapes in value", "key", "\u001b[1G\u001b[K\u001b[1A")
68+
log.Info("Bash escapes in key", "\u001b[1G\u001b[K\u001b[1A", "value")
69+
70+
log.Info("Bash escapes in message \u001b[1G\u001b[K\u001b[1A end", "key", "value")
71+
72+
colored := fmt.Sprintf("\u001B[%dmColored\u001B[0m[", 35)
73+
log.Info(colored, colored, colored)
74+
}
75+
{ // Custom Stringer() - type
76+
log.Info("Custom Stringer value", "2562047h47m16.854s", common.PrettyDuration(time.Duration(9223372036854775807)))
77+
}
78+
{ // Lazy eval
79+
log.Info("Lazy evaluation of value", "key", log.Lazy{Fn: func() interface{} { return "lazy value" }})
80+
}
81+
return nil
82+
}

cmd/geth/main.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package main
1919

2020
import (
2121
"fmt"
22-
"math/big"
2322
"os"
2423
"sort"
2524
"strconv"
@@ -46,7 +45,6 @@ import (
4645
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
4746
_ "github.com/ethereum/go-ethereum/eth/tracers/native"
4847

49-
"github.com/holiman/uint256"
5048
"github.com/urfave/cli/v2"
5149
)
5250

@@ -473,28 +471,3 @@ func unlockAccounts(ctx *cli.Context, stack *node.Node) {
473471
unlockAccount(ks, account, i, passwords)
474472
}
475473
}
476-
477-
// logTest is an entry point which spits out some logs. This is used by testing
478-
// to verify expected outputs
479-
func logTest(ctx *cli.Context) error {
480-
ba, _ := new(big.Int).SetString("111222333444555678999", 10) // "111,222,333,444,555,678,999"
481-
bb, _ := new(big.Int).SetString("-111222333444555678999", 10) // "-111,222,333,444,555,678,999"
482-
bc, _ := new(big.Int).SetString("11122233344455567899900", 10) // "11,122,233,344,455,567,899,900"
483-
bd, _ := new(big.Int).SetString("-11122233344455567899900", 10) // "-11,122,233,344,455,567,899,900"
484-
485-
ua, _ := uint256.FromDecimal("111222333444555678999")
486-
ub, _ := uint256.FromDecimal("11122233344455567899900")
487-
488-
log.Info("Output testing started",
489-
"big.Int", ba,
490-
"-big.Int", bb,
491-
"big.Int", bc,
492-
"-big.Int", bd)
493-
log.Info("Testing uint256",
494-
"uint256.Int", ua,
495-
"uint256.Int", ub)
496-
log.Info("Special chars",
497-
"special \r\n\t chars", "special \r\n\t chars",
498-
)
499-
return nil
500-
}
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1-
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
2-
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
3-
t=2023-10-18T11:29:46+0200 lvl=info msg="Special chars" "special \r\n\t chars"="special \r\n\t chars"
1+
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
2+
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
3+
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
4+
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
5+
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
6+
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
7+
t=2023-10-18T14:10:59+0200 lvl=info msg=int64 1,000,000=1,000,000
8+
t=2023-10-18T14:10:59+0200 lvl=info msg=int64 -1,000,000=-1,000,000
9+
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
10+
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
11+
t=2023-10-18T14:10:59+0200 lvl=info msg=uint64 1,000,000=1,000,000
12+
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
13+
t=2023-10-18T14:10:59+0200 lvl=info msg="Special chars in value" key="special \r\n\t chars"
14+
t=2023-10-18T14:10:59+0200 lvl=info msg="Special chars in key" "special \n\t chars"=value
15+
t=2023-10-18T14:10:59+0200 lvl=info msg=nospace nospace=nospace
16+
t=2023-10-18T14:10:59+0200 lvl=info msg="with space" "with nospace"="with nospace"
17+
t=2023-10-18T14:10:59+0200 lvl=info msg="Bash escapes in value" key="\x1b[1G\x1b[K\x1b[1A"
18+
t=2023-10-18T14:10:59+0200 lvl=info msg="Bash escapes in key" "\x1b[1G\x1b[K\x1b[1A"=value
19+
t=2023-10-18T14:10:59+0200 lvl=info msg="Bash escapes in message \x1b[1G\x1b[K\x1b[1A end" key=value
20+
t=2023-10-18T14:10:59+0200 lvl=info msg="\x1b[35mColored\x1b[0m[" "\x1b[35mColored\x1b[0m["="\x1b[35mColored\x1b[0m["
21+
t=2023-10-18T14:10:59+0200 lvl=info msg="Custom Stringer value" 2562047h47m16.854s=2562047h47m16.854s
22+
t=2023-10-18T14:10:59+0200 lvl=info msg="Lazy evaluation of value" key="lazy value"
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1-
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
2-
INFO [XXXXXXXXXXXXXXXXXX] Testing uint256 uint256.Int=111,222,333,444,555,678,999 uint256.Int=11,122,233,344,455,567,899,900
3-
INFO [XXXXXXXXXXXXXXXXXX] Special chars "special \r\n\t chars"="special \r\n\t chars"
1+
INFO [10-18|14:11:31.106] big.Int 111,222,333,444,555,678,999=111,222,333,444,555,678,999
2+
INFO [10-18|14:11:31.106] -big.Int -111,222,333,444,555,678,999=-111,222,333,444,555,678,999
3+
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
4+
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
5+
INFO [10-18|14:11:31.106] uint256 111,222,333,444,555,678,999=111,222,333,444,555,678,999
6+
INFO [10-18|14:11:31.106] uint256 11,122,233,344,455,567,899,900=11,122,233,344,455,567,899,900
7+
INFO [10-18|14:11:31.106] int64 1,000,000=1,000,000
8+
INFO [10-18|14:11:31.106] int64 -1,000,000=-1,000,000
9+
INFO [10-18|14:11:31.106] int64 9,223,372,036,854,775,807=9,223,372,036,854,775,807
10+
INFO [10-18|14:11:31.106] int64 -9,223,372,036,854,775,808=-9,223,372,036,854,775,808
11+
INFO [10-18|14:11:31.106] uint64 1,000,000=1,000,000
12+
INFO [10-18|14:11:31.106] uint64 18,446,744,073,709,551,615=18,446,744,073,709,551,615
13+
INFO [10-18|14:11:31.106] Special chars in value key="special \r\n\t chars"
14+
INFO [10-18|14:11:31.106] Special chars in key "special \n\t chars"=value
15+
INFO [10-18|14:11:31.106] nospace nospace=nospace
16+
INFO [10-18|14:11:31.106] with space "with nospace"="with nospace"
17+
INFO [10-18|14:11:31.106] Bash escapes in value key="\x1b[1G\x1b[K\x1b[1A"
18+
INFO [10-18|14:11:31.106] Bash escapes in key "\x1b[1G\x1b[K\x1b[1A"=value
19+
INFO [10-18|14:11:31.106] "Bash escapes in message \x1b[1G\x1b[K\x1b[1A end" key=value
20+
INFO [10-18|14:11:31.106] "\x1b[35mColored\x1b[0m[" "\x1b[35mColored\x1b[0m["="\x1b[35mColored\x1b[0m["
21+
INFO [10-18|14:11:31.106] Custom Stringer value 2562047h47m16.854s=2562047h47m16.854s
22+
INFO [10-18|14:11:31.106] Lazy evaluation of value key="lazy value"

log/format_test.go

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,10 @@
11
package log
22

33
import (
4-
"fmt"
5-
"math"
6-
"math/big"
74
"math/rand"
8-
"strings"
95
"testing"
10-
11-
"github.com/holiman/uint256"
126
)
137

14-
func TestPrettyInt64(t *testing.T) {
15-
tests := []struct {
16-
n int64
17-
s string
18-
}{
19-
{0, "0"},
20-
{10, "10"},
21-
{-10, "-10"},
22-
{100, "100"},
23-
{-100, "-100"},
24-
{1000, "1000"},
25-
{-1000, "-1000"},
26-
{10000, "10000"},
27-
{-10000, "-10000"},
28-
{99999, "99999"},
29-
{-99999, "-99999"},
30-
{100000, "100,000"},
31-
{-100000, "-100,000"},
32-
{1000000, "1,000,000"},
33-
{-1000000, "-1,000,000"},
34-
{math.MaxInt64, "9,223,372,036,854,775,807"},
35-
{math.MinInt64, "-9,223,372,036,854,775,808"},
36-
}
37-
for i, tt := range tests {
38-
if have := FormatLogfmtInt64(tt.n); have != tt.s {
39-
t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
40-
}
41-
}
42-
}
43-
44-
func TestPrettyUint64(t *testing.T) {
45-
tests := []struct {
46-
n uint64
47-
s string
48-
}{
49-
{0, "0"},
50-
{10, "10"},
51-
{100, "100"},
52-
{1000, "1000"},
53-
{10000, "10000"},
54-
{99999, "99999"},
55-
{100000, "100,000"},
56-
{1000000, "1,000,000"},
57-
{math.MaxUint64, "18,446,744,073,709,551,615"},
58-
}
59-
for i, tt := range tests {
60-
if have := FormatLogfmtUint64(tt.n); have != tt.s {
61-
t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
62-
}
63-
}
64-
}
65-
66-
func TestPrettyBigInt(t *testing.T) {
67-
tests := []struct {
68-
int string
69-
s string
70-
}{
71-
{"111222333444555678999", "111,222,333,444,555,678,999"},
72-
{"-111222333444555678999", "-111,222,333,444,555,678,999"},
73-
{"11122233344455567899900", "11,122,233,344,455,567,899,900"},
74-
{"-11122233344455567899900", "-11,122,233,344,455,567,899,900"},
75-
}
76-
77-
for _, tt := range tests {
78-
v, _ := new(big.Int).SetString(tt.int, 10)
79-
if have := formatLogfmtBigInt(v); have != tt.s {
80-
t.Errorf("invalid output %s, want %s", have, tt.s)
81-
}
82-
}
83-
}
84-
85-
func TestPrettyUint256(t *testing.T) {
86-
tests := []struct {
87-
int string
88-
s string
89-
}{
90-
{"111222333444555678999", "111,222,333,444,555,678,999"},
91-
{"11122233344455567899900", "11,122,233,344,455,567,899,900"},
92-
}
93-
94-
for _, tt := range tests {
95-
v := new(uint256.Int)
96-
v.SetFromDecimal(tt.int)
97-
if have := formatLogfmtUint256(v); have != tt.s {
98-
t.Errorf("invalid output %s, want %s", have, tt.s)
99-
}
100-
}
101-
}
102-
1038
var sink string
1049

10510
func BenchmarkPrettyInt64Logfmt(b *testing.B) {
@@ -115,47 +20,3 @@ func BenchmarkPrettyUint64Logfmt(b *testing.B) {
11520
sink = FormatLogfmtUint64(rand.Uint64())
11621
}
11722
}
118-
119-
func TestSanitation(t *testing.T) {
120-
msg := "\u001b[1G\u001b[K\u001b[1A"
121-
msg2 := "\u001b \u0000"
122-
msg3 := "NiceMessage"
123-
msg4 := "Space Message"
124-
msg5 := "Enter\nMessage"
125-
126-
for i, tt := range []struct {
127-
msg string
128-
want string
129-
}{
130-
{
131-
msg: msg,
132-
want: fmt.Sprintf("] %q %q=%q\n", msg, msg, msg),
133-
},
134-
{
135-
msg: msg2,
136-
want: fmt.Sprintf("] %q %q=%q\n", msg2, msg2, msg2),
137-
},
138-
{
139-
msg: msg3,
140-
want: fmt.Sprintf("] %s %s=%s\n", msg3, msg3, msg3),
141-
},
142-
{
143-
msg: msg4,
144-
want: fmt.Sprintf("] %s %q=%q\n", msg4, msg4, msg4),
145-
},
146-
{
147-
msg: msg5,
148-
want: fmt.Sprintf("] %s %q=%q\n", msg5, msg5, msg5),
149-
},
150-
} {
151-
var (
152-
logger = New()
153-
out = new(strings.Builder)
154-
)
155-
logger.SetHandler(LvlFilterHandler(LvlInfo, StreamHandler(out, TerminalFormat(false))))
156-
logger.Info(tt.msg, tt.msg, tt.msg)
157-
if have := out.String()[24:]; tt.want != have {
158-
t.Fatalf("test %d: want / have: \n%v\n%v", i, tt.want, have)
159-
}
160-
}
161-
}

0 commit comments

Comments
 (0)