Skip to content

Commit 2359a9e

Browse files
committed
log: add special casing of uint256 into the logger (ethereum#26936)
1 parent 5eca853 commit 2359a9e

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

log/format.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"sync/atomic"
1313
"time"
1414
"unicode/utf8"
15+
16+
"github.com/holiman/uint256"
1517
)
1618

1719
const (
@@ -339,12 +341,20 @@ func formatLogfmtValue(value interface{}, term bool) string {
339341
return v.Format(timeFormat)
340342

341343
case *big.Int:
342-
// Big ints get consumed by the Stringer clause so we need to handle
344+
// Big ints get consumed by the Stringer clause, so we need to handle
343345
// them earlier on.
344346
if v == nil {
345347
return "<nil>"
346348
}
347349
return formatLogfmtBigInt(v)
350+
351+
case *uint256.Int:
352+
// Uint256s get consumed by the Stringer clause, so we need to handle
353+
// them earlier on.
354+
if v == nil {
355+
return "<nil>"
356+
}
357+
return formatLogfmtUint256(v)
348358
}
349359
if term {
350360
if s, ok := value.(TerminalStringer); ok {
@@ -469,6 +479,36 @@ func formatLogfmtBigInt(n *big.Int) string {
469479
return string(buf[i+1:])
470480
}
471481

482+
// formatLogfmtUint256 formats n with thousand separators.
483+
func formatLogfmtUint256(n *uint256.Int) string {
484+
if n.IsUint64() {
485+
return FormatLogfmtUint64(n.Uint64())
486+
}
487+
var (
488+
text = n.Dec()
489+
buf = make([]byte, len(text)+len(text)/3)
490+
comma = 0
491+
i = len(buf) - 1
492+
)
493+
for j := len(text) - 1; j >= 0; j, i = j-1, i-1 {
494+
c := text[j]
495+
496+
switch {
497+
case c == '-':
498+
buf[i] = c
499+
case comma == 3:
500+
buf[i] = ','
501+
i--
502+
comma = 0
503+
fallthrough
504+
default:
505+
buf[i] = c
506+
comma++
507+
}
508+
}
509+
return string(buf[i+1:])
510+
}
511+
472512
// escapeString checks if the provided string needs escaping/quoting, and
473513
// calls strconv.Quote if needed
474514
func escapeString(s string) string {

log/format_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"math/rand"
88
"strings"
99
"testing"
10+
11+
"github.com/holiman/uint256"
1012
)
1113

1214
func TestPrettyInt64(t *testing.T) {
@@ -80,6 +82,24 @@ func TestPrettyBigInt(t *testing.T) {
8082
}
8183
}
8284

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+
83103
var sink string
84104

85105
func BenchmarkPrettyInt64Logfmt(b *testing.B) {

0 commit comments

Comments
 (0)