From 64697a9647667fa9904b88b3475ff11da0eb2695 Mon Sep 17 00:00:00 2001 From: Enrique Jose Avila Asapche Date: Wed, 29 Jun 2022 05:29:59 +0300 Subject: [PATCH] torrent verbosity now uses int (#4551) * torrent verbosity now uses int * logging torrent verbosity * moved crit correctly --- .../downloader/downloadercfg/logger.go | 21 ++++++++++++++++--- cmd/downloader/main.go | 7 ++++--- cmd/utils/flags.go | 11 +++++----- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/cmd/downloader/downloader/downloadercfg/logger.go b/cmd/downloader/downloader/downloadercfg/logger.go index bf1da9bfb..f21650263 100644 --- a/cmd/downloader/downloader/downloadercfg/logger.go +++ b/cmd/downloader/downloader/downloadercfg/logger.go @@ -1,6 +1,7 @@ package downloadercfg import ( + "fmt" "strings" utp "github.com/anacrolix/go-libutp" @@ -13,10 +14,24 @@ func init() { utp.Logger.Handlers = []lg.Handler{noopHandler{}} } -func Str2LogLevel(in string) (lg.Level, error) { +func Int2LogLevel(level int) (lg.Level, error) { lvl := lg.Level{} - if err := lvl.UnmarshalText([]byte(in)); err != nil { - return lvl, err + + switch level { + case 0: + lvl = lg.NotSet + case 1: + lvl = lg.Critical + case 2: + lvl = lg.Error + case 3: + lvl = lg.Warning + case 4: + lvl = lg.Info + case 5: + lvl = lg.Debug + default: + return lvl, fmt.Errorf("invalid level set, expected a number between 0-5 but got: %d", level) } return lvl, nil } diff --git a/cmd/downloader/main.go b/cmd/downloader/main.go index acd3d856a..ac415b95e 100644 --- a/cmd/downloader/main.go +++ b/cmd/downloader/main.go @@ -38,7 +38,7 @@ var ( forceVerify bool downloaderApiAddr string natSetting string - torrentVerbosity string + torrentVerbosity int downloadRateStr, uploadRateStr string torrentDownloadSlots int torrentPort int @@ -55,9 +55,9 @@ func init() { rootCmd.Flags().StringVar(&natSetting, "nat", utils.NATFlag.Value, utils.NATFlag.Usage) rootCmd.Flags().StringVar(&downloaderApiAddr, "downloader.api.addr", "127.0.0.1:9093", "external downloader api network address, for example: 127.0.0.1:9093 serves remote downloader interface") - rootCmd.Flags().StringVar(&torrentVerbosity, "torrent.verbosity", utils.TorrentVerbosityFlag.Value, utils.TorrentVerbosityFlag.Usage) rootCmd.Flags().StringVar(&downloadRateStr, "torrent.download.rate", utils.TorrentDownloadRateFlag.Value, utils.TorrentDownloadRateFlag.Usage) rootCmd.Flags().StringVar(&uploadRateStr, "torrent.upload.rate", utils.TorrentUploadRateFlag.Value, utils.TorrentUploadRateFlag.Usage) + rootCmd.Flags().IntVar(&torrentVerbosity, "torrent.verbosity", utils.TorrentVerbosityFlag.Value, utils.TorrentVerbosityFlag.Usage) rootCmd.Flags().IntVar(&torrentPort, "torrent.port", utils.TorrentPortFlag.Value, utils.TorrentPortFlag.Usage) rootCmd.Flags().IntVar(&torrentMaxPeers, "torrent.maxpeers", utils.TorrentMaxPeersFlag.Value, utils.TorrentMaxPeersFlag.Usage) rootCmd.Flags().IntVar(&torrentConnsPerFile, "torrent.conns.perfile", utils.TorrentConnsPerFileFlag.Value, utils.TorrentConnsPerFileFlag.Usage) @@ -114,10 +114,11 @@ var rootCmd = &cobra.Command{ func Downloader(ctx context.Context) error { dirs := datadir.New(datadirCli) - torrentLogLevel, err := downloadercfg.Str2LogLevel(torrentVerbosity) + torrentLogLevel, err := downloadercfg.Int2LogLevel(torrentVerbosity) if err != nil { return err } + log.Info("torrentLogLevel", torrentLogLevel) var downloadRate, uploadRate datasize.ByteSize if err := downloadRate.UnmarshalText([]byte(downloadRateStr)); err != nil { diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index ff39c1e43..31b3a90ad 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -29,7 +29,6 @@ import ( "text/tabwriter" "text/template" - lg "github.com/anacrolix/log" "github.com/c2h5oh/datasize" "github.com/ledgerwatch/erigon-lib/kv/kvcache" "github.com/ledgerwatch/erigon-lib/txpool" @@ -645,10 +644,10 @@ var ( Name: ethconfig.FlagSnapStop, Usage: "Stop producing new snapshots", } - TorrentVerbosityFlag = cli.StringFlag{ + TorrentVerbosityFlag = cli.IntFlag{ Name: "torrent.verbosity", - Value: lg.Warning.LogString(), - Usage: "DBG | INF | WRN | ERR (must set --verbosity to equal or higher level)", + Value: 3, + Usage: "0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail (must set --verbosity to equal or higher level and has defeault: 3)", } TorrentDownloadRateFlag = cli.StringFlag{ Name: "torrent.download.rate", @@ -1401,8 +1400,8 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.C if err := uploadRate.UnmarshalText([]byte(uploadRateStr)); err != nil { panic(err) } - - lvl, err := downloadercfg.Str2LogLevel(ctx.GlobalString(TorrentVerbosityFlag.Name)) + log.Info("torrent verbosity", "level", ctx.GlobalInt(TorrentVerbosityFlag.Name)) + lvl, err := downloadercfg.Int2LogLevel(ctx.GlobalInt(TorrentVerbosityFlag.Name)) if err != nil { panic(err) }