github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/internal/debug/flags.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package debug
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"net/http"
    23  	_ "net/http/pprof"
    24  	"os"
    25  	"runtime"
    26  
    27  	"github.com/intfoundation/intchain/log"
    28  	"github.com/intfoundation/intchain/log/term"
    29  	"github.com/intfoundation/intchain/metrics"
    30  	"github.com/intfoundation/intchain/metrics/exp"
    31  	colorable "github.com/mattn/go-colorable"
    32  	"gopkg.in/urfave/cli.v1"
    33  )
    34  
    35  var (
    36  	verbosityFlag = cli.IntFlag{
    37  		Name:  "verbosity",
    38  		Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
    39  		Value: 3,
    40  	}
    41  	vmoduleFlag = cli.StringFlag{
    42  		Name:  "vmodule",
    43  		Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
    44  		Value: "",
    45  	}
    46  	backtraceAtFlag = cli.StringFlag{
    47  		Name:  "backtrace",
    48  		Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
    49  		Value: "",
    50  	}
    51  	debugFlag = cli.BoolFlag{
    52  		Name:  "debug",
    53  		Usage: "Prepends log messages with call-site location (file and line number)",
    54  	}
    55  	pprofFlag = cli.BoolFlag{
    56  		Name:  "pprof",
    57  		Usage: "Enable the pprof HTTP server",
    58  	}
    59  	pprofPortFlag = cli.IntFlag{
    60  		Name:  "pprofport",
    61  		Usage: "pprof HTTP server listening port",
    62  		Value: 6060,
    63  	}
    64  	pprofAddrFlag = cli.StringFlag{
    65  		Name:  "pprofaddr",
    66  		Usage: "pprof HTTP server listening interface",
    67  		Value: "127.0.0.1",
    68  	}
    69  	memprofilerateFlag = cli.IntFlag{
    70  		Name:  "memprofilerate",
    71  		Usage: "Turn on memory profiling with the given rate",
    72  		Value: runtime.MemProfileRate,
    73  	}
    74  	blockprofilerateFlag = cli.IntFlag{
    75  		Name:  "blockprofilerate",
    76  		Usage: "Turn on block profiling with the given rate",
    77  	}
    78  	cpuprofileFlag = cli.StringFlag{
    79  		Name:  "cpuprofile",
    80  		Usage: "Write CPU profile to the given file",
    81  	}
    82  	traceFlag = cli.StringFlag{
    83  		Name:  "trace",
    84  		Usage: "Write execution trace to the given file",
    85  	}
    86  )
    87  
    88  // Flags holds all command-line flags required for debugging.
    89  var Flags = []cli.Flag{
    90  	verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
    91  	pprofFlag, pprofAddrFlag, pprofPortFlag,
    92  	memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
    93  }
    94  
    95  var (
    96  	ostream log.Handler
    97  	glogger *log.GlogHandler
    98  )
    99  
   100  func init() {
   101  	usecolor := term.IsTty(os.Stderr.Fd()) && os.Getenv("TERM") != "dumb"
   102  	output := io.Writer(os.Stderr)
   103  	if usecolor {
   104  		output = colorable.NewColorableStderr()
   105  	}
   106  	ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
   107  	glogger = log.NewGlogHandler(ostream)
   108  }
   109  
   110  // Setup initializes profiling and logging based on the CLI flags.
   111  // It should be called as early as possible in the program.
   112  func Setup(ctx *cli.Context) error {
   113  	// logging
   114  	// (Update, Logging has been setup in the main intchain cmd)
   115  	//log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
   116  	//if logdir != "" {
   117  	//	rfh, err := log.RotatingFileHandler(
   118  	//		logdir,
   119  	//		262144,
   120  	//		log.JSONFormatOrderedEx(false, true),
   121  	//	)
   122  	//	if err != nil {
   123  	//		return err
   124  	//	}
   125  	//	glogger.SetHandler(log.MultiHandler(ostream, rfh))
   126  	//}
   127  	//glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
   128  	//glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
   129  	//glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))
   130  	//log.Root().SetHandler(glogger)
   131  
   132  	// profiling, tracing
   133  	runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
   134  	Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
   135  	if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
   136  		if err := Handler.StartGoTrace(traceFile); err != nil {
   137  			return err
   138  		}
   139  	}
   140  	if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" {
   141  		if err := Handler.StartCPUProfile(cpuFile); err != nil {
   142  			return err
   143  		}
   144  	}
   145  
   146  	// pprof server
   147  	if ctx.GlobalBool(pprofFlag.Name) {
   148  		// Hook go-metrics into expvar on any /debug/metrics request, load all vars
   149  		// from the registry into expvar, and execute regular expvar handler.
   150  		exp.Exp(metrics.DefaultRegistry)
   151  
   152  		address := fmt.Sprintf("%s:%d", ctx.GlobalString(pprofAddrFlag.Name), ctx.GlobalInt(pprofPortFlag.Name))
   153  		go func() {
   154  			log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
   155  			if err := http.ListenAndServe(address, nil); err != nil {
   156  				log.Error("Failure in running pprof server", "err", err)
   157  			}
   158  		}()
   159  	}
   160  	return nil
   161  }
   162  
   163  // Exit stops all running profiles, flushing their output to the
   164  // respective file.
   165  func Exit() {
   166  	Handler.StopCPUProfile()
   167  	Handler.StopGoTrace()
   168  }