diff --git a/config.go b/config.go index 68429ea9c7b..3f882981323 100644 --- a/config.go +++ b/config.go @@ -41,7 +41,7 @@ const ( defaultLogLevel = "info" defaultLogDirname = "logs" defaultLogFilename = "btcd.log" - defaultLogCompressor = "gzip" + defaultLogCompressor = Gzip defaultMaxPeers = 125 defaultBanDuration = time.Hour * 24 defaultBanThreshold = 100 @@ -126,7 +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. Valid algorithms are: gzip and zstd."` + 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"` @@ -665,7 +665,7 @@ func loadConfig() (*config, []string, error) { } // Validate that the selected log compression algorithm is supported. - if _, ok := logCompressors[cfg.LogCompressor]; !ok { + 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) diff --git a/log.go b/log.go index 54c72acb2c3..44742818b9f 100644 --- a/log.go +++ b/log.go @@ -107,11 +107,23 @@ 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", + Gzip: "gz", + Zstd: "zst", +} + +func supportedCompressor(compressor string) bool { + _, ok := logCompressors[compressor] + return ok } // initLogRotator initializes the logging rotater to write logs to logFile and @@ -130,8 +142,12 @@ func initLogRotator(logFile, logCompressor string) { os.Exit(1) } - // The selected compressor was validated by the caller, so we don't need - // to handle an unknown compressor here. + // Reject unknown compressors. + if !supportedCompressor(cfg.LogCompressor) { + fmt.Printf("specified log compressor [%v] is invalid", + cfg.LogCompressor) + } + var c rotator.Compressor switch logCompressor { case "gzip":