forked from celo-org/celo-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
279 lines (249 loc) · 8.18 KB
/
flags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package debug
import (
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"github.com/tmptmptmp53451/celo-blockchain/log"
"github.com/tmptmptmp53451/celo-blockchain/metrics"
"github.com/tmptmptmp53451/celo-blockchain/metrics/exp"
"github.com/fjl/memsize/memsizeui"
colorable "github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"gopkg.in/urfave/cli.v1"
)
var Memsize memsizeui.Handler
var (
verbosityFlag = cli.IntFlag{
Name: "verbosity",
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/*=5,p2p=4)",
Value: "",
}
backtraceAtFlag = cli.StringFlag{
Name: "backtrace",
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",
}
pprofPortFlag = cli.IntFlag{
Name: "pprofport",
Usage: "pprof HTTP server listening port",
Value: 6060,
}
pprofAddrFlag = cli.StringFlag{
Name: "pprofaddr",
Usage: "pprof HTTP server listening interface",
Value: "127.0.0.1",
}
memprofilerateFlag = cli.IntFlag{
Name: "memprofilerate",
Usage: "Turn on memory profiling with the given rate",
Value: runtime.MemProfileRate,
}
blockprofilerateFlag = cli.IntFlag{
Name: "blockprofilerate",
Usage: "Turn on block profiling with the given rate",
}
cpuprofileFlag = cli.StringFlag{
Name: "cpuprofile",
Usage: "Write CPU profile to the given file",
}
traceFlag = cli.StringFlag{
Name: "trace",
Usage: "Write execution trace to the given file",
}
consoleFormatFlag = cli.StringFlag{
Name: "consoleformat",
Usage: "Write console logs as 'json' or 'term'",
}
consoleOutputFlag = cli.StringFlag{
Name: "consoleoutput",
Usage: "(stderr|stdout|split) By default, console output goes to stderr. " +
"In stdout mode, write console logs to stdout (not stderr). " +
"In split mode, write critical(warning, error, and critical) console logs to stderr " +
"and non-critical (info, debug, and trace) to stdout",
}
)
// Flags holds all command-line flags required for debugging.
var Flags = []cli.Flag{
verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
pprofFlag, pprofAddrFlag, pprofPortFlag,
memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
consoleFormatFlag, consoleOutputFlag,
}
var (
ostream log.Handler
glogger *log.GlogHandler
)
type StdoutStderrHandler struct {
stdoutHandler log.Handler
stderrHandler log.Handler
}
func (this StdoutStderrHandler) Log(r *log.Record) error {
switch r.Lvl {
case log.LvlCrit:
fallthrough
case log.LvlError:
fallthrough
case log.LvlWarn:
return this.stderrHandler.Log(r)
case log.LvlInfo:
fallthrough
case log.LvlDebug:
fallthrough
case log.LvlTrace:
return this.stdoutHandler.Log(r)
default:
return this.stdoutHandler.Log(r)
}
}
func init() {
ostream = log.StreamHandler(io.Writer(os.Stderr), log.TerminalFormat(false))
glogger = log.NewGlogHandler(ostream)
}
// 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, logdir string) error {
// logging
consoleFormat := ctx.GlobalString(consoleFormatFlag.Name)
consoleOutputMode := ctx.GlobalString(consoleOutputFlag.Name)
ostream := CreateStreamHandler(consoleFormat, consoleOutputMode)
glogger = log.NewGlogHandler(ostream)
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
if logdir != "" {
rfh, err := log.RotatingFileHandler(
logdir,
262144,
log.JSONFormatOrderedEx(false, true),
)
if err != nil {
return err
}
glogger.SetHandler(log.MultiHandler(ostream, rfh))
}
glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))
log.Root().SetHandler(glogger)
// profiling, tracing
runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
if err := Handler.StartGoTrace(traceFile); err != nil {
return err
}
}
if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" {
if err := Handler.StartCPUProfile(cpuFile); err != nil {
return err
}
}
// pprof server
if ctx.GlobalBool(pprofFlag.Name) {
address := fmt.Sprintf("%s:%d", ctx.GlobalString(pprofAddrFlag.Name), ctx.GlobalInt(pprofPortFlag.Name))
StartPProf(address)
}
return nil
}
func CreateStreamHandler(consoleFormat string, consoleOutputMode string) log.Handler {
if consoleOutputMode == "stdout" {
usecolor := useColor(os.Stdout)
var output io.Writer
if usecolor {
output = colorable.NewColorableStdout()
} else {
output = io.Writer(os.Stdout)
}
return log.StreamHandler(output, getConsoleLogFormat(consoleFormat, usecolor))
}
// This is the default mode to maintain backward-compatibility with the geth command-line
if consoleOutputMode == "stderr" || len(consoleOutputMode) == 0 {
usecolor := useColor(os.Stderr)
var output io.Writer
if usecolor {
output = colorable.NewColorableStderr()
} else {
output = io.Writer(os.Stderr)
}
return log.StreamHandler(output, getConsoleLogFormat(consoleFormat, usecolor))
}
if consoleOutputMode == "split" {
usecolorStdout := useColor(os.Stdout)
usecolorStderr := useColor(os.Stderr)
var outputStdout io.Writer
var outputStderr io.Writer
if usecolorStdout {
outputStdout = colorable.NewColorableStdout()
} else {
outputStdout = io.Writer(os.Stdout)
}
if usecolorStderr {
outputStderr = colorable.NewColorableStderr()
} else {
outputStderr = io.Writer(os.Stderr)
}
return StdoutStderrHandler{
stdoutHandler: log.StreamHandler(outputStdout, getConsoleLogFormat(consoleFormat, usecolorStdout)),
stderrHandler: log.StreamHandler(outputStderr, getConsoleLogFormat(consoleFormat, usecolorStderr))}
}
panic(fmt.Sprintf("Unexpected value for \"%s\" flag: \"%s\"", consoleOutputFlag.Name, consoleOutputMode))
}
func useColor(file *os.File) bool {
return (isatty.IsTerminal(file.Fd()) || isatty.IsCygwinTerminal(file.Fd())) && os.Getenv("TERM") != "dumb"
}
func getConsoleLogFormat(consoleFormat string, usecolor bool) log.Format {
if consoleFormat == "json" {
return log.JSONFormat()
}
if consoleFormat == "term" || len(consoleFormat) == 0 /* No explicit format specified */ {
return log.TerminalFormat(usecolor)
}
panic(fmt.Sprintf("Unexpected value for \"%s\" flag: \"%s\"", consoleFormatFlag.Name, consoleFormat))
}
func StartPProf(address string) {
// Hook go-metrics into expvar on any /debug/metrics request, load all vars
// from the registry into expvar, and execute regular expvar handler.
exp.Exp(metrics.DefaultRegistry)
http.Handle("/memsize/", http.StripPrefix("/memsize", &Memsize))
log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
go func() {
if err := http.ListenAndServe(address, nil); err != nil {
log.Error("Failure in running pprof server", "err", err)
}
}()
}
// Exit stops all running profiles, flushing their output to the
// respective file.
func Exit() {
Handler.StopCPUProfile()
Handler.StopGoTrace()
}