Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
f89dd627
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
f89dd627
编写于
2月 23, 2017
作者:
P
Péter Szilágyi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
internal, log: support debug log prints, displaying log origins
上级
1ca20a26
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
61 addition
and
7 deletion
+61
-7
internal/debug/flags.go
internal/debug/flags.go
+11
-3
log/format.go
log/format.go
+50
-4
未找到文件。
internal/debug/flags.go
浏览文件 @
f89dd627
...
...
@@ -30,12 +30,12 @@ import (
var
(
verbosityFlag
=
cli
.
IntFlag
{
Name
:
"verbosity"
,
Usage
:
"Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=
core, 5=debug, 6
=detail"
,
Usage
:
"Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=
debug, 5
=detail"
,
Value
:
3
,
}
vmoduleFlag
=
cli
.
StringFlag
{
Name
:
"vmodule"
,
Usage
:
"Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=
6,p2p=5
)"
,
Usage
:
"Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=
5,p2p=4
)"
,
Value
:
""
,
}
backtraceAtFlag
=
cli
.
StringFlag
{
...
...
@@ -43,6 +43,10 @@ var (
Usage
:
"Request a stack trace at a specific logging statement (e.g.
\"
block.go:271
\"
)"
,
Value
:
""
,
}
debugFlag
=
cli
.
BoolFlag
{
Name
:
"debug"
,
Usage
:
"Prepends log messages with call-site location (file and line number)"
,
}
pprofFlag
=
cli
.
BoolFlag
{
Name
:
"pprof"
,
Usage
:
"Enable the pprof HTTP server"
,
...
...
@@ -78,17 +82,21 @@ var (
// Flags holds all command-line flags required for debugging.
var
Flags
=
[]
cli
.
Flag
{
verbosityFlag
,
vmoduleFlag
,
backtraceAtFlag
,
verbosityFlag
,
vmoduleFlag
,
backtraceAtFlag
,
debugFlag
,
pprofFlag
,
pprofAddrFlag
,
pprofPortFlag
,
memprofilerateFlag
,
blockprofilerateFlag
,
cpuprofileFlag
,
traceFlag
,
}
// glogger is the glog handler used by Geth, allowing the debug APIs to modify
// verbosity levels, vmodules and backtrace locations.
var
glogger
=
log
.
NewGlogHandler
(
log
.
StreamHandler
(
os
.
Stderr
,
log
.
TerminalFormat
()))
// Setup initializes profiling and logging based on the CLI flags.
// It should be called as early as possible in the program.
func
Setup
(
ctx
*
cli
.
Context
)
error
{
// logging
log
.
PrintOrigins
(
ctx
.
GlobalBool
(
debugFlag
.
Name
))
glogger
.
Verbosity
(
log
.
Lvl
(
ctx
.
GlobalInt
(
verbosityFlag
.
Name
)))
glogger
.
Vmodule
(
ctx
.
GlobalString
(
vmoduleFlag
.
Name
))
glogger
.
BacktraceAt
(
ctx
.
GlobalString
(
backtraceAtFlag
.
Name
))
...
...
log/format.go
浏览文件 @
f89dd627
...
...
@@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
...
...
@@ -18,6 +19,30 @@ const (
termMsgJust
=
40
)
// locationTrims are trimmed for display to avoid unwieldy log lines.
var
locationTrims
=
[]
string
{
"github.com/ethereum/go-ethereum/"
,
"github.com/ethereum/ethash/"
,
}
// PrintOrigins sets or unsets log location (file:line) printing for terminal
// format output.
func
PrintOrigins
(
print
bool
)
{
if
print
{
atomic
.
StoreUint32
(
&
locationEnabled
,
1
)
}
else
{
atomic
.
StoreUint32
(
&
locationEnabled
,
0
)
}
}
// locationEnabled is an atomic flag controlling whether the terminal formatter
// should append the log locations too when printing entries.
var
locationEnabled
uint32
// locationLength is the maxmimum path length encountered, which all logs are
// padded to to aid in alignment.
var
locationLength
uint32
type
Format
interface
{
Format
(
r
*
Record
)
[]
byte
}
...
...
@@ -64,12 +89,33 @@ func TerminalFormat() Format {
b
:=
&
bytes
.
Buffer
{}
lvl
:=
strings
.
ToUpper
(
r
.
Lvl
.
String
())
if
color
>
0
{
fmt
.
Fprintf
(
b
,
"
\x1b
[%dm%s
\x1b
[0m[%s] %s "
,
color
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
r
.
Msg
)
if
atomic
.
LoadUint32
(
&
locationEnabled
)
!=
0
{
// Log origin printing was requested, format the location path and line number
location
:=
fmt
.
Sprintf
(
"%+v"
,
r
.
Call
)
for
_
,
prefix
:=
range
locationTrims
{
location
=
strings
.
TrimPrefix
(
location
,
prefix
)
}
// Maintain the maximum location length for fancyer alignment
align
:=
int
(
atomic
.
LoadUint32
(
&
locationLength
))
if
align
<
len
(
location
)
{
align
=
len
(
location
)
atomic
.
StoreUint32
(
&
locationLength
,
uint32
(
align
))
}
padding
:=
strings
.
Repeat
(
" "
,
align
-
len
(
location
))
// Assemble and print the log heading
if
color
>
0
{
fmt
.
Fprintf
(
b
,
"
\x1b
[%dm%s
\x1b
[0m[%s|%s]%s %s "
,
color
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
location
,
padding
,
r
.
Msg
)
}
else
{
fmt
.
Fprintf
(
b
,
"[%s] [%s|%s]%s %s "
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
location
,
padding
,
r
.
Msg
)
}
}
else
{
fmt
.
Fprintf
(
b
,
"[%s] [%s] %s "
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
r
.
Msg
)
if
color
>
0
{
fmt
.
Fprintf
(
b
,
"
\x1b
[%dm%s
\x1b
[0m[%s] %s "
,
color
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
r
.
Msg
)
}
else
{
fmt
.
Fprintf
(
b
,
"[%s] [%s] %s "
,
lvl
,
r
.
Time
.
Format
(
termTimeFormat
),
r
.
Msg
)
}
}
// try to justify the log output for short messages
if
len
(
r
.
Ctx
)
>
0
&&
len
(
r
.
Msg
)
<
termMsgJust
{
b
.
Write
(
bytes
.
Repeat
([]
byte
{
' '
},
termMsgJust
-
len
(
r
.
Msg
)))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录