Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
e523a7fb
Unverified
Commit
e523a7fb
authored
Jun 28, 2022
by
Diederik Loerakker
Committed by
GitHub
Jun 27, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(op-node): testlog alignment and color by default (#2854)
parent
33a59146
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
2 deletions
+44
-2
README.md
op-node/testlog/README.md
+5
-0
testlog.go
op-node/testlog/testlog.go
+39
-2
No files found.
op-node/testlog/README.md
View file @
e523a7fb
...
...
@@ -4,3 +4,8 @@
Since we use the same logging, but as an external package, we have to move the test utility to our own internal package.
This fork also made minor modifications:
-
Enable color by default.
-
Add
`estimateInfoLen`
and use this for message padding in
`flush()`
to align the contents of the log entries,
compensating for the different lengths of the log decoration that the Go library adds.
op-node/testlog/testlog.go
View file @
e523a7fb
...
...
@@ -18,6 +18,9 @@
package
testlog
import
(
"runtime"
"strconv"
"strings"
"sync"
"testing"
...
...
@@ -66,7 +69,7 @@ func Logger(t *testing.T, level log.Lvl) log.Logger {
t
:
t
,
l
:
log
.
New
(),
mu
:
new
(
sync
.
Mutex
),
h
:
&
bufHandler
{
fmt
:
log
.
TerminalFormat
(
fals
e
)},
h
:
&
bufHandler
{
fmt
:
log
.
TerminalFormat
(
tru
e
)},
}
l
.
l
.
SetHandler
(
log
.
LvlFilterHandler
(
level
,
l
.
h
))
return
l
...
...
@@ -135,8 +138,42 @@ func (l *logger) SetHandler(h log.Handler) {
// flush writes all buffered messages and clears the buffer.
func
(
l
*
logger
)
flush
()
{
l
.
t
.
Helper
()
// 2 frame skip for flush() + public logger fn
decorationLen
:=
estimateInfoLen
(
2
)
padding
:=
20
if
decorationLen
<=
25
{
padding
=
25
-
decorationLen
}
for
_
,
r
:=
range
l
.
h
.
buf
{
l
.
t
.
Logf
(
"%
s
"
,
l
.
h
.
fmt
.
Format
(
r
))
l
.
t
.
Logf
(
"%
*s%s"
,
padding
,
"
"
,
l
.
h
.
fmt
.
Format
(
r
))
}
l
.
h
.
buf
=
nil
}
// The Go testing lib uses the runtime package to get info about the calling site, and then decorates the line.
// We can't disable this decoration, but we can adjust the contents to align by padding after the info.
// To pad the right amount, we estimate how long the info is.
func
estimateInfoLen
(
frameSkip
int
)
int
{
var
pc
[
50
]
uintptr
// Skip two extra frames to account for this function
// and runtime.Callers itself.
n
:=
runtime
.
Callers
(
frameSkip
+
2
,
pc
[
:
])
if
n
==
0
{
return
8
}
frames
:=
runtime
.
CallersFrames
(
pc
[
:
n
])
frame
,
_
:=
frames
.
Next
()
file
:=
frame
.
File
line
:=
frame
.
Line
if
file
!=
""
{
// Truncate file name at last file name separator.
if
index
:=
strings
.
LastIndex
(
file
,
"/"
);
index
>=
0
{
file
=
file
[
index
+
1
:
]
}
else
if
index
=
strings
.
LastIndex
(
file
,
"
\\
"
);
index
>=
0
{
file
=
file
[
index
+
1
:
]
}
return
4
+
len
(
file
)
+
1
+
len
(
strconv
.
FormatInt
(
int64
(
line
),
10
))
}
else
{
return
8
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment