|
| 1 | +//go:build integrationtests |
| 2 | + |
| 3 | +// Copyright 2023 The go-ethereum Authors |
| 4 | +// This file is part of go-ethereum. |
| 5 | +// |
| 6 | +// go-ethereum is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU General Public License as published by |
| 8 | +// the Free Software Foundation, either version 3 of the License, or |
| 9 | +// (at your option) any later version. |
| 10 | +// |
| 11 | +// go-ethereum is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU General Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU General Public License |
| 17 | +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "math" |
| 24 | + "math/big" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/ethereum/go-ethereum/common" |
| 28 | + "github.com/ethereum/go-ethereum/log" |
| 29 | + "github.com/holiman/uint256" |
| 30 | + "github.com/urfave/cli/v2" |
| 31 | +) |
| 32 | + |
| 33 | +var logTestCommand = &cli.Command{ |
| 34 | + Action: logTest, |
| 35 | + Name: "logtest", |
| 36 | + Usage: "Print some log messages", |
| 37 | + ArgsUsage: " ", |
| 38 | + Description: ` |
| 39 | +This command is only meant for testing. |
| 40 | +`} |
| 41 | + |
| 42 | +// logTest is an entry point which spits out some logs. This is used by testing |
| 43 | +// to verify expected outputs |
| 44 | +func logTest(ctx *cli.Context) error { |
| 45 | + log.ResetGlobalState() |
| 46 | + { // big.Int |
| 47 | + ba, _ := new(big.Int).SetString("111222333444555678999", 10) // "111,222,333,444,555,678,999" |
| 48 | + bb, _ := new(big.Int).SetString("-111222333444555678999", 10) // "-111,222,333,444,555,678,999" |
| 49 | + bc, _ := new(big.Int).SetString("11122233344455567899900", 10) // "11,122,233,344,455,567,899,900" |
| 50 | + bd, _ := new(big.Int).SetString("-11122233344455567899900", 10) // "-11,122,233,344,455,567,899,900" |
| 51 | + log.Info("big.Int", "111,222,333,444,555,678,999", ba) |
| 52 | + log.Info("-big.Int", "-111,222,333,444,555,678,999", bb) |
| 53 | + log.Info("big.Int", "11,122,233,344,455,567,899,900", bc) |
| 54 | + log.Info("-big.Int", "-11,122,233,344,455,567,899,900", bd) |
| 55 | + } |
| 56 | + { //uint256 |
| 57 | + ua, _ := uint256.FromDecimal("111222333444555678999") |
| 58 | + ub, _ := uint256.FromDecimal("11122233344455567899900") |
| 59 | + log.Info("uint256", "111,222,333,444,555,678,999", ua) |
| 60 | + log.Info("uint256", "11,122,233,344,455,567,899,900", ub) |
| 61 | + } |
| 62 | + { // int64 |
| 63 | + log.Info("int64", "1,000,000", int64(1000000)) |
| 64 | + log.Info("int64", "-1,000,000", int64(-1000000)) |
| 65 | + log.Info("int64", "9,223,372,036,854,775,807", int64(math.MaxInt64)) |
| 66 | + log.Info("int64", "-9,223,372,036,854,775,808", int64(math.MinInt64)) |
| 67 | + } |
| 68 | + { // uint64 |
| 69 | + log.Info("uint64", "1,000,000", uint64(1000000)) |
| 70 | + log.Info("uint64", "18,446,744,073,709,551,615", uint64(math.MaxUint64)) |
| 71 | + } |
| 72 | + { // Special characters |
| 73 | + log.Info("Special chars in value", "key", "special \r\n\t chars") |
| 74 | + log.Info("Special chars in key", "special \n\t chars", "value") |
| 75 | + |
| 76 | + log.Info("nospace", "nospace", "nospace") |
| 77 | + log.Info("with space", "with nospace", "with nospace") |
| 78 | + |
| 79 | + log.Info("Bash escapes in value", "key", "\u001b[1G\u001b[K\u001b[1A") |
| 80 | + log.Info("Bash escapes in key", "\u001b[1G\u001b[K\u001b[1A", "value") |
| 81 | + |
| 82 | + log.Info("Bash escapes in message \u001b[1G\u001b[K\u001b[1A end", "key", "value") |
| 83 | + |
| 84 | + colored := fmt.Sprintf("\u001B[%dmColored\u001B[0m[", 35) |
| 85 | + log.Info(colored, colored, colored) |
| 86 | + } |
| 87 | + { // Custom Stringer() - type |
| 88 | + log.Info("Custom Stringer value", "2562047h47m16.854s", common.PrettyDuration(time.Duration(9223372036854775807))) |
| 89 | + } |
| 90 | + { // Lazy eval |
| 91 | + log.Info("Lazy evaluation of value", "key", log.Lazy{Fn: func() interface{} { return "lazy value" }}) |
| 92 | + } |
| 93 | + { // Multi-line message |
| 94 | + log.Info("A message with wonky \U0001F4A9 characters") |
| 95 | + log.Info("A multiline message \nINFO [10-18|14:11:31.106] with wonky characters \U0001F4A9") |
| 96 | + log.Info("A multiline message \nLALA [ZZZZZZZZZZZZZZZZZZ] Actually part of message above") |
| 97 | + } |
| 98 | + { // Miscellaneous json-quirks |
| 99 | + // This will check if the json output uses strings or json-booleans to represent bool values |
| 100 | + log.Info("boolean", "true", true, "false", false) |
| 101 | + // Handling of duplicate keys. |
| 102 | + // This is actually ill-handled by the current handler: the format.go |
| 103 | + // uses a global 'fieldPadding' map and mixes up the two keys. If 'alpha' |
| 104 | + // is shorter than beta, it sometimes causes erroneous padding -- and what's more |
| 105 | + // it causes _different_ padding in multi-handler context, e.g. both file- |
| 106 | + // and console output, making the two mismatch. |
| 107 | + log.Info("repeated-key 1", "foo", "alpha", "foo", "beta") |
| 108 | + log.Info("repeated-key 2", "xx", "short", "xx", "longer") |
| 109 | + } |
| 110 | + { // loglevels |
| 111 | + log.Debug("log at level debug") |
| 112 | + log.Trace("log at level trace") |
| 113 | + log.Info("log at level info") |
| 114 | + log.Warn("log at level warn") |
| 115 | + log.Error("log at level error") |
| 116 | + } |
| 117 | + { |
| 118 | + // The current log formatter has a global map of paddings, storing the |
| 119 | + // longest seen padding per key in a map. This results in a statefulness |
| 120 | + // which has some odd side-effects. Demonstrated here: |
| 121 | + log.Info("test", "bar", "short", "a", "aligned left") |
| 122 | + log.Info("test", "bar", "a long message", "a", 1) |
| 123 | + log.Info("test", "bar", "short", "a", "aligned right") |
| 124 | + } |
| 125 | + { |
| 126 | + // This sequence of logs should be output with alignment, so each field becoems a column. |
| 127 | + log.Info("The following logs should align so that the key-fields make 5 columns") |
| 128 | + log.Info("Inserted known block", "number", 1_012, "hash", common.HexToHash("0x1234"), "txs", 200, "gas", 1_123_123, "other", "first") |
| 129 | + log.Info("Inserted new block", "number", 1, "hash", common.HexToHash("0x1235"), "txs", 2, "gas", 1_123, "other", "second") |
| 130 | + log.Info("Inserted known block", "number", 99, "hash", common.HexToHash("0x12322"), "txs", 10, "gas", 1, "other", "third") |
| 131 | + log.Warn("Inserted known block", "number", 1_012, "hash", common.HexToHash("0x1234"), "txs", 200, "gas", 99, "other", "fourth") |
| 132 | + } |
| 133 | + return nil |
| 134 | +} |
0 commit comments