mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
log: add special casing of uint256 into the logger (#26936)
This commit is contained in:
parent
5eca853e52
commit
2359a9ed34
2 changed files with 61 additions and 1 deletions
|
|
@ -12,6 +12,8 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"github.com/holiman/uint256"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -339,12 +341,20 @@ func formatLogfmtValue(value interface{}, term bool) string {
|
||||||
return v.Format(timeFormat)
|
return v.Format(timeFormat)
|
||||||
|
|
||||||
case *big.Int:
|
case *big.Int:
|
||||||
// Big ints get consumed by the Stringer clause so we need to handle
|
// Big ints get consumed by the Stringer clause, so we need to handle
|
||||||
// them earlier on.
|
// them earlier on.
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return "<nil>"
|
return "<nil>"
|
||||||
}
|
}
|
||||||
return formatLogfmtBigInt(v)
|
return formatLogfmtBigInt(v)
|
||||||
|
|
||||||
|
case *uint256.Int:
|
||||||
|
// Uint256s get consumed by the Stringer clause, so we need to handle
|
||||||
|
// them earlier on.
|
||||||
|
if v == nil {
|
||||||
|
return "<nil>"
|
||||||
|
}
|
||||||
|
return formatLogfmtUint256(v)
|
||||||
}
|
}
|
||||||
if term {
|
if term {
|
||||||
if s, ok := value.(TerminalStringer); ok {
|
if s, ok := value.(TerminalStringer); ok {
|
||||||
|
|
@ -469,6 +479,36 @@ func formatLogfmtBigInt(n *big.Int) string {
|
||||||
return string(buf[i+1:])
|
return string(buf[i+1:])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// formatLogfmtUint256 formats n with thousand separators.
|
||||||
|
func formatLogfmtUint256(n *uint256.Int) string {
|
||||||
|
if n.IsUint64() {
|
||||||
|
return FormatLogfmtUint64(n.Uint64())
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
text = n.Dec()
|
||||||
|
buf = make([]byte, len(text)+len(text)/3)
|
||||||
|
comma = 0
|
||||||
|
i = len(buf) - 1
|
||||||
|
)
|
||||||
|
for j := len(text) - 1; j >= 0; j, i = j-1, i-1 {
|
||||||
|
c := text[j]
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case c == '-':
|
||||||
|
buf[i] = c
|
||||||
|
case comma == 3:
|
||||||
|
buf[i] = ','
|
||||||
|
i--
|
||||||
|
comma = 0
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
buf[i] = c
|
||||||
|
comma++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(buf[i+1:])
|
||||||
|
}
|
||||||
|
|
||||||
// escapeString checks if the provided string needs escaping/quoting, and
|
// escapeString checks if the provided string needs escaping/quoting, and
|
||||||
// calls strconv.Quote if needed
|
// calls strconv.Quote if needed
|
||||||
func escapeString(s string) string {
|
func escapeString(s string) string {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/holiman/uint256"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPrettyInt64(t *testing.T) {
|
func TestPrettyInt64(t *testing.T) {
|
||||||
|
|
@ -80,6 +82,24 @@ func TestPrettyBigInt(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
var sink string
|
||||||
|
|
||||||
func BenchmarkPrettyInt64Logfmt(b *testing.B) {
|
func BenchmarkPrettyInt64Logfmt(b *testing.B) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue