Skip to content

Commit f89dd62

Browse files
committed
internal, log: support debug log prints, displaying log origins
1 parent 1ca20a2 commit f89dd62

2 files changed

Lines changed: 61 additions & 7 deletions

File tree

‎internal/debug/flags.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,23 @@ import (
3030
var (
3131
verbosityFlag = cli.IntFlag{
3232
Name: "verbosity",
33-
Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=detail",
33+
Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
3434
Value: 3,
3535
}
3636
vmoduleFlag = cli.StringFlag{
3737
Name: "vmodule",
38-
Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=6,p2p=5)",
38+
Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
3939
Value: "",
4040
}
4141
backtraceAtFlag = cli.StringFlag{
4242
Name: "backtrace",
4343
Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
4444
Value: "",
4545
}
46+
debugFlag = cli.BoolFlag{
47+
Name: "debug",
48+
Usage: "Prepends log messages with call-site location (file and line number)",
49+
}
4650
pprofFlag = cli.BoolFlag{
4751
Name: "pprof",
4852
Usage: "Enable the pprof HTTP server",
@@ -78,17 +82,21 @@ var (
7882

7983
// Flags holds all command-line flags required for debugging.
8084
var Flags = []cli.Flag{
81-
verbosityFlag, vmoduleFlag, backtraceAtFlag,
85+
verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
8286
pprofFlag, pprofAddrFlag, pprofPortFlag,
8387
memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
8488
}
8589

90+
// glogger is the glog handler used by Geth, allowing the debug APIs to modify
91+
// verbosity levels, vmodules and backtrace locations.
8692
var glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat()))
8793

8894
// Setup initializes profiling and logging based on the CLI flags.
8995
// It should be called as early as possible in the program.
9096
func Setup(ctx *cli.Context) error {
9197
// logging
98+
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
99+
92100
glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
93101
glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
94102
glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))

‎log/format.go‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99
"strings"
1010
"sync"
11+
"sync/atomic"
1112
"time"
1213
)
1314

@@ -18,6 +19,30 @@ const (
1819
termMsgJust = 40
1920
)
2021

22+
// locationTrims are trimmed for display to avoid unwieldy log lines.
23+
var locationTrims = []string{
24+
"github.com/ethereum/go-ethereum/",
25+
"github.com/ethereum/ethash/",
26+
}
27+
28+
// PrintOrigins sets or unsets log location (file:line) printing for terminal
29+
// format output.
30+
func PrintOrigins(print bool) {
31+
if print {
32+
atomic.StoreUint32(&locationEnabled, 1)
33+
} else {
34+
atomic.StoreUint32(&locationEnabled, 0)
35+
}
36+
}
37+
38+
// locationEnabled is an atomic flag controlling whether the terminal formatter
39+
// should append the log locations too when printing entries.
40+
var locationEnabled uint32
41+
42+
// locationLength is the maxmimum path length encountered, which all logs are
43+
// padded to to aid in alignment.
44+
var locationLength uint32
45+
2146
type Format interface {
2247
Format(r *Record) []byte
2348
}
@@ -64,12 +89,33 @@ func TerminalFormat() Format {
6489

6590
b := &bytes.Buffer{}
6691
lvl := strings.ToUpper(r.Lvl.String())
67-
if color > 0 {
68-
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg)
92+
if atomic.LoadUint32(&locationEnabled) != 0 {
93+
// Log origin printing was requested, format the location path and line number
94+
location := fmt.Sprintf("%+v", r.Call)
95+
for _, prefix := range locationTrims {
96+
location = strings.TrimPrefix(location, prefix)
97+
}
98+
// Maintain the maximum location length for fancyer alignment
99+
align := int(atomic.LoadUint32(&locationLength))
100+
if align < len(location) {
101+
align = len(location)
102+
atomic.StoreUint32(&locationLength, uint32(align))
103+
}
104+
padding := strings.Repeat(" ", align-len(location))
105+
106+
// Assemble and print the log heading
107+
if color > 0 {
108+
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s|%s]%s %s ", color, lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg)
109+
} else {
110+
fmt.Fprintf(b, "[%s] [%s|%s]%s %s ", lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg)
111+
}
69112
} else {
70-
fmt.Fprintf(b, "[%s] [%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg)
113+
if color > 0 {
114+
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg)
115+
} else {
116+
fmt.Fprintf(b, "[%s] [%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg)
117+
}
71118
}
72-
73119
// try to justify the log output for short messages
74120
if len(r.Ctx) > 0 && len(r.Msg) < termMsgJust {
75121
b.Write(bytes.Repeat([]byte{' '}, termMsgJust-len(r.Msg)))

0 commit comments

Comments
 (0)