From 8507e15f29902a11e0b753d3239c1681394a5ede Mon Sep 17 00:00:00 2001 From: Jonathan Harvey-Buschel Date: Mon, 26 Aug 2024 17:04:37 -0400 Subject: [PATCH] main: add logcompressor flag --- config.go | 12 ++++++++++++ doc.go | 2 ++ log.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/config.go b/config.go index 9bbce7f69a..c5a206976a 100644 --- a/config.go +++ b/config.go @@ -41,6 +41,7 @@ const ( defaultLogLevel = "info" defaultLogDirname = "logs" defaultLogFilename = "btcd.log" + defaultLogCompressor = Gzip defaultMaxPeers = 125 defaultBanDuration = time.Hour * 24 defaultBanThreshold = 100 @@ -125,6 +126,7 @@ type config struct { FreeTxRelayLimit float64 `long:"limitfreerelay" description:"Limit relay of transactions with no transaction fee to the given amount in thousands of bytes per minute"` Listeners []string `long:"listen" description:"Add an interface/port to listen for connections (default all interfaces port: 8333, testnet: 18333)"` LogDir string `long:"logdir" description:"Directory to log output."` + LogCompressor string `long:"logcompressor" description:"Compression algorithm to use when rotating logs." choice:"gzip" choice:"zstd"` MaxOrphanTxs int `long:"maxorphantx" description:"Max number of orphan transactions to keep in memory"` MaxPeers int `long:"maxpeers" description:"Max number of inbound and outbound peers"` MiningAddrs []string `long:"miningaddr" description:"Add the specified payment address to the list of addresses to use for generated blocks -- At least one address is required if the generate option is set"` @@ -428,6 +430,7 @@ func loadConfig() (*config, []string, error) { RPCMaxConcurrentReqs: defaultMaxRPCConcurrentReqs, DataDir: defaultDataDir, LogDir: defaultLogDir, + LogCompressor: defaultLogCompressor, DbType: defaultDbType, RPCKey: defaultRPCKeyFile, RPCCert: defaultRPCCertFile, @@ -661,6 +664,15 @@ func loadConfig() (*config, []string, error) { os.Exit(0) } + // Validate that the selected log compression algorithm is supported. + if !supportedCompressor(cfg.LogCompressor) { + str := "%s: The specified log compressor [%v] is invalid" + err := fmt.Errorf(str, funcName, cfg.LogCompressor) + fmt.Fprintln(os.Stderr, err) + fmt.Fprintln(os.Stderr, usageMessage) + return nil, nil, err + } + // Initialize log rotation. After log rotation has been initialized, the // logger variables may be used. initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename)) diff --git a/doc.go b/doc.go index 47e4e626b7..6972f23baf 100644 --- a/doc.go +++ b/doc.go @@ -76,6 +76,8 @@ Application Options: (default all interfaces port: 8333, testnet: 18333, signet: 38333) --logdir= Directory to log output + --logcompressor= Compression algorithm to use when rotating logs. + (default: gzip) --maxorphantx= Max number of orphan transactions to keep in memory (default: 100) --maxpeers= Max number of inbound and outbound peers diff --git a/log.go b/log.go index 5707d7c23a..dcac10b350 100644 --- a/log.go +++ b/log.go @@ -105,6 +105,27 @@ var subsystemLoggers = map[string]btclog.Logger{ "TXMP": txmpLog, } +// Declare the supported compressors as exported consts for easier use from +// other projects. +const ( + Gzip = "gzip" + Zstd = "zstd" +) + +// logCompressors maps the identifier for each supported compression algorithm +// to the extension used for the compressed log files. +var logCompressors = map[string]string{ + Gzip: "gz", + Zstd: "zst", +} + +// supportedCompressor checks that the named compressor is known and supported +// for use during log rotation. +func supportedCompressor(compressor string) bool { + _, ok := logCompressors[compressor] + return ok +} + // initLogRotator initializes the logging rotater to write logs to logFile and // create roll files in the same directory. It must be called before the // package-global log rotater variables are used. @@ -121,6 +142,13 @@ func initLogRotator(logFile string) { os.Exit(1) } + // Reject unknown compressors. + if !supportedCompressor(cfg.LogCompressor) { + fmt.Fprintf(os.Stderr, "specified log compressor [%v] is "+ + "invalid", cfg.LogCompressor) + os.Exit(1) + } + logRotator = r }